You are on page 1of 4

AWT to Swing

„ AWT: Abstract Windowing Toolkit


Java Swing „
„ import java.awt.*
Swing: new with Java2
„ import javax.swing.*
„ Extends AWT
„ Many new improved components
„ Standard dialog boxes, tooltips, …
„ Look-and-feel, skins
„ Event listeners
„ API:
„ http://java.sun.com/j2se/1.3/docs/api/index.html

GUI Component API Using a GUI Component


„ Java: GUI component = class 1. Create it
„ Instantiate object: b = new JButton(“press me”);

„ Properties 2. Configure it
„ Properties: b.text = “press me”; [avoided in java]
„ JButton „ Methods: b.setText(“press me”);
„ Methods 3. Add it
„
„ panel.add(b);
Events
JButton
„ 4. Listen to it
„ „ Events: Listeners

Anatomy of an Application GUI Using a GUI Component 2


JFrame GUI 1. Create it
Internal structure
JPanel 2. Configure it
order
JFrame 3. Add children (if container) important
JButton containers 4. Add to parent (if not JFrame)
JPanel
5. Listen to it

JLabel

JButton JLabel

1
Build from bottom up Code
Listener
„ Create: JFrame f = new JFrame(“title”);
JPanel p = new JPanel( );
„ Frame JLabel JButton JButton b = new JButton(“press me”);
„ Panel
„ Components p.add(b); // add button to panel
„ Listeners f.setContentPane(p); // add panel to frame
Add: (bottom up)
JPanel
„
„ listeners into components f.show();

„ components into panel press me


„ panel into frame
JFrame

Application Code Layout Managers


import javax.swing.*;
„ Automatically control placement of
components in a panel
class hello {
public static void main(String[] args){
JFrame f = new JFrame(“title”);
JPanel p = new JPanel(); „ Why?
JButton b = new JButton(“press me”);
„

p.add(b); // add button to panel


f.setContentPane(p); // add panel to frame

f.show(); press me
}
}

Layout Manager Heuristics Layout Manager Heuristics

null FlowLayout GridLayout


BorderLayout CardLayout GridBagLayout
n
none,
Left to right,
programmer
Top to bottom w e JButton
sets x,y,w,h c One at a time

2
Coordinate System Component Hierarchy
„ Each component has its own subwindow
„ Upside-down Cartesian „ Subwindow = rectangular area within parent component
„ Has own coordinate system
(0,0) (width,0) „ Clipping:
„ Can’t paint outside its subwindow
„ Can’t paint over child components?
(0,0) JPanel

(0,0) JButton
(0,height) (width, height) JButton
(wb, hb)
„ Ywindow = height - Ycartesian
(wp, hp)

Combinations Combinations
JButton JButton
JButton JButton JFrame

n
JPanel: FlowLayout
JPanel: BorderLayout

JTextArea
c
JTextArea

Code: null layout Code: FlowLayout


JFrame f = new JFrame(“title”); JFrame f = new JFrame(“title”);
JPanel p = new JPanel( ); JPanel p = new JPanel( );
JButton b = new JButton(“press me”); FlowLayout L = new FlowLayout( );
JButton b1 = new JButton(“press me”);
b.setBounds(new Rectangle(10,10, 100,50)); JButton b2 = new JButton(“then me”);
p.setLayout(null); // x,y layout
p.add(b); p.setLayout(L);
p.add(b1);
f.setContentPane(p);
p.add(b2);
press me then me
press me f.setContentPane(p);

Set layout mgr before adding components

3
Applets Applet Methods
„ JApplet is like a JFrame „ Called by browser:
JApplet
„ Already has a panel
„ Access panel with JApplet.getContentPane( )
„ init( ) - initialization
import javax.swing.*; contentPane „ start( ) - resume processing (e.g. animations)
„ stop( ) - pause
class hello extends JApplet {
public void init(){ „ destroy( ) - cleanup
JButton b = new JButton(“press me”);
JButton „ paint( ) - redraw stuff (‘expose’ event)
getContentPane().add(b);
}
}

Application + Applet
Command line Browser
Applet Security
import javax.swing.*;

No read/write on client machine


class helloApp {
public static void main(String[] args){
// create Frame and put my mainPanel in it
„

Cannot execute programs on client


JFrame f = new JFrame(“title”);
mainPanel p = new mainPanel();
f.setContentPane(p); JFrame or JApplet „

machine
f.show();
}
}
contentPane
class helloApplet extends JApplet {
public void init(){
„ Communicate only with server
“Java applet window” Warning
// put my mainPanel in the Applet
mainPanel p = new mainPanel();
getContentPane().add(p); „
}
}

// my main GUI is in here:


JPanel
class mainPanel extends JPanel {
mainPanel(){
setLayout(new FlowLayout());
JButton b = new JButton(“press me”);
add(b); JButton
}
}

You might also like