UI With Swing
UI With Swing
● The object of a JTextArea class is a multi line region that displays text.
● It allows the editing of multiple line text.
● Commonly used constructor are:
○ JTextArea()
○ JTextArea(String s)
○ JTextArea(int row, int column)
○ JTextArea(String s, int row, int column)
JButton
● The JButton class provides the functionality of a push button.
● JButton allows an icon, a string, or both to be associated with the push button.
Three of its constructors are shown here:
○ JButton(Icon icon)
○ JButton(String str)
○ JButton(String str, Icon icon)
● Here, str and icon are the string and icon used for the button.
● When the button is pressed, an ActionEvent is generated.
JPasswordField
● The object of a JPasswordField class is a text component specialized for
password entry.
● It allows the editing of a single line of text.
● It inherits JTextField class.
● Common Constructors are:
○ JPasswordField()
○ JPasswordField(String str)
○ JPasswordField(int column)
○ JPasswordField(String str, int column)
Check Boxes
● The JCheckBox class provides the functionality of a check box.
● JCheckBox defines several constructors.
○ JCheckBox(String str)
○ JCheckBox(String text, boolean selected)
○ JCheckBox()
● When the user selects or deselect a checkbox, an ItemEvent is generated.
● The easiest way to determine the selected state of a check box is to call
isSelected( ) on the JCheckBox instance.
Radio Buttons
● Radio buttons are a group of mutually exclusive buttons, in which only one
button can be selected at any one time.
● JRadioButton provides several constructors.
○ JRadioButton(String str)
● Here, str is the label for the button. Other constructors let you specify the
initial selection state of the button and specify an icon.
● In order for their mutually exclusive nature to be activated, radio buttons must
be configured into a group.
● Only one of the buttons in the group can be selected at any time.
● A button group is created by the ButtonGroup class.
● A a reference to the button to be added to the group.
JComboBox
● Swing provides a combo box (a combination of a text field and a drop-down list)
through the JComboBox class.
● A combo box normally displays one entry, but it will also display a drop-down list that
allows a user to select a different entry.
● You can also create a combo box that lets the user enter a selection into the text field.
● In the past, the items in a JComboBox were represented as Object references.
However, beginning with JDK 7, JComboBox was made generic and is now declared
like this: class JComboBox<E>
● Here, E represents the type of the items in the combo box.
● The JComboBox constructor:
○ JComboBox(E[ ] items)
○ JComboBox()
● Here, items is an array that initializes the combo box.
Slider
● The Java JSlider class is used to create the slider.
● By using JSlider, a user can select a value from a specific range.
● Constructor:
○ JSlider()
○ JSlider(int orientation)
○ JSlider(int min, int max)
○ JSlider(int min, int max, int value)
○ JSlider(int orientation, int min, int max, int value)
To create a menu bar, first create an instance of MenuBar. This class defines only
the default constructor.
● JMenuBar()
2. Create menu
Following are the constructors for Menu:
● JMenu( ) throws HeadlessException
● JMenu(String optionName) throws HeadlessException
● JMenu(String optionName, boolean removable) throws HeadlessException
● Here, optionName specifies the name of the menu selection. If removable is
true, the menu can be removed and allowed to float free. Otherwise, it will
remain attached to the menu bar. (Removable menus are implementation-
dependent.)
● The first form creates an empty menu.
3. Create Menu Item
● Individual menu items are of type MenuItem.
● It defines these constructors:
○ JMenuItem( )
○ MenuItem(String itemName)
○ MenuItem(String itemName, int memonic)
● You can disable or enable a menu item by using the setEnabled( ) method. Its form is shown here:
○ void setEnabled(boolean enabledFlag)
○ If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is
disabled.
Dailog Box
● Often, you will want to use a dialog box to hold a set of related controls.
● Dialog boxes are primarily used to obtain user input and are often child windows of a top-level
window.
● Dialog boxes don’t have menu bars, but in other respects, they function like frame windows. (You
can add controls to them, for example, in the same way that you add controls to a frame window.)
● Dialog boxes may be modal or modeless. When a modal dialog box is active, all input is directed
to it until it is closed. This means that you cannot access other parts of your program until you
have closed the dialog box.
● When a modeless dialog box is active, input focus can be directed to another window in your
program. Thus, other parts of your program remain active and accessible.
● In the Swing, dialog boxes are of type Dialog. Two commonly used constructors are shown here:
● JDialog(Frame parentWindow, boolean mode)
● JDialog(Frame parentWindow, String title, boolean mode)
● Here, parentWindow is the owner of the dialog box. If mode is true, the dialog
box is modal. Otherwise, it is modeless. The title of the dialog box can be
passed in title. Generally, you will subclass Dialog, adding the functionality
required by your application.
FileDialog
● Java provides a built-in dialog box that lets the user specify a file.
● To create a file dialog box, instantiate an object of type JFileChooser. This
causes a file dialog box to be displayed.
● Usually, this is the standard file dialog box provided by the operating system.
Here are three FileDialog constructors:
○ JFileChooser()
○ JFileChooser(String currentDirectory)
● FileDialog provides methods that allow you to determine the name of the file
and its path as selected by the user. Here are two examples:
○ String getDirectory( )
○ String getFile( )
● These methods return the directory and the filename, respectively.
Tooltip
● Tooltips are small windows of text that popup when the user leaves the mouse
cursor over a component.
● They are used to explain the functionality of the component.
● Tooltips are an integral part of Swing components. They can be specified by
calling the setToolTipText method as shown below.
○ setToolTipText("Deletes the selected grocery item");
JcolorChooser
● JColorChooser provides a pane of controls designed to allow a user to
manipulate and select a color.
● Constructors:
○ JColorChooser(): Creates a color chooser pane with an initial color of white.
○ JColorChooser(Color initialColor): Creates a color chooser pane with the specified initial color.
Internal Frame
● JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many
features of a frame which includes displaying title, opening, closing, resizing, support for menu
bar, etc.
● Constructors for JInternalFrame
○ JInternalFrame() : creates a new non- closable, non- resizable, non- maximizable JInternalFrame with no title
○ JInternalFrame(String t) :creates a new non- closable, non- resizable, non- maximizable JInternalFrame with a
title specified
○ JInternalFrame(String t, boolean resizable) :creates a new non- closable, non- maximizable JInternalFrame with a
title and resizability specified
○ JInternalFrame(String t, boolean resizable, boolean closable) : creates a new non- maximizable JInternalFrame
with a title, closability and resizability specified
○ JInternalFrame(String t, boolean resizable, boolean closable, boolean maximizable) :creates a new
JInternalFrame with a title, closability, maximizability and resizability specified
○ JInternalFrame(String t, boolean resizable, boolean closable, boolean maximizable : creates a new
JInternalFrame with a title, closability, maximizability, and resizability specified
Table
● The JTable class is a part of Java Swing Package and is generally used to
display or edit two-dimensional data that is having both rows and columns. It
is similar to a spreadsheet. This arranges data in a tabular form.
● Constructors in JTable:
○ JTable(): A table is created with empty cells.
○ JTable(int rows, int cols): Creates a table of size rows * cols.
○ JTable(Object[][] data, Object []Column): A table is created with the specified name where
[]Column defines the column names.
Tree
● The JTree class is used to display the tree structured data or hierarchical
data.
● JTree is a complex component.
● It has a 'root node' at the top most which is a parent for all nodes in the tree.
● Constructors:
○ JTree()
○ JTree(Object[] value)
○ JTree(TreeNode root)