Java GUI Programming - Computer and Information Science

Java GUI Programming - Computer and Information Science

Department of Computer and Information Science, School of Science, IUPUI GUI Programming using Java - Layout Managers Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected] Dale Roberts Layout Managers Layout managers Provided to arrange GUI components in a container Provide basic layout capabilities Implement the interface LayoutManager Layout manager Description FlowLayout Default for javax.swing.JPanel. Places components sequentially (left to right) in the order they were added. It is also possible to specify the order of the components by using the Container method add, which takes a Component and an integer index position as arguments. BorderLayout Default for JFrames (and other windows). Arranges the components into five areas: NORTH, SOUTH, EAST, WEST and CENTER. GridLayout Arranges the components into rows and columns. 2 Dale Roberts Look-and-Feel Observation 11.17

It is possible to set a Containers layout to null, which indicates that no layout manager should be used. In a Container without a layout manager, the programmer must position and size the components in the given container and take care that, on resize events, all components are repositioned as necessary. A components resize events can be processed by a ComponentListener. 3 Dale Roberts FlowLayout FlowLayout Simplest layout manager Components are placed left to right in the order they are added Components can be left aligned, centered or right aligned 4 Dale Roberts FlowLayout Demo Dale Roberts 1 2 // Fig. 11.39: FlowLayoutFrame.java // Demonstrating FlowLayout alignments. 3 4 5

6 7 8 9 10 import import import import import import Outline java.awt.FlowLayout; java.awt.Container; java.awt.event.ActionListener; java.awt.event.ActionEvent; javax.swing.JFrame; javax.swing.JButton; FlowLayoutFra me .java (1 of 3) public class FlowLayoutFrame extends JFrame 11 { 12 13 private JButton leftJButton; // button to set alignment left private JButton centerJButton; // button to set alignment center 14 15 private JButton rightJButton; // button to set alignment right

private FlowLayout layout; // layout object 16 17 18 19 20 21 private Container container; // container to set layout 22 23 24 25 26 // set up GUI and register button listeners public FlowLayoutFrame() { super( "FlowLayout Demo" ); Create FlowLayout layout = new FlowLayout(); // create FlowLayout container = getContentPane(); // get container to layout setLayout( layout ); // set frame layout Set layout of application Dale Roberts 6 27 28 29 30 31 32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 Outline // set up leftJButton and register listener leftJButton = new JButton( "Left" ); // create Left button add( leftJButton ); // add Left button to frame leftJButton.addActionListener( new ActionListener() // anonymous inner Add JButton; FlowLayout will handle placement class { // process leftJButton event public void actionPerformed( ActionEvent event )

{ layout.setAlignment( FlowLayout.LEFT ); (2 of 3) Set alignment to left // realign attached components layout.layoutContainer( container ); } // end method actionPerformed } // end anonymous inner class ); // end call to addActionListener Adjust layout // set up centerJButton and register listener centerJButton = new JButton( "Center" ); // create Center button add( centerJButton ); // add Center button to frame centerJButton.addActionListener( new ActionListener() // anonymous inner { Add JButton; FlowLayout classwill handle placement // process centerJButton event Set alignment public void actionPerformed( ActionEvent event ) { layout.setAlignment( FlowLayout.CENTER ); Dale Roberts FlowLayoutFra me .java to center

7 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 Outline // realign attached components layout.layoutContainer( container ); } // end method actionPerformed } // end anonymous inner class ); // end call to addActionListener Adjust layout // set up rightJButton and register listener rightJButton = new JButton( "Right" ); // create Right button add( rightJButton ); // add Right button to frame rightJButton.addActionListener( new ActionListener() // anonymous inner {

Add JButton; FlowLayout classwill handle placement // process rightJButton event public void actionPerformed( ActionEvent event ) { layout.setAlignment( FlowLayout.RIGHT ); // realign attached components layout.layoutContainer( container ); 77 } // end method actionPerformed 78 } // end anonymous inner class 79 ); // end call to addActionListener 80 } // end FlowLayoutFrame constructor 81 } // end class FlowLayoutFrame Set alignment to right Adjust layout Dale Roberts FlowLayoutFra me .java (3 of 3) 8 1 // Fig. 11.40: FlowLayoutDemo.java 2 // Testing FlowLayoutFrame.

3 import javax.swing.JFrame; Outline 4 5 public class FlowLayoutDemo 6 { 7 public static void main( String args[] ) 8 { 9 FlowLayoutFrame flowLayoutFrame = new FlowLayoutFrame(); 10 flowLayoutFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 flowLayoutFrame.setSize( 300, 75 ); // set frame size 12 flowLayoutFrame.setVisible( true ); // display frame

13 } // end main 14 } // end class FlowLayoutDemo Dale Roberts FlowLayoutDe mo .java (1 of 2) 9 Outline 10 FlowLayoutDe mo .java (2 of 2) Dale Roberts BorderLayout BorderLayout Arranges components into five regions north, south, east, west and center Implements interface LayoutManager2 Provides horizontal gap spacing and vertical gap spacing 11 Dale Roberts BorderLayout Demo Dale Roberts

1 2 3 4 5 6 Outline // Fig. 11.41: BorderLayoutFrame.java // Demonstrating BorderLayout. import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; 13 7 import javax.swing.JButton; 8 9 public class BorderLayoutFrame extends JFrame implements ActionListener 10 { 11 private JButton buttons[]; // array of buttons to hide portions 12 private final String names[] = { "Hide North", "Hide South", 13 "Hide East", "Hide West", "Hide Center" }; 14 15 16 17 18 19 20 21 22 23 24

25 26 27 28 29 30 private BorderLayout layout; // borderlayout object // set up GUI and event handling public BorderLayoutFrame() { super( "BorderLayout Demo" ); BorderLayout Frame.java (1 of 2) Declare BorderLayout instance variable Create BorderLayout Set layout layout = new BorderLayout( 5, 5 ); // 5 pixel gaps setLayout( layout ); // set frame layout buttons = new JButton[ names.length ]; // set size of array // create JButtons and register listeners for them for ( int count = 0; count < names.length; count++ ) { Register buttons[ count ] = new JButton( names[ count ] ); buttons[ count ].addActionListener( this ); } // end for Dale Roberts event handler 31 32

add( buttons[ 0 ], BorderLayout.NORTH ); // add button to north 33 34 35 36 37 38 39 40 add( buttons[ 1 ], BorderLayout.SOUTH ); // add button to south add( buttons[ 2 ], BorderLayout.EAST ); // add button to east add( buttons[ 3 ], BorderLayout.WEST ); // add button to west add( buttons[ 4 ], BorderLayout.CENTER ); // add button to center } // end BorderLayoutFrame constructor 41 42 43 44 45 46 { Outline 14 BorderLayout Add buttons to application usingFrame.java // handle button events public void actionPerformed( ActionEvent event ) layout manager constants // check event source and layout content pane correspondingly for ( JButton button : buttons )

Make button invisible { if ( event.getSource() == button ) button.setVisible( false ); // hide buttonMake clicked button visible 47 else 48 button.setVisible( true ); // show other buttons 49 } // end for 50 51 layout.layoutContainer( getContentPane() ); // layout content pane 52 } // end method actionPerformed Update layout 53 } // end class BorderLayoutFrame Dale Roberts (2 of 2) 1 // Fig. 11.42: BorderLayoutDemo.java 2 // Testing BorderLayoutFrame. 3 import javax.swing.JFrame; Outline 15

4 5 public class BorderLayoutDemo 6 { 7 public static void main( String args[] ) 8 { 9 BorderLayoutFrame borderLayoutFrame = new BorderLayoutFrame(); 10 borderLayoutFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 borderLayoutFrame.setSize( 300, 200 ); // set frame size 12 borderLayoutFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class BorderLayoutDemo

horizontal gap vertical gap Dale Roberts BorderLayout Demo.java (1 of 2) Outline 16 BorderLayout Demo.java (2 of 2) Dale Roberts 11.17.3 GridLayout GridLayout Divides container into a grid Every component has the same width and height 17 Dale Roberts GridLayout Demo Dale Roberts 1 2 3 4 5 6 7

8 9 Outline // Fig. 11.43: GridLayoutFrame.java // Demonstrating GridLayout. import java.awt.GridLayout; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; 19 GridLayout Frame.java 10 public class GridLayoutFrame extends JFrame implements ActionListener 11 { 12 private JButton buttons[]; // array of buttons 13 private final String names[] = Declare two GridLayout 14 { "one", "two", "three", "four", "five", "six" }; 15 private boolean toggle = true; // toggle between two instance layoutsvariables 16 17 18 19 20 21 22 23

24 25 26 27 28 29 private Container container; // frame container private GridLayout gridLayout1; // first gridlayout private GridLayout gridLayout2; // second gridlayout // no-argument constructor public GridLayoutFrame() { super( "GridLayout Demo" ); gridLayout1 = new GridLayout( 2, gridLayout2 = new GridLayout( 3, container = getContentPane(); // setLayout( gridLayout1 ); // set Create GridLayout 3, 5, 5 ); // 2 by 3; gaps of 5 2 ); // 3 by 2; no gaps get content pane JFrame layout buttons = new JButton[ names.length ]; // create array of JButtons Set layout Dale Roberts (1 of 2) 30 for ( int count = 0; count < names.length; count++ ) 31 {

20 32 buttons[ count ] = new JButton( names[ count ] ); 33 buttons[ count ].addActionListener( this ); // register listener 34 add( buttons[ count ] ); // add button to JFrame } // end for 35 36 Add button to JFrame } // end GridLayoutFrame constructor 37 38 // handle button events by toggling between layouts 39 public void actionPerformed( ActionEvent event ) 40 { 41 42 43 44

Use second layout if ( toggle ) container.setLayout( gridLayout2 ); // set layout to second Use first layout else container.setLayout( gridLayout1 ); // set layout to first 45 46 toggle = !toggle; // set toggle to opposite value 47 container.validate(); // re-layout container 48 Outline } // end method actionPerformed 49 } // end class GridLayoutFrame Update layout Dale Roberts GridLayout Frame.java (2 of 2) 1 // Fig. 11.44: GridLayoutDemo.java

2 // Testing GridLayoutFrame. 3 import javax.swing.JFrame; Outline 21 4 5 public class GridLayoutDemo 6 { 7 public static void main( String args[] ) 8 { 9 GridLayoutFrame gridLayoutFrame = new GridLayoutFrame(); 10 gridLayoutFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11

gridLayoutFrame.setSize( 300, 200 ); // set frame size 12 gridLayoutFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class GridLayoutDemo Dale Roberts GridLayoutDemo .java Using Panels to Manage More Complex Layouts Complex GUIs often require multiple panels to arrange their components properly 22 Dale Roberts 1 // Fig. 11.45: PanelFrame.java 2 // Using a JPanel to help lay out components. 3 4 5 6 import import

import import Outline 23 java.awt.GridLayout; java.awt.BorderLayout; javax.swing.JFrame; javax.swing.JPanel; 7 import javax.swing.JButton; 8 9 public class PanelFrame extends JFrame 10 { 11 private JPanel buttonJPanel; // panel to hold buttons 12 13 14 15 16 17 18 19 20 21 private JButton buttons[]; // array of buttons // no-argument constructor public PanelFrame() { PanelFrame.ja va (1 of 2) Declare a JPanel to hold buttons

Create JPanel super( "Panel Demo" ); buttons = new JButton[ 5 ]; // create buttons array buttonJPanel = new JPanel(); // set up panel buttonJPanel.setLayout( new GridLayout( 1, buttons.length ) ); Set layout Dale Roberts 22 23 24 25 26 27 // create and add buttons for ( int count = 0; count < buttons.length; count++ ) { buttons[ count ] = new JButton( "Button " + ( count + 1 ) ); buttonJPanel.add( buttons[ count ] ); // add button to panel } // end for 28 29 30 add( buttonJPanel, BorderLayout.SOUTH ); // add panel to JFrame } // end PanelFrame constructor Add button to panel 31 } // end class PanelFrame Add panel to application Dale Roberts

Outline 24 PanelFrame.ja va (2 of 2) 1 // Fig. 11.46: PanelDemo.java 2 // Testing PanelFrame. 3 import javax.swing.JFrame; Outline 25 4 5 public class PanelDemo extends JFrame 6 { PanelDemo.java 7 public static void main( String args[] ) 8

{ 9 PanelFrame panelFrame = new PanelFrame(); 10 panelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 panelFrame.setSize( 450, 200 ); // set frame size 12 panelFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class PanelDemo Dale Roberts Acknowledgements Deitel, Java How to Program Dale Roberts

Recently Viewed Presentations

  • Time Management and Study Skills Strategies

    Time Management and Study Skills Strategies

    Study Strategies (continued) Find the best time to study and complete work. As soon as you get home or. Relax first, study later. Be sure to set a specific time and stick to the schedule as much as possible
  • Evidence of Evolution

    Evidence of Evolution

    Nicholas Steno observed a shark tooth fossil was identical to an drawing of an extinct shark and made the jump that the fossil came from that shark. Another example of evidence to support evolution was found by Mary Ann Mantell...
  • Parent-Child Interaction Therapy

    Parent-Child Interaction Therapy

    CDI The Child-Directed Interaction CHILD DIRECTED INTERACTION (PHASE 1 OF PCIT) The CDI Teaching Session One-hour session Parents alone Didactic presentation of skills "Rules" Reasons Examples Modeling/demonstration Role-play with parents CDI The Basic Rule CDI The Don't Rules No commands...
  • Potential Jobs in California in Renewable Generation ...

    Potential Jobs in California in Renewable Generation ...

    The California Workforce Advisory Board proposal was spawned out of this New Green Energy Economy Summit. Sponsors included the Willie Brown, Jr. Institute, the Ella Baker Center for Human Rights, and the Apollo Alliance Subsequent Summits will be held in...
  • HST -1 - Edl

    HST -1 - Edl

    HST -1 Respiratory System Cystic Fibrosis Causes: Genetic disorder Dx by 6 months Symptoms: Mucus becomes thicker Excess salt appears on skin Treament: No cure Intensive pulmonary care Upper Respiratory Infection Cause: virus or bacteria in nose, pharynx, and/or larynx...
  • Georgia and the American Experience

    Georgia and the American Experience

    Words/Terms of Importance For 20th Century in Georgia. Use this PowerPoint to help with reviewing the information that has been presented with the decade projects and also discussions in class.
  • INTRODUCTION to HIGHER EDUCATION

    INTRODUCTION to HIGHER EDUCATION

    STEREOTYPES. Don't be put off by mistaken stereotypes i.e. that Oxbridge is only for super-intelligent, public school types who have loads of money to afford it, speak Latin & compose symphonies in their spare time between filling in applications for...
  • Homecrest Presbyterian Church Worship of the Lords Day

    Homecrest Presbyterian Church Worship of the Lords Day

    God delights in us when we cry out to him, through our confession and our prayers. As we approach God's throne in worship, let us renew our commitment to live honorably and faithfully in God's grace. ... Sharing of Joys...