Java Swing Components and Containers part 2
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.
Â
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.
Â
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.
Â
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.
Â
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.
Â
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:
Â
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:
Â
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:
Â
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:
Â
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.
Â
Â