New Tutorials:   JAVASCRIPT    SASS/SCSS    PL/SQL  
See the Tutorial List

Java Swing Components and Containers



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:

  
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:

  
import javax.swing.*;  
public class SOptionPaneDemo
{  
  JFrameoptionPane_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:

  
import javax.swing.*;  
public class SOptionPaneDemo
{  
  JFrameoptionPane_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:

  
import javax.swing.*;  
class SScrollBarDemo
{  
  SScrollBarDemo()
{  
JFramescrollBar_f= new JFrame("studytonight ==> Scrollbar Demo");  
  JScrollBarscrollBar_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.

Declaration

public class JMenuBar extends JComponent implements MenuElement, Accessible

Declaration

public class JMenu extends JMenuItem implements MenuElement, Accessible

Declaration

public class JMenuItem extends AbstractButton implements Accessible, MenuElement

Example:

  
import javax.swing.*;  
class SMenuDemo1  
{  
JMenum_menu, m_submenu;  
JMenuItem menu_i1, menu_i2, menu_i3, menu_i4, menu_i5;  
          SMenuDemo1()
      {  
JFramemenu_f= new JFrame("Menu and MenuItem Example");  
JMenuBarmenu_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 JFramepop_upf= new JFrame("studytonight ==>PopupMenu Demo");  
         final JPopupMenu popupmenu1 = new JPopupMenu("Edit");   
JMenuItempop_upcut = new JMenuItem("Cut");  
JMenuItempop_upcopy = new JMenuItem("Copy");  
JMenuItempop_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[]) 
  {  
JFramecheckbox_frame = new JFrame("studytonight ==>Jmenu Example");  
checkbox_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
JMenuBarcheckbox_menuBar = new JMenuBar();  
JMenucheckbox_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);  

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

ActionListenercheckbox_aListener = new ActionListener() 
    {  
            public void actionPerformed(ActionEvent event) 
      {  
AbstractButtoncheckbox_aButton = (AbstractButton) event.getSource();  
booleancheckbox_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
{    
JMenusep_menu, sep_submenu;    
JMenuItem sep_i1, sep_i2, sep_i3, sep_i4, sep_i5;    
SeparatorDemo()  
      {    
JFramesep_f= new JFrame("Separator Example");    
JMenuBarsep_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
{    
  JProgressBarprogBar_jb;    
  intprogBar_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) {    
ProgressBarDemoobj=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:

  
import javax.swing.*;  
import javax.swing.tree.DefaultMutableTreeNode;  
public class TreeDemo
{  
  JFrametree_f;  
  TreeDemo()
  {  
tree_f=new JFrame();   
DefaultMutableTreeNodetree_style=new DefaultMutableTreeNode("Style");  
DefaultMutableTreeNodetree_color=new DefaultMutableTreeNode("color");  
DefaultMutableTreeNodetree_font=new DefaultMutableTreeNode("font");  
tree_style.add(tree_color);  
tree_style.add(tree_font);  
DefaultMutableTreeNodetree_red=new DefaultMutableTreeNode("red");  
DefaultMutableTreeNodetree_blue=new DefaultMutableTreeNode("blue");  
DefaultMutableTreeNodetree_black=new DefaultMutableTreeNode("black");  
DefaultMutableTreeNodetree_green=new DefaultMutableTreeNode("green");  
tree_color.add(tree_red); 
  tree_color.add(tree_blue); 
  tree_color.add(tree_black); 
  tree_color.add(tree_green);      
JTreetree_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