Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Exploring swing classes

JComboBox:

--> It is used to create a drop down list. One item can be selected

from the given list.

-->Constructor:

a. JComboBox()

-->It is used to create an empty drop down list. We can add items to the list by using void
addItem(Object)

b. JComboBox(Object[]items)

-->It is used to create a drop down list with specified items.

-->setMaximumRowCount(int) is used to control the maximum rows visible at a time. (By default 8 rows
are visible)

-->getSelectedItem() or getSelectedIndex() are used to get the selected item or index.

-->Both ActionEvent and ItemEvent are generated when an item is selected.

-->ItemEvent -- when a new list item is selected

-->ActionEvent --when any item in the list is selected

JList:

-->creates a list of items in which one or more items can be selected.

-->Constructor:

a. JList()

-->is used to create an empty list. Items can be added to the list by using void setListData(Object[]
items)

b. JList(Object[]items)

-->is used to create a list with specified items.


-->void setSelectionMode(int mode) allows to specify whether the list is single selection or multiple
selection.

Mode can be:

a. ListSelectionModel.SINGLE_SELECTION

b. ListSelectionModel.SINGLE_INTERVAL_SELECTION

c. ListSelectionModel.MULTIPLE_INTERVAL_SELECTION (default)

-->getSelectedValue() getSelectedValuesList()

getSelectedIndex() getSelectedIndices()

-->ListSelectionEvent is generated when an item is selected.

--> ListSelectionListener provides the method

void valueChanged(ListSelectionEvent e)

-->javax.swing.event package contains ListSelectionEvent and ListSelectionListener.

-->Scrollbar should be manually added to a JList.

--> void setVisibleRowCount(int) can be used to specify the number of items visible at a time.

Difference between JComboBox and JList

--------------------------------------

JComboBox JList

1. only Single item can be 1. Multiple items can be selected.

selected.

2. scroll bar is added 2. Scroll bar should be added

automatically. manually.

Create a swing GUI with a list box and a text field. When item in the list box is/are selected, display
it/them in the text field.
JToggleButton

-->used to create a toggle button. It is a component like

button however, it has two distinct states:- pressed (On) and released (Off). It can be used as a switch.

-->isSelected() method returns true if the button is selected(pressed) otherwise it returns false(when
released).

-->Both ActionEvent and ItemEvent are generated.

Create a swing GUI with a toggle button. When the toggle button is pressed display "Button is ON" in a
message dialog, when the button is released display "Button is OFF" in a message dialog.

JTable:

--> is used to create a table.

-->Its constructor is:

JTable(Object[][]data,String cols[])

For eg: JTable t1;

JFrame f1;

Object data[][]={{1,"ram",2000},{2,"hari",3000}};

String cols[]={"Sno","Name","Salary"};

t1=new JTable(data,cols);

-->A JTable should be enclosed inside a JScrollPane otherwise it does not show the column headings and
scroll bar.

eg: JScrollPane jp=new JScrollPane(t1);

f1.add(jp);

JTabbedPane
-->used to create a tabbed pane. It is a component in which other components can be placed in a group
forming different tabs.

-->Its constructors are:

JTabbedPane() -->tabs are placed at top

JTabbedPane(int tabDirection) -->tabs can be placed in any direction


(TOP(1),LEFT(2),BOTTOM(3),RIGHT(4))

-->addTab(String,Component) is used to add a tab to the tabbed pane.

JRadioButton and JCheckBox

--> JRadioButton is used to create radio button.

JCheckBox is used to create a check box.

Difference is that we can select only one radio button however we can select multiple checkbox.

For eg: JRadioButton r1,r2;

JCheckBox c1,c2,c3;

r1=new JRadioButton("Male");

r2=new JRadioButton("Female");

f1.add(r1); f1.add(r2);

c1=new JCheckBox("Music");

c2=new JCheckBox("Sports");

c3=new JCheckBox("Travelling");

f1.add(c1);f1.add(c2);f1.add(c3);
We need to group the radio buttons otherwise all radio buttons can be selected. To group the radio
buttons, ButtonGroup class is used.

For eg: ButtonGroup bg=new ButtonGroup();

bg.add(r1);bg.add(r2);

--> isSelected() method returns true if radio button or checkbox is selected otherwise it returns false.

Dialog Box
--> A dialog box is a window similar to frame however it
cannot have menu bar and it has only close button (there
are no minimize and maximize buttons).
--> Dialog box can be of two types:
1. Library/Built-in Dialog Box --> which are already
available in Java. For eg:
Input Dialog --> JOptionPane.showInputDialog()
Message Dialog --> JOptionPane.showMessageDialog()
Confirm Dialog --> JOptionPane.showConfirmDialog()
Color Dialog --> JColorChooser
File Dialog --> JFileChooser
2. User Defined Dialog Box: -- created by
programmer/developer according to their requirements.
JDialog class is used.
Its constructors are:
a. JDialog(JFrame parent)
b. JDialog(JFrame parent,boolean isModal)
c. JDialog(JFrame parent,String title, boolean
isModal)

Both Library/built-in and user defined dialog box can be


Modal or non-modal.
a. Modal Dialog Box: It is a type of dialog box which does
not allow to access other parts of application until it is
closed. We set the value "true" for isModal in the
constructor to make a modal dialog box.
b. Non-Modal Dialog Box:- It is a type of dialog box which
allows to access other parts of application before it is
closed. We set the value "false" for isModal in the
constructor to make a non-modal dialog box.
Two Key Swing Features:

1. Swing Components Are Lightweight

2. Swing Supports a Pluggable Look and Feel

1. Swing Components are Lightweight

--> Swing components are lightweight. This means that they are written entirely in Java and do not
map directly to platform-specific peers.

-->Since swing components are lightweight, they can be transparent, which enables non-rectangular
shapes.

--> Since lightweight components do not translate into native peers, the look and feel of each
component is determined by Swing, not by the underlying operating system. This means that each
component will work in a consistent manner across all platforms.

2. Swing Supports a Pluggable Look and Feel

--> Each Swing component is rendered by Java code rather than by native peers, so the look and feel of a
component is under the control of Swing.

--> It means that it is possible to separate the look and feel of a component from the logic of the
component

-->Separating out the look and feel provides a significant advantage: it becomes possible to change the
way that a component is rendered without affecting any of its other aspects.

-->In other words, it is possible to "plug in" a new look and feel for any given component without
creating any side effects in the code.
The Model-View-Controller Architecture

(Swing MVC connection)

Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its
components. Essentially, MVC breaks GUI components into three elements. (Model View Controller)

Model

--> The model encompasses the state data for each component.There are different models for different
types of components. For example, the model of a scrollbar component might contain information
about the current position of its adjustable “thumb,” its minimum and maximum values.

View

--> The view refers to how you see the component on the screen. For eg. how views can differ,we can
look at an application window on two different GUI platforms. Almost all window frames will have a
titlebar spanning the top of the window. However, the titlebar may have a close box on the left side
(MacOS platform), or it may have the close box on the right side (as in the Windows platform).

Controller

--> The controller is the portion of the user interface that dictates how the component interacts with
events. Events come in many forms — a mouse click, gaining or losing focus, a keyboard event that
triggers a specific menu command.

The controller decides how each component will react to the event.

Note:

--> Swing actually makes use of a simplified variant of the MVC design called the "model-delegate".

-->This design combines the view and the controller object into a single element that draws the
component to the screen and handles GUI events known as the UI delegate.

-->The model is responsible for maintaining information about the component’s state.

-->The UI delegate is responsible for maintaining information about how to draw the component on the
screen. In addition, the UI delegate reacts to various events that propagate through the component.

You might also like