AWT to Swing Java Swing AWT: Abstract Windowing Toolkit Swing: new with Java2 API: GUI Component API import java.awt.* import javax.swing.* Extends AWT Many new improved components Standard dialog boxes, tooltips, … Look-and-feel, skins Event listeners http://java.sun.com/j2se/1.3/docs/api/index.html Using a GUI Component Java: GUI component = class 1. Create it Properties 2. Configure it JButton Methods Add it 4. Listen to it Events Anatomy of an Application GUI GUI JFrame JPanel JFrame JButton 2. 3. 4. containers 5. JPanel panel.add(b); [avoided in java] JButton Events: Listeners Using a GUI Component 2 1. Internal structure Properties: b.text = “press me”; Methods: b.setText(“press me”); 3. Instantiate object: b = new JButton(“press me”); Create it Configure it Add children (if container) Add to parent (if not JFrame) Listen to it order important JLabel JButton JLabel 1 Build from bottom up Create: Frame Panel Components Listeners JLabel Add: (bottom up) Code Listener JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); JButton p.add(b); // add button to panel f.setContentPane(p); // add panel to frame JPanel listeners into components components into panel panel into frame f.show(); press me JFrame Application Code Layout Managers import javax.swing.*; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); Automatically control placement of components in a panel Why? p.add(b); f.setContentPane(p); // add button to panel // add panel to frame f.show(); press me } } Layout Manager Heuristics null none, programmer sets x,y,w,h FlowLayout Layout Manager Heuristics GridLayout BorderLayout n Left to right, Top to bottom w c e CardLayout One at a time GridBagLayout JButton s 2 Coordinate System Component Hierarchy Upside-down Cartesian Each component has its own subwindow (0,0) (width,0) Subwindow = rectangular area within parent component Has own coordinate system Clipping: Can’t paint outside its subwindow Can’t paint over child components? JPanel (0,0) (0,height) (width, height) JButton (0,0) JButton (wb, hb) Ywindow = height - Ycartesian (wp, hp) Combinations Combinations JButton JButton JFrame JButton JButton n JPanel: FlowLayout JPanel: BorderLayout JTextArea c JTextArea Code: null layout Code: FlowLayout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null); // x,y layout p.add(b); f.setContentPane(p); press me 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”); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); press me then me Set layout mgr before adding components 3 Applets Applet Methods JApplet is like a JFrame Already has a panel JApplet Access panel with JApplet.getContentPane( ) contentPane import javax.swing.*; class hello extends JApplet { public void init(){ JButton b = new JButton(“press me”); getContentPane().add(b); } } JButton Called by browser: init( ) start( ) stop( ) destroy( ) paint( ) Application + Applet import javax.swing.*; 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(); f.setContentPane(p); f.show(); } } Command line Browser JFrame JApplet contentPane 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); } } Applet Security or - initialization - resume processing (e.g. animations) - pause - cleanup - redraw stuff (‘expose’ event) No read/write on client machine Cannot execute programs on client machine Communicate only with server “Java applet window” Warning JPanel JButton 4