Swings
Swings
G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Textbook
Click to edit Master text styles
Second level
Third level
◦ Fourth level
◦ Fifth level
ComboBox Scrollbar
Text Area
TextField List
Button
Swing gives far more options for everything (buttons with pictures on them, etc.)
Component & Containers VIEW: The way that the component looks when rendered on
Swing Packages the screen
Simple Swing Application
CONTROLLER: The way that the component reacts to the user
Event Handling in Swings
MVC Connection
Component is an independent visual control
Ex: button, checkbox
Component & Containers
Container holds group of components
Swing Packages
For a component to be displayed, it should be within a
Simple Swing Application
container.
Event Handling in Swings
All Swing GUI will have at least 1 container enabling Swings
Simple Swing Applet to define containment hierarchy
Lightweight containers
jfrm.setSize(300, 100);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.add(jlab);
jfrm.setVisible(true);
}
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(jlab);
setVisible(true);
}
MVC Connection
Swing Packages
Add event listeners to each button
Simple Swing Application
If Button1 is clicked, display “Button1 was clicked” in Label
Event Handling in Swings
ComboBox Scrollbar
Text Area
TextField List
Button
add(tbutton);
add(label);
}
}
JTabbedPane Steps that you should follow to use a scroll pane in an applet:
Create a JComponent object.
JScrollPane
Create a JScrollPane object passing to it the object to scroll.
JList
Add the scroll pane to the content pane.
JComboBox
JTable
JList
Generates ListSelectionEvent when user makes or changes a selection or
deselects an item.
JComboBox
Handled by implementing ListSelectionListener interface.
JTable
Specifies one method
void valueChanged(ListSelectionEvent le)
Packaged into javax.swing.event
JList Example
{
setLayout(new FlowLayout());