Java Swing
Chris North
cs3724: HCI
AWT to Swing
AWT: Abstract Windowing Toolkit
import [Link].*
Swing: new with Java2
import [Link].*
Extends AWT
Tons o new improved components
Standard dialog boxes, tooltips,
Look-and-feel, skins
Event listeners
API:
[Link]
Swing Set Demo
J2sdk/demo/jfc/SwingSet2
Many predefined
GUI components
GUI Component API
Java: GUI component = class
Properties
Methods
Events
JButton
Using a GUI Component
1. Create it
Instantiate object: b = new JButton(press me);
2. Configure it
Properties: [Link] = press me;
Methods:
[Link](press me);
[avoided in java]
3. Add it
[Link](b);
4. Listen to it
Events: Listeners
JButton
Anatomy of an Application GUI
GUI
Internal structure
JFrame
JFrame
JPanel
containers
JPanel
JButton
JButton
JLabel
JLabel
Using a GUI Component 2
1.
2.
3.
4.
5.
Create it
Configure it
Add children (if container)
Add to parent (if not JFrame)
Listen to it
order
important
Build from bottom up
Create:
Frame
Panel
Components
Listeners
Add: (bottom up)
Listener
JLabel
JButton
JPanel
listeners into components
components into panel
panel into frame
JFrame
Code
JFrame f = new JFrame(title);
JPanel p = new JPanel( );
JButton b = new JButton(press me);
[Link](b);
[Link](p);
// add button to panel
// add panel to frame
[Link]();
press me
Application Code
import [Link].*;
class hello {
public static void main(String[] args){
JFrame f = new JFrame(title);
JPanel p = new JPanel();
JButton b = new JButton(press me);
[Link](b);
[Link](p);
[Link]();
}
}
// add button to panel
// add panel to frame
press me
Layout Managers
Automatically control placement of components
in a panel
Why?
Layout Manager Heuristics
null
FlowLayout
none,
programmer
sets x,y,w,h
Left to right,
Top to bottom
BorderLayout
n
w
c
s
CardLayout
One at a time
GridLayout
GridBagLayout
JButton
Combinations
JButton JButton
JTextArea
Combinations
JButton
JButton
JFrame
n
JPanel: FlowLayout
JPanel: BorderLayout
c
JTextArea
Code: null layout
JFrame f = new JFrame(title);
JPanel p = new JPanel( );
JButton b = new JButton(press me);
[Link](new Rectangle(10,10, 100,50));
[Link](null);
// x,y layout
[Link](b);
[Link](p);
press me
Code: FlowLayout
JFrame f =
JPanel p =
FlowLayout
JButton b1
JButton b2
new JFrame(title);
new JPanel( );
L = new FlowLayout( );
= new JButton(press me);
= new JButton(then me);
[Link](L);
[Link](b1);
[Link](b2);
[Link](p);
Set layout mgr before adding components
press me then me
Applets
JApplet is like a JFrame
Already has a panel
Access panel with [Link]( )
JApplet
contentPane
import [Link].*;
JButton
class hello extends JApplet {
public void init(){
JButton b = new JButton(press me);
getContentPane().add(b);
}
}
Applet Methods
Called by browser:
init( ) - initialization
start( ) - resume processing (e.g. animations)
stop( ) - pause
destroy( )
- cleanup
paint( ) - redraw stuff (expose event)
Application + Applet
import [Link].*;
class helloApp {
public static void main(String[] args){
// create Frame and put my mainPanel in it
JFrame f = new JFrame(title);
mainPanel p = new mainPanel();
[Link](p);
[Link]();
}
}
class helloApplet extends JApplet {
public void init(){
// put my mainPanel in the Applet
mainPanel p = new mainPanel();
getContentPane().add(p);
}
}
// my main GUI is in here:
class mainPanel extends JPanel {
mainPanel(){
setLayout(new FlowLayout());
JButton b = new JButton(press me);
add(b);
}
}
Command line
Browser
JFrame
JApplet
or
contentPane
JPanel
JButton
Applet Security
No read/write on client machine
Cant execute programs on client machine
Communicate only with server
Java applet window Warning
In JBuilder