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

  • September 2018 doc.: 15-18-0480-00-0000 Project: IEEE P802.15 Working

    September 2018 doc.: 15-18-0480-00-0000 Project: IEEE P802.15 Working

    Al Petrick, Jones-Petrick and Associates. Slide . NOTE: Update all red fields replacing with your information; they are required. This is a manual update in appropriate
  • OUR LADY OF LOURDES CATHOLIC SCHOOL GUIDANCE DEPARTMENT

    OUR LADY OF LOURDES CATHOLIC SCHOOL GUIDANCE DEPARTMENT

    The school body participates as a whole to celebrate Red Ribbon Week-Drug Free Awareness, Our Lady of Lourdes Kindness month, Unity Day, Our Lady of Lourdes Peace Day, as well as working with the community to share the cards, letters,...
  • Light: Refraction and Absorption - studentorg

    Light: Refraction and Absorption - studentorg

    Light: Refraction and Absorption Vanderbilt Student Volunteers for Science Spring, 2008 Training Presentation Introduction Pass out the Instruction and Observations sheets Write the following words on the board: Refraction Visible light Electromagnetic spectrum Laser Absorption Explain that light is a...
  • Mathematics in Context (Core Maths) Launch event Grahame

    Mathematics in Context (Core Maths) Launch event Grahame

    Department for Education, Core Maths Technical Guidance, July 2014 Mathematics in Context - Launch events * Core Maths qualifications Intended for learners who achieve A* to C in GCSE Mathematics, who choose not to study AS or A Level Mathematics...
  • What you can extract from this required reading

    What you can extract from this required reading

    Do you know the fundamental classes of DNA binding domain described here, and the modes of DNA recognition (helix-turn-helix [including homeodomain], zinc finger, basic-leucine zipper, basic-helix-loop-helix)? Are you familiar with the general techniques, described here, used to study transcription factors?
  • Illinois State Vertical Jumps Clinic - ITCCCA

    Illinois State Vertical Jumps Clinic - ITCCCA

    Improper rotation. Jump leg lags behind. Foot always drags bar off. High Jump Training. Drills: Approach. Curve running - circle and on track. Figure 8's. Mid-mark check. Takeoff / Preparation. 3-step jumps / 5-step jumps. Incline box jumps. Scissor jumps...
  • Case Notes for Wagner Peyser - Home - FloridaJobs.org

    Case Notes for Wagner Peyser - Home - FloridaJobs.org

    Case Notes for every service. When providing services to job seekers in Wagner Peyser, one must first take the service, second, give a concise and factual account of the service provided.
  • Story &amp; Production elements - bellarine media

    Story & Production elements - bellarine media

    Story & Production elements. Year 9 Media Studies. Story elements. Multiple storylines. Includes the plot (main storyline) and subplots (other stories that add to the story / tv show). Cause and effect. One event starts the story which causes something...