Signup/Sign In

Java Swing Components and Containers

In our previous topic, we discussed about swing and its some component. Now here we will cover rest of the components of swing framework. Lets start with JoptionPane.

JOptionPane

In Java, Swing toolkit contains a JOptionPane Class. It is under package javax.swing.JOptionPane class. It is used for creating dialog boxes for displaying a message, confirm box or input dialog box.

Declaration

public class JOptionPane extends JComponent implements Accessible

The JOptionPaneContains 3 constructors. They are as following:

1. JOptionPane()

2. JOptionPane(Object message)

3. JOptionPane(Object message, intmessageType)

Example:

Lets take an example to create JoptionPane to display dialogue box. See the below example.

  
import javax.swing.*;  
public class SOptionPaneDemo
{  
  JFrameoptionPane_f;  
  SOptionPaneDemo()
  {  
    optionPane_f=new JFrame();  
    JOptionPane.showMessageDialog(optionPane_f,"Hello, Welcome to studytonight.com");  
  }  
  public static void main(String[] args) 
  {  
    new SOptionPaneDemo();  
  }  
}   
  

swing-paneout swing-paneout

Example:

Lets take another example to understand the use of JoptionPane. Here we are creating a dialogue to display a update message to the user.

  
import javax.swing.*;  
public class SOptionPaneDemo
{  
  JFrame optionPane_f;  
  SOptionPaneDemo()
  {  
    optionPane_f=new JFrame();  
    JOptionPane.showMessageDialog(optionPane_f,"Successfully Updated.","studytonight",JOptionPane.WARNING_MESSAGE);     
  }  
  public static void main(String[] args) 
  {  
    new SOptionPaneDemo();  
  }  
}
  

swing-pane swing-pane

Example:

We can use JoptionPane to create prompt box that used to get user input. In this example, we are creating a dialogue box to get user name.

  
import javax.swing.*;  
public class SOptionPaneDemo
{  
  JFrame optionPane_f;  
  SOptionPaneDemo()
  {  
    optionPane_f=new JFrame();  
    JOptionPane.showInputDialog(optionPane_f,"Enter your Name");     
  }  
  public static void main(String[] args) 
  {  
    new SOptionPaneDemo();  
  }  
}   
  

swing-optionpane swing-optionpane

JScrollBar

In Java, Swing toolkit contains a JScrollBar class. It is under package javax.swing.JScrollBar class. It is used for adding horizontal and vertical scrollbar.

Declaration

public class JScrollBar extends JComponent implements Adjustable, Accessible

The JScrollBarContains 3 constructors. They are as following:

1. JScrollBar()

2. JScrollBar(int orientation)

3. JScrollBar(int orientation, int value, int extent, int min_, intmax_)

Example:

We can use swing JscrollBar class to create horizontal and vertical scrollbar. In this example, we are creating horizontal and vertical scrollbar.

  
import javax.swing.*;  
class SScrollBarDemo
{  
  SScrollBarDemo()
  {  
    JFrame scrollBar_f= new JFrame("studytonight ==> Scrollbar Demo");  
    JScrollBar scrollBar_s=new JScrollBar();  
    scrollBar_s.setBounds(100,100, 80,100);  
    scrollBar_f.add(scrollBar_s);  
    scrollBar_f.setSize(500,500);  
    scrollBar_f.setLayout(null);  
    scrollBar_f.setVisible(true);  
  }  
  public static void main(String args[])  
  {  
    new SScrollBarDemo();  
  }
}
  

swing-scroller swing-scroller

JMenuBar, JMenu and JMenuItem

In Java, Swing toolkit contains a JMenuBar, JMenu and JMenuItem class. It is under package javax.swing.JMenuBar, javax.swing.JMenu and javax.swing.JMenuItem class. The JMenuBar class is used for displaying menubar on the frame. The JMenu Object is used for pulling down the components of the menu bar. The JMenuItem Object is used for adding the labelled menu item.

JMenuBar, JMenu and JMenuItem Declaration

public class JMenuBar extends JComponent implements MenuElement, Accessible

public class JMenu extends JMenuItem implements MenuElement, Accessible

public class JMenuItem extends AbstractButton implements Accessible, MenuElement

Example:

Lets take an example to create menu and sub menu in the swing jframe container. See the below example.

  
import javax.swing.*;  
class SMenuDemo1  
{  
  JMenu m_menu, m_submenu;  
  JMenuItem menu_i1, menu_i2, menu_i3, menu_i4, menu_i5;  
  SMenuDemo1()
  {  
    JFrame menu_f= new JFrame("Menu and MenuItem Example");  
    JMenuBar menu_mb=new JMenuBar();  
    m_menu=new JMenu("Menu");  
    m_submenu=new JMenu("Sub Menu");  
    menu_i1=new JMenuItem("Red");  
    menu_i2=new JMenuItem("Pink");  
    menu_i3=new JMenuItem("Black");  
    menu_i4=new JMenuItem("Green");  
    menu_i5=new JMenuItem("White");  
    m_menu.add(menu_i1); 
    m_menu.add(menu_i2); 
    m_menu.add(menu_i3);  
    m_submenu.add(menu_i4); 
    m_submenu.add(menu_i5);  
    m_menu.add(m_submenu);  
    menu_mb.add(m_menu);  
    menu_f.setJMenuBar(menu_mb);  
    menu_f.setSize(500,500);  
    menu_f.setLayout(null);  
    menu_f.setVisible(true);  
  }  
  public static void main(String args[])  
  {  
    new SMenuDemo1();  
  }
} 
  

swing-menu swing-menu

JPopupMenu

In Java, Swing toolkit contains a JPopupMenu Class. It is under package javax.swing.JPopupMenu class. It is used for creating popups dynamically on a specified position.

Declaration

public class JPopupMenu extends JComponent implements Accessible, MenuElement

The JPopupMenuContains 2 constructors. They are as following:

1. JPopupMenu()

2. JPopupMenu(String label)

Example:

  
import javax.swing.*;  
import java.awt.event.*;  
class PopupMenuDemo
{  
  PopupMenuDemo(){  
    final JFrame pop_upf= new JFrame("studytonight ==>PopupMenu Demo");  
    final JPopupMenu popupmenu1 = new JPopupMenu("Edit");   
    JMenuItem pop_upcut = new JMenuItem("Cut");  
    JMenuItem pop_upcopy = new JMenuItem("Copy");  
    JMenuItem pop_uppaste = new JMenuItem("Paste");  
    popupmenu1.add(pop_upcut); 
    popupmenu1.add(pop_upcopy); 
    popupmenu1.add(pop_uppaste);        
    pop_upf.addMouseListener(new MouseAdapter() 
    {  
      public void mouseClicked(MouseEvent a) 
      {              
        popupmenu1.show(pop_upf ,a.getX(), a.getY());  
      }                 
    });  
    pop_upf.add(popupmenu1);   
    pop_upf.setSize(300,300);  
    pop_upf.setLayout(null);  
    pop_upf.setVisible(true);  
  }  
  public static void main(String args[])  
  {  
    new PopupMenuDemo();  
  }
}
  

swing-popup swing-popup

JCheckBoxMenuItem

In Java, Swing toolkit contains a JCheckBoxMenuItem Class. It is under package javax.swing.JCheckBoxMenuItem class. It is used to create a checkbox on a menu.

The JCheckBoxMenuItemContains 2 constructors. They are as following:

1. JCheckBoxMenuItem()

2. JCheckBoxMenuItem(Action a)

3. JCheckBoxMenuItem(Icon icon)

4. JCheckBoxMenuItem(String text)

5. JCheckBoxMenuItem(String text, boolean b)

6. JCheckBoxMenuItem(String text, Icon icon)

7. JCheckBoxMenuItem(String text, Icon icon, boolean b)

Example:

  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.awt.event.KeyEvent;  
import javax.swing.AbstractButton;  
import javax.swing.Icon;  
import javax.swing.JCheckBoxMenuItem;  
import javax.swing.JFrame;  
import javax.swing.JMenu;  
import javax.swing.JMenuBar;  
import javax.swing.JMenuItem;  

public class SCheckBoxDemo
{  
  public static void main(final String args[]) 
  {  
    JFrame checkbox_frame = new JFrame("studytonight ==>Jmenu Example");  
    checkbox_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    JMenuBar checkbox_menuBar = new JMenuBar();  
    JMenu checkbox_fileMenu = new JMenu("File");  
    checkbox_fileMenu.setMnemonic(KeyEvent.VK_F);  
    checkbox_menuBar.add(checkbox_fileMenu);  
    JMenuItem checkbox_menuItem1 = new JMenuItem("Open", KeyEvent.VK_N);  
    checkbox_fileMenu.add(checkbox_menuItem1);  

    JCheckBoxMenuItem checkbox_caseMenuItem = new JCheckBoxMenuItem("Option_1");  
    checkbox_caseMenuItem.setMnemonic(KeyEvent.VK_C);  
    checkbox_fileMenu.add(checkbox_caseMenuItem);  

    ActionListener checkbox_aListener = new ActionListener() 
    {  
      public void actionPerformed(ActionEvent event) 
      {  
        AbstractButton checkbox_aButton = (AbstractButton) event.getSource();  
        boolean checkbox_selected = checkbox_aButton.getModel().isSelected();  
        String checkbox_newLabel;  
        Icon checkbox_newIcon;  
        if (checkbox_selected) {  
          checkbox_newLabel = "Value-1";  
        } else {  
          checkbox_newLabel = "Value-2";  
        }  
        checkbox_aButton.setText(checkbox_newLabel);  
      }  
    };  

    checkbox_caseMenuItem.addActionListener(checkbox_aListener);  
    checkbox_frame.setJMenuBar(checkbox_menuBar);  
    checkbox_frame.setSize(350, 250);  
    checkbox_frame.setVisible(true);  
  }  
} 
  

swing-checkbox swing-checkbox

JSeparator

In Java, Swing toolkit contains a JSeparator Class. It is under package javax.swing.JSeparator class. It is used for creating a separator line between two components.

Declaration

public class JSeparator extends JComponent implements SwingConstants, Accessible

The JSeparatorContains 2 constructors. They are as following:

1. JSeparator()

2. JSeparator(int orientation)

Example:

  
import javax.swing.*;    
class SeparatorDemo
{    
  JMenu sep_menu, sep_submenu;    
  JMenuItem sep_i1, sep_i2, sep_i3, sep_i4, sep_i5;    
  SeparatorDemo()  
  {    
    JFrame sep_f= new JFrame("Separator Example");    
    JMenuBar sep_mb=new JMenuBar();    
    sep_menu = new JMenu("Menu");    
    sep_i1=new JMenuItem("Black");    
    sep_i2=new JMenuItem("White");       
    sep_menu.add(sep_i1);  
    sep_menu.addSeparator();  
    sep_menu.add(sep_i2);  
    sep_mb.add(sep_menu);    
    sep_f.setJMenuBar(sep_mb);    
    sep_f.setSize(500,500);    
    sep_f.setLayout(null);    
    sep_f.setVisible(true);    
  }    
  public static void main(String args[])    
  {    
    new SeparatorDemo();    
  }
}
  

swing-seprator swing-seprator


JProgressBar

In Java, Swing toolkit contains a JProgressBar Class. It is under package javax.swing.JProgressBarclass. It is used for creating a progress bar of a task.

Declaration

public class JProgressBar extends JComponent implements SwingConstants, Accessible

The JProgressBarContains 4 constructors. They are as following:

1. JProgressBar()

2. JProgressBar(int min, int max)

3. JProgressBar(int orient)

4. JProgressBar(int orient, int min, int max)

Example:

  
import javax.swing.*;    
public class ProgressBarDemo extends JFrame
{    
  JProgressBar progBar_jb;    
  int progBar_i=0, progBar_num=0;     
  ProgressBarDemo()
  {    
    progBar_jb=new JProgressBar(0,2000);    
    progBar_jb.setBounds(40,40,180,30);         
    progBar_jb.setValue(0);    
    progBar_jb.setStringPainted(true);    
    add(progBar_jb);    
    setSize(250,150);    
    setLayout(null);    
  }    
  public void iterate()
  {    
    while(progBar_i<=2000)
    {    
      progBar_jb.setValue(progBar_i);    
      progBar_i = progBar_i + 10;    
      try
      {
        Thread.sleep(150);
      }
      catch(Exception e){}    
    }    
  }    
  public static void main(String[] args) {    
    ProgressBarDemo obj=new ProgressBarDemo();    
    obj.setVisible(true);    
    obj.iterate();    
  }    
} 
  

swing-progressbar swing-progressbar

JTree

In Java, Swing toolkit contains a JTree Class. It is under package javax.swing.JTreeclass. It is used for creating a tree-structured of data. It is a very complex component.

Declaration

public class JTree extends JComponent implements Scrollable, Accessible

The JTreeContains 3 constructors. They are as following:

1. JTree()

2. JTree(Object[] value)

3. JTree(TreeNode root)

Example:

In this example, we are creating a tree structure of a menu that shows a directory. We used Jtree class to create tree structure. See the below example.

  
import javax.swing.*;  
import javax.swing.tree.DefaultMutableTreeNode;  
public class TreeDemo
{  
  JFrame tree_f;  
  TreeDemo()
  {  
    tree_f=new JFrame();   
    DefaultMutableTreeNode tree_style=new DefaultMutableTreeNode("Style");  
    DefaultMutableTreeNode tree_color=new DefaultMutableTreeNode("color");  
    DefaultMutableTreeNode tree_font=new DefaultMutableTreeNode("font");  
    tree_style.add(tree_color);  
    tree_style.add(tree_font);  
    DefaultMutableTreeNode tree_red=new DefaultMutableTreeNode("red");  
    DefaultMutableTreeNode tree_blue=new DefaultMutableTreeNode("blue");  
    DefaultMutableTreeNode tree_black=new DefaultMutableTreeNode("black");  
    DefaultMutableTreeNode tree_green=new DefaultMutableTreeNode("green");  
    tree_color.add(tree_red); 
    tree_color.add(tree_blue); 
    tree_color.add(tree_black); 
    tree_color.add(tree_green);      
    JTree tree_jt=new JTree(tree_style);  
    tree_f.add(tree_jt);  
    tree_f.setSize(200,200);  
    tree_f.setVisible(true);  
  }  
  public static void main(String[] args) {  
    new TreeDemo();  
  }
}
  

swing-tree swing-tree