Tuesday 5 February 2013

swing tutorial in pdf


Download swing tutorial in pdf format :

Click Download

Login form

Login Form in Swing:

To create a Login Form, we have used two class files:


1) NextPage.java
2) LoginDemo.java

Here is the code of NextPage.java

import javax.swing.*;
import java.awt.*;

class NextPage extends JFrame
{
  NextPage()
 {
 setDefaultCloseOperation(javax.swing.
  WindowConstants.DISPOSE_ON_CLOSE);
 setTitle("Welcome");
 setSize(400, 200);
  }
 }



 Here is the code of LoginDemo.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
  Login()
  {
  label1 = new JLabel();
  label1.setText("Username:");
  text1 = new JTextField(15);

  label2 = new JLabel();
  label2.setText("Password:");
  text2 = new JPasswordField(15);

  SUBMIT=new JButton("SUBMIT");

  panel=new JPanel(new GridLayout(3,1));
  panel.add(label1);
  panel.add(text1);
  panel.add(label2);
  panel.add(text2);
  panel.add(SUBMIT);
  add(panel,BorderLayout.CENTER);
  SUBMIT.addActionListener(this);
  setTitle("LOGIN FORM");
  }
 public void actionPerformed(ActionEvent ae)
  {
  String value1=text1.getText();
  String value2=text2.getText();
  if (value1.equals("roseindia") && value2.equals("roseindia")) {
  NextPage page=new NextPage();
  page.setVisible(true);
  JLabel label = new JLabel("Welcome:"+value1);
  page.getContentPane().add(label);
  }
  else{
  System.out.println("enter the valid username and password");
  JOptionPane.showMessageDialog(this,"Incorrect login or password",
  "Error",JOptionPane.ERROR_MESSAGE);
  }
}
}
 class LoginDemo
{
  public static void main(String arg[])
  {
  try
  {
  Login frame=new Login();
  frame.setSize(300,100);
  frame.setVisible(true);
  }
  catch(Exception e)
  {JOptionPane.showMessageDialog(null, e.getMessage());}
  }
}


Output Window:





create Animation in basic

How to create Animation:





Sample program:

import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;

public class CreateAnimation extends JFrame {
private static int DELAY = 90;
Insets insets;

  Color colors[] = { Color.PINK, Color.YELLOW, Color.GREEN, Color.RED,
  Color.BLUE, Color.CYAN };

  public void paint(Graphics graphics) {
  super.paint(graphics);
  if (insets == null) {
  insets = getInsets();
  }
  int steps=colors.length;
  int x = insets.left;
  int y = insets.top;
  int width = getWidth() - insets.left - insets.right;
  int height = getHeight() - insets.top - insets.bottom;
  
  synchronized (colors) {
  for (int i = 0; i < steps; i++) {
  graphics.setColor(colors[i]);
  graphics.fillRect(x, y, width, height);
  }
  }
  }
  public void go() {
  TimerTask timerTask = new TimerTask() {
  public void run() {
  Color color = colors[0];
  synchronized (colors) {
  System.arraycopy(colors, 1, colors, 0, colors.length - 1);
  colors[colors.length - 1] = color;
  }
  repaint();
  }
  };
  Timer timer = new Timer();
  timer.schedule(timerTask, 0, DELAY);
  }
 public static void main(String args[]) {
  CreateAnimation animation = new CreateAnimation();
  animation.setSize(250200);
  animation.show();
  animation.go();
  }
}

Monday 4 February 2013

Text Area In Java

Text Area In Java:


Sample program:

import javax.swing.*;

public class TextAreaExample {

  public static void main(String[] args){
  JFrame frame= new JFrame("TextArea frame");
  JPanel panel=new JPanel();
  JTextArea jt= new JTextArea("Welcome Roseindia",5,20);
  frame.add(panel);
  panel.add(jt);
  frame.setSize(250,200);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Tab menu in java

Add and Remove Tab to a JTabbedPane Container:

 




Sample program:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class AddRemoveTab{
  JTabbedPane tab;
  public static void main(String[] args){
  AddRemoveTab ar = new AddRemoveTab();
  }

  public AddRemoveTab(){
  JFrame frame = new JFrame("Add Remove Tab Frame");
  tab = new JTabbedPane();
  frame.add(tab, BorderLayout.CENTER);
  JPanel panel = new JPanel();
  JButton button = new JButton("Add Tab");
  button.addActionListener(new MyAction());
  panel.add(button);
  tab.add("Add Tab", panel);
  JPanel panel1 = new JPanel();
  JButton button1 = new JButton("Remove Tab");
  button1.addActionListener(new MyAction());
  panel1.add(button1);
  tab.add("Remove Tab", panel1);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent e){
  String str = e.getActionCommand();
  if(str.equals("Add Tab")){
  String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
  if(!st.equals("")){
  JPanel panel2 = new JPanel();
  JLabel label = new JLabel("Your program is working successfully.");
  panel2.add(label);
  tab.add(st, panel2);
  }
  }
  else if(str.equals("Remove Tab")){
  tab.remove(tab.getTabCount()-1);
  }
  }
  }
}



Progress Bar

Progress Bar in java swing

Sample program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.html.*;

public class SwingProgressBar{
  final static int interval = 1000;
  int i;
  JLabel label;
  JProgressBar pb;
  Timer timer;
  JButton button;

  public SwingProgressBar() {
  JFrame frame = new JFrame("Swing Progress Bar");
  button = new JButton("Start");
  button.addActionListener(new ButtonListener());

  pb = new JProgressBar(020);
  pb.setValue(0);
  pb.setStringPainted(true);

  label = new JLabel("Roseindia.net");
  
  JPanel panel = new JPanel();
  panel.add(button);
  panel.add(pb);

  JPanel panel1 = new JPanel();
  panel1.setLayout(new BorderLayout());
  panel1.add(panel, BorderLayout.NORTH);
  panel1.add(label, BorderLayout.CENTER);
  panel1.setBorder(BorderFactory.createEmptyBorder(20202020));
  frame.setContentPane(panel1);
  frame.pack();
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //Create a timer.
  timer = new Timer(interval, new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  if (i == 20){
  Toolkit.getDefaultToolkit().beep();
  timer.stop();
  button.setEnabled(true);
  pb.setValue(0);
  String str = "<html>" "<font color=\"#FF0000\">" "<b>" +
"Downloading completed." "</b>" "</font>" "</html>";
  label.setText(str);
  }
  i = i + 1;
  pb.setValue(i);
  }
  });
  }

  class ButtonListener implements ActionListener {
  public void actionPerformed(ActionEvent ae) {
  button.setEnabled(false);
  i = 0;
  String str = "<html>" "<font color=\"#008000\">" "<b>" +
"Downloading is in process......." "</b>" "</font>" "</html>";
  label.setText(str);
  timer.start();
  }
  }
  
  public static void main(String[] args) {
  SwingProgressBar spb = new SwingProgressBar();
  }
}

Slider in java swing

JSlider Component of Java Swing












Sample program:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class CreateSlider{
  JSlider slider;
  JLabel label;
  public static void main(String[] args){
  CreateSlider cs = new CreateSlider();
  }

  public CreateSlider(){
  JFrame frame = new JFrame("Slider Frame");
  slider = new JSlider();
  slider.setValue(70);
  slider.addChangeListener(new MyChangeAction());
  label = new JLabel("Roseindia.net");
  JPanel panel = new JPanel();
  panel.add(slider);
  panel.add(label);
  frame.add(panel, BorderLayout.CENTER);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyChangeAction implements ChangeListener{
  public void stateChanged(ChangeEvent ce){
  int value = slider.getValue();
  String str = Integer.toString(value);
  label.setText(str);
  }
  }
}

Drawing with Color in Java


Drawing with Color in Java






sample program:

import javax.swing.*;
import java.awt.*;

public class DrawingColor{
  public static void main(String[] args) {
  DrawingColor d = new DrawingColor();
  }

  public DrawingColor(){
  JFrame frame = new JFrame("Drawing with Alpha");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(new MyComponent());
  frame.setSize(400,400);
  frame.setVisible(true);  
  }

  public class MyComponent extends JComponent{
  public void paint(Graphics g){
  int height = 200;
  int width = 120;
  g.setColor(Color.red);
  g.drawRect(10,10,height,width);
  g.setColor(Color.gray);
  g.fillRect(11,11,height,width)
  g.setColor(Color.red);
  g.drawOval(250,20, height,width);
  g.setColor(Color.magenta);
  g.fillOval(249,19,height,width)
  }
  }
}

Toolbar

JToolbar





Sample program:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;


public class Example extends JFrame {

    public Example() {
        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");
        menubar.add(file);
        setJMenuBar(menubar);

        JToolBar toolbar = new JToolBar();

        ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));

        JButton exitButton = new JButton(icon);
        toolbar.add(exitButton);
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });

        add(toolbar, BorderLayout.NORTH);

        setTitle("Simple toolbar");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Example ex = new Example();
                ex.setVisible(true);
            }
        });
    }
}

popup menu

popup menu:

                         

 Sample program:


import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;


public class PopupMenu extends JFrame
{

    private JPopupMenu menu;
    private Toolkit toolkit;

    public PopupMenu(String title)
    {
        super(title);
        this.initUI();
    }

    private void initUI()
    {
        toolkit = this.getToolkit();

        menu = new JPopupMenu();
        JMenuItem menuItemBeep = new JMenuItem("Beep");

        menuItemBeep.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                toolkit.beep();
            }
        });

        menu.add(menuItemBeep);

        JMenuItem menuItemClose = new JMenuItem("Close");
        menuItemClose.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

        menu.add(menuItemClose);

        this.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                if (e.getButton() == MouseEvent.BUTTON3)
                {
                    menu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(250, 200);
        this.setLocationRelativeTo(null);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                PopupMenu pm = new PopupMenu("JPopupMenu");
                pm.setVisible(true);
            }
        });

    }
}

 

 

 

 

CheckBox

JCheckBoxMenuItem






Sample program:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;


public class Example extends JFrame {

    private JLabel statusbar;

    public Example() {
        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);

        JMenu view = new JMenu("View");
        view.setMnemonic(KeyEvent.VK_V);

        JCheckBoxMenuItem sbar = new JCheckBoxMenuItem("Show StatuBar");
        sbar.setState(true);

        sbar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              if (statusbar.isVisible()) {
                  statusbar.setVisible(false);
              } else {
                  statusbar.setVisible(true);
              }
            }

        });

        view.add(sbar);

        menubar.add(file);
        menubar.add(view);

        setJMenuBar(menubar);

        statusbar = new JLabel(" Statusbar");
        statusbar.setBorder(BorderFactory.createEtchedBorder(
                EtchedBorder.RAISED));
        add(statusbar, BorderLayout.SOUTH);

        setTitle("JCheckBoxMenuItem");
        setSize(360, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Example ex = new Example();
                ex.setVisible(true);
            }
        });
    }
}

Sub menu

Submenu




Sample program:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;


public class Example extends JFrame {

    public Example() {
        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        ImageIcon iconNew = new ImageIcon(getClass().getResource("new.png"));
        ImageIcon iconOpen = new ImageIcon(getClass().getResource("open.png"));
        ImageIcon iconSave = new ImageIcon(getClass().getResource("save.png"));
        ImageIcon iconExit = new ImageIcon(getClass().getResource("exit.png"));

        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);

        JMenu imp = new JMenu("Import");
        imp.setMnemonic(KeyEvent.VK_M);

        JMenuItem newsf = new JMenuItem("Import newsfeed list...");
        JMenuItem bookm = new JMenuItem("Import bookmarks...");
        JMenuItem mail = new JMenuItem("Import mail...");

        imp.add(newsf);
        imp.add(bookm);
        imp.add(mail);

        JMenuItem fileNew = new JMenuItem("New", iconNew);
        fileNew.setMnemonic(KeyEvent.VK_N);

        JMenuItem fileOpen = new JMenuItem("Open", iconOpen);
        fileNew.setMnemonic(KeyEvent.VK_O);

        JMenuItem fileSave = new JMenuItem("Save", iconSave);
        fileSave.setMnemonic(KeyEvent.VK_S);

        JMenuItem fileExit = new JMenuItem("Exit", iconExit);
        fileExit.setMnemonic(KeyEvent.VK_C);
        fileExit.setToolTipText("Exit application");
        fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
            ActionEvent.CTRL_MASK));

        fileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });

        file.add(fileNew);
        file.add(fileOpen);
        file.add(fileSave);
        file.addSeparator();
        file.add(imp);
        file.addSeparator();
        file.add(fileExit);

        menubar.add(file);

        setJMenuBar(menubar);

        setTitle("Submenu");
        setSize(360, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Example ex = new Example();
                ex.setVisible(true);
            }
        });
    }
}

Simple menu

Simple menu










sample program:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;


public class Example extends JFrame {

    public Example() {
        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));

        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);

        JMenuItem eMenuItem = new JMenuItem("Exit", icon);
        eMenuItem.setMnemonic(KeyEvent.VK_C);
        eMenuItem.setToolTipText("Exit application");
        eMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });

        file.add(eMenuItem);

        menubar.add(file);

        setJMenuBar(menubar);

        setTitle("Simple menu");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Example ex = new Example();
                ex.setVisible(true);
            }
        });
    }
}

How to create a button in java swing

 Sample program:



 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Example extends JFrame {

    public Example() {
        initUI();
    }

    public final void initUI() {

       JPanel panel = new JPanel();
       getContentPane().add(panel);

       panel.setLayout(null);

       JButton quitButton = new JButton("Quit");
       quitButton.setBounds(50, 60, 80, 30);
       quitButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event) {
               System.exit(0);
          }
       });

       panel.add(quitButton);

       setTitle("Quit button");
       setSize(300, 200);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Example ex = new Example();
                ex.setVisible(true);
            }
        });
    }
}