SWINGS - Continued
Java JRadioButton
• The JRadioButton class is used to create a radio button.
• It is used to choose one option from multiple options.
• It is widely used in exam systems or quiz.
• It should be added in ButtonGroup to select one radio button only.
JRadioButton class declaration
Declaration for javax.swing.JRadioButton class.
public class JRadioButton extends JToggleButton implements Accessible
Commonly used Constructors
Constructor Description
JRadioButton() Creates an unselected radio button with
no text.
JRadioButton(String s) Creates an unselected radio button with
specified text.
JRadioButton(String s, Creates a radio button with the
boolean selected) specified text and selected status.
Commonly used Methods:
Methods Description
void setText(String s) It is used to set specified text on button.
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the
button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the
button.
void It is used to add the action listener to this
addActionListener(ActionListener object.
a)
Java JList
• The object of JList class represents a list of text items.
• The list of text items can be set up so that the user can choose
either one item or multiple items.
• It inherits JComponent class.
Commonly used Constructors
Constructor Description
JList() Creates a JList with an empty, read-only,
model.
JList(ary[] listData) Creates a JList that displays the elements in
the specified array.
JList(ListModel<ary Creates a JList that displays elements from
> dataModel) the specified, non-null, model.
Commonly used Methods
Methods Description
Void It is used to add a listener to the list, to
addListSelectionListener(ListSelectionLi be notified each time a change to the
stener listener) selection occurs.
int getSelectedIndex() It is used to return the smallest selected
cell index.
ListModel getModel() It is used to return the data model that
holds a list of items displayed by the
JList component.
Java JMenuBar, JMenu and JMenuItem
• The JMenuBar class is used to display menubar on the window or
frame. It may have several menus.
• The object of JMenu class is a pull down menu component which is
displayed from the menu bar. It inherits the JMenuItem class.
• The object of JMenuItem class adds a simple labeled menu item. The
items used in a menu must belong to the JMenuItem or any of its
subclass.
Constructors
1. MenuItem() : new=new MenuItem();
2. MenuItem(String itemName): new=new MenuItem(“New”);
3. Menu(String itemName, MenuShortcut shortKey):
new=new MenuItem(“New”,VK_A);
Methods
1. add(MenuItem menuitemObj): file.add(new)
2. setEnabled(Boolean enable): new.setEnabled(true);
3. void setLabel(String Name):
4. String getLabel()
Java JScrollPane
• A JscrollPane is used to make scrollable view of a component.
• When screen size is limited, we use a scroll pane to display a large
component or a component whose size can change dynamically.
Constructors
Constructor Purpose
JScrollPane() It creates a scroll pane. The Component parameter,
when present, sets the scroll pane's client. The two int
JScrollPane(Component) parameters, when present, set the vertical and
horizontal scroll bar policies (respectively).
JScrollPane(int, int)
JScrollPane(Component,
int, int)
Useful Methods
Modifier Method Description
void setColumnHeaderView(Compone It sets the column header for the scroll pane.
nt)
void setRowHeaderView(Component) It sets the row header for the scroll pane.
void setCorner(String, Component) It sets or gets the specified corner. The int parameter
specifies which corner and must be one of the following
Component getCorner(String)
constants defined in ScrollPaneConstants:
UPPER_LEFT_CORNER, UPPER_RIGHT_CORNER,
LOWER_LEFT_CORNER,
LOWER_RIGHT_CORNER,
LOWER_LEADING_CORNER,
LOWER_TRAILING_CORNER,
UPPER_LEADING_CORNER,
UPPER_TRAILING_CORNER.
void setViewportView(Component) Set the scroll pane's client.
Java JTabbedPane
The JTabbedPane class is used to switch between a group of
components by clicking on a tab with a given title or icon. It
inherits Jcomponent class.
Commonly used Constructors
Constructor Description
JTabbedPane() Creates an empty TabbedPane with a default
tab placement of JTabbedPane.Top.
JTabbedPane(int Creates an empty TabbedPane with a
tabPlacement) specified tab placement.
JTabbedPane(int Creates an empty TabbedPane with a
tabPlacement, int specified tab placement and tab layout
tabLayoutPolicy) policy.
Java JComboBox
• The object of Choice class is used to show popup menu of choices.
•
• Choice selected by user is shown on the top of a menu.
• It inherits JComponent class
Commonly used Constructors
Constructor Description
JComboBox() Creates a JComboBox with a default
data model.
JComboBox(Object[] Creates a JComboBox that contains the elements
items) in the specified array.
JComboBox(Vector<?> Creates a JComboBox that contains the elements
items) in the specified Vector.
Commonly used Methods
Methods Description
void addItem(Object anObject) It is used to add an item to the item list.
void removeItem(Object anObject) It is used to delete an item to the item list.
void removeAllItems() It is used to remove all the items from the
list.
void setEditable(boolean b) It is used to determine whether the
JComboBox is editable.
void addActionListener(ActionListener It is used to add the ActionListener.
a)
void addItemListener(ItemListener i) It is used to add the ItemListener.
Java JTable
The JTable class is used to display data in tabular form. It is
composed of rows and columns.
Constructors
Constructor Description
JTable() Creates a table with empty cells.
JTable(Object[][] rows, Creates a table with the specified
Object[] columns) data.
Layout Manager
• The LayoutManagers are used to arrange components in a
particular manner.
• The Java LayoutManagers facilitates us to control the
positioning and size of the components in GUI forms.
Some of the mainly used layouts are as follows:
1) BorderLayout
2) FlowLayout
FlowLayout
• The FlowLayout arranges the components in a directional flow,
either from left to right or from right to left.
• Normally all components are set to one row, according to the
order of different components.
• If all components can not be fit into one row, it will start a new
row and fit the rest in.
BorderLayout
• A BorderLayout lays out a container, arranging its components
to fit into five regions: NORTH, SOUTH, EAST, WEST and
CENTER.
• For each region, it may contain no more than one component.
• When adding different components, you need to specify the
orientation of it to be the one of the five regions.