Professional Documents
Culture Documents
Exploring Swing Classes
Exploring Swing Classes
JComboBox:
--> It is used to create a drop down list. One item can be selected
-->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)
-->setMaximumRowCount(int) is used to control the maximum rows visible at a time. (By default 8 rows
are visible)
JList:
-->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)
a. ListSelectionModel.SINGLE_SELECTION
b. ListSelectionModel.SINGLE_INTERVAL_SELECTION
c. ListSelectionModel.MULTIPLE_INTERVAL_SELECTION (default)
-->getSelectedValue() getSelectedValuesList()
getSelectedIndex() getSelectedIndices()
void valueChanged(ListSelectionEvent e)
--> void setVisibleRowCount(int) can be used to specify the number of items visible at a time.
--------------------------------------
JComboBox JList
selected.
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
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).
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:
JTable(Object[][]data,String cols[])
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.
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.
Difference is that we can select only one radio button however we can select multiple checkbox.
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.
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)
--> 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.
--> 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 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.