February 24th, 2008
Chapter 12 Graphical User Interface Components: Part 1 703 After JPanel buttonPanel is created at line 27, lines 28 29 set button- Panel s layout to a GridLayout of one row and five columns (there are five JButtons in array buttons). The five JButtons in array buttons are added to the JPanelin the loop with line 35. Notice that the buttons are added directly to the JPanel class JPanel does not have a content pane like an applet or a JFrame. Line 38 uses the content pane s default BorderLayout to add buttonPanel to the SOUTH region. Note that the SOUTH region is as tall as the buttons on buttonPanel. A JPanel is sized to the components it contains. As more components are added, the JPanel grows (according to the restrictions of its layout manager) to accommodate the components. Resize the window to see how the layout manager affects the size of the JButtons. 12.16 (Optional Case Study) Thinking About Objects: Use Cases The previous eight Thinking About Objects sections have concentrated on the elevator- simulation model. We have identified and honed the structure and behavior of our system. In this section, we model the interaction between the user and our elevator simulation through the UML use-case diagram, which describes the sets of scenarios that occur between the user and the system. Use-Case Diagrams When developers begin a project, they rarely start with as detailed a problem statement as the one we provided in Section 2.9. This document and others are the result of the object- oriented analysis (OOA) phase. In this phase you meet with the people who want you to build a system and with the people who will eventually use that system. You use the information gained in these meetings to compile a list of system requirements. These requirements guide you and your fellow developers as you design the system. In our case study, the problem statement described the requirements of our elevator simulation in sufficient detail that you did not need to go through an analysis phase. The analysis phase is enormously important you should consult the references we provide in Section 2.9 to learn more about object-oriented analysis. The UML provides the use-case diagram to facilitate requirements gathering. This diagram models the interactions between the system s external clients and the use cases of the system. Each use case represents a different capability that the system provides to clients. For example, automated teller machines typically have several use cases, including Deposit Money, Withdraw Money and Transfer Funds. In larger systems, use-case diagrams are indispensable tools that help system designers remain focused on satisfying the users needs. The goal of the use-case diagram is to show the kinds of interactions users have with a system without providing the details of those interactions: those details are, of course, provided in other UML diagrams. Figure 12.28 shows the use-case diagram for our elevator simulation. The stick figure represents an actor, which, in turn, represents a set of roles that an external entity such as a person or another system can play. Consider again our automated teller machine example. The actor is a BankCustomer who can deposit, withdraw and transfer funds from the ATM. In this sense, BankCustomeris more like a class rather than an object it is not an actual person, but rather describes the roles that a real person when playing the part of a BankCustomer can perform while interacting with the ATM (deposit, with Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 24th, 2008
702 Graphical User Interface Components: Part 1 Chapter 12 13 private JButton buttons[]; 14 15 // set up GUI 16 public PanelDemo() 17 { 18 super( “Panel Demo” ); 19 20 // get content pane 21 Container container = getContentPane(); 22 23 // create buttons array 24 buttons = new JButton[ 5 ]; 25 26 // set up panel and set its layout 27 buttonPanel = new JPanel(); 28 buttonPanel.setLayout( 29 new GridLayout( 1, buttons.length ) ); 30 31 // create and add buttons 32 for ( int count = 0; count < buttons.length; count++ ) { 33 buttons[ count ] = 34 new JButton( “Button ” + ( count + 1 ) ); 35 buttonPanel.add( buttons[ count ] ); 36 } 37 38 container.add( buttonPanel, BorderLayout.SOUTH ); 39 40 setSize( 425, 150 ); 41 setVisible( true ); 42 } 43 44 // execute application 45 public static void main( String args[] ) 46 { 47 PanelDemo application = new PanelDemo(); 48 49 application.setDefaultCloseOperation( 50 JFrame.EXIT_ON_CLOSE ); 51 } 52 53 } // end class PanelDemo Fig. 12.27 A JPanel with five JButtons in a GridLayoutattached to the SOUTHregion of a BorderLayout(part 2 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 23rd, 2008
Chapter 12 Graphical User Interface Components: Part 1 701 Fig. 12.26 Program that demonstrates components in GridLayout. Lines 27 28 in the constructor define two GridLayoutobjects. The GridLayout constructor used at line 27 specifies a GridLayoutwith 2rows, 3columns, 5 pixels of horizontal-gap space between Components in the grid and 5pixels of vertical-gap space between Components in the grid. The GridLayoutconstructor used at line 28 specifies a GridLayoutwith 3rows, 2 columns and no gap space. The JButtonobjects in this example initially are arranged using grid1(set for the content pane at line 32 with method setLayout). The first component is added to the first column of the first row. The next component is added to the second column of the first row, and so on. When a JButton is pressed, method actionPerformed (lines 48 57) is called. Every call to actionPerformedtoggles the layout between grid2and grid1. Line 56 illustrates another way to relayout a container for which the layout has changed. Container method validate recomputes the container s layout based on the current layout manager for the Containerand the current set of displayed GUI components. 12.15 Panels Complex GUIs (like Fig. 12.1) require that each component be placed in an exact location. They often consist of multiple panels with each panel s components arranged in a specific layout. Panels are created with class JPanel a subclass of JComponent. Class JComponent inherits from class java.awt.Container, so every JPanel is a Container. Thus JPanels may have components, including other panels, added to them. The program of Fig. 12.27 demonstrates how a JPanelcan be used to create a more complex layout for Components. 1 // Fig. 12.27: PanelDemo.java 2 // Using a JPanel to help lay out components. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class PanelDemo extends JFrame { 12 private JPanel buttonPanel; Fig. 12.27 A JPanel with five JButtons in a GridLayoutattached to the SOUTHregion of a BorderLayout(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 22nd, 2008
700 Graphical User Interface Components: Part 1 Chapter 12 18 private Container container; 19 private GridLayout grid1, grid2; 20 21 // set up GUI 22 public GridLayoutDemo() 23 { 24 super( “GridLayout Demo” ); 25 26 // set up layouts 27 grid1 = new GridLayout( 2, 3, 5, 5 ); 28 grid2 = new GridLayout( 3, 2 ); 29 30 // get content pane and set its layout 31 container = getContentPane(); 32 container.setLayout( grid1 ); 33 34 // create and add buttons 35 buttons = new JButton[ names.length ]; 36 37 for ( int count = 0; count < names.length; count++ ) { 38 buttons[ count ] = new JButton( names[ count ] ); 39 buttons[ count ].addActionListener( this ); 40 container.add( buttons[ count ] ); 41 } 42 43 setSize( 300, 150 ); 44 setVisible( true ); 45 } 46 47 // handle button events by toggling between layouts 48 public void actionPerformed( ActionEvent event ) 49 { 50 if ( toggle ) 51 container.setLayout( grid2 ); 52 else 53 container.setLayout( grid1 ); 54 55 toggle = !toggle; // set toggle to opposite value 56 container.validate(); 57 } 58 59 // execute application 60 public static void main( String args[] ) 61 { 62 GridLayoutDemo application = new GridLayoutDemo(); 63 64 application.setDefaultCloseOperation( 65 JFrame.EXIT_ON_CLOSE ); 66 } 67 68 } // end class GridLayoutDemo Fig. 12.26 Program that demonstrates components in GridLayout. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 22nd, 2008
Chapter 12 Graphical User Interface Components: Part 1 699 Look-and-Feel Observation 12.11 If no region is specified when adding a Component to a BorderLayout, it is assumed that the Componentshould be added to region BorderLayout.CENTER. Common Programming Error 12.6 Adding more than one component to a particular region in a BorderLayout results in only the last component added being displayed. There is no error message to indicate this problem. When the user clicks on a particular JButton in the layout, method actionPerformed(lines 50 61) executes. The for loop at lines 52 57 uses an if/elsestructure to hide the particular JButtonthat generated the event. Method setVisible(inherited into JButton from class Component) is called with a false argument to hide the JButton. If the current JButton in the array is not the one that generated the event, method setVisibleis called with a trueargument to ensure that the JButtonis displayed on the screen. Line 60 uses LayoutManager method layoutContainer to recalculate the layout of the content pane. Notice in the screen captures of Fig. 12.25 that certain regions in the BorderLayout change shape as JButtons are hidden and displayed in other regions. Try resizing the application window to see how the various regions resize based on the width and height of the window. 12.14.3 GridLayout The GridLayoutlayout manager divides the container into a grid so that components can be placed in rows and columns. Class GridLayoutinherits directly from class Object and implements interface LayoutManager. Every Componentin a GridLayouthas the same width and height. Components are added to a GridLayout starting at the top- left cell of the grid and proceeding left-to-right until the row is full. Then the process continues left-to-right on the next row of the grid, etc. Figure 12.26 demonstrates the Grid- Layoutlayout manager using six JButtons. 1 // Fig. 12.26: GridLayoutDemo.java 2 // Demonstrating GridLayout. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class GridLayoutDemo extends JFrame 12 implements ActionListener { 13 14 private JButton buttons[]; 15 private String names[] = 16 { “one”, “two”, “three”, “four”, “five”, “six” }; 17 private boolean toggle = true; Fig. 12.26 Program that demonstrates components in GridLayout. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 21st, 2008
698 Graphical User Interface Components: Part 1 Chapter 12 67 68 application.setDefaultCloseOperation( 69 JFrame.EXIT_ON_CLOSE ); 70 } 71 72 } // end class BorderLayoutDemo Fig. 12.25 Demonstrating components in BorderLayout(part 3 of 3). Line 24 in the constructor defines a BorderLayout. The arguments specify the number of pixels between components that are arranged horizontally (horizontal gap space) and the number of pixels between components that are arranged vertically (vertical gap space), respectively. The default BorderLayoutconstructor supplies 0 pixels of gap space horizontally and vertically. Line 28 uses method setLayout to set the content pane s layout to layout. Adding Components to a BorderLayout requires a different add method from class Container, which takes two arguments the Component to add and the region in which the Component will be placed. For example, line 39 specifies that the buttons[0] should appear in the NORTH position. The components can be added in any order, but only one component can be added to each region. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 20th, 2008
Chapter 12 Graphical User Interface Components: Part 1 697 14 private JButton buttons[]; 15 private String names[] = { “Hide North”, “Hide South”, 16 “Hide East”, “Hide West”, “Hide Center” }; 17 private BorderLayout layout; 18 19 // set up GUI and event handling 20 public BorderLayoutDemo() 21 { 22 super( “BorderLayout Demo” ); 23 24 layout = new BorderLayout( 5, 5 ); 25 26 // get content pane and set its layout 27 Container container = getContentPane(); 28 container.setLayout( layout ); 29 30 // instantiate button objects 31 buttons = new JButton[ names.length ]; 32 33 for ( int count = 0; count < names.length; count++ ) { 34 buttons[ count ] = new JButton( names[ count ] ); 35 buttons[ count ].addActionListener( this ); 36 } 37 38 // place buttons in BorderLayout; order not important 39 container.add( buttons[ 0 ], BorderLayout.NORTH ); 40 container.add( buttons[ 1 ], BorderLayout.SOUTH ); 41 container.add( buttons[ 2 ], BorderLayout.EAST ); 42 container.add( buttons[ 3 ], BorderLayout.WEST ); 43 container.add( buttons[ 4 ], BorderLayout.CENTER ); 44 45 setSize( 300, 200 ); 46 setVisible( true ); 47 } 48 49 // handle button events 50 public void actionPerformed( ActionEvent event ) 51 { 52 for ( int count = 0; count < buttons.length; count++ ) 53 54 if ( event.getSource() == buttons[ count ] ) 55 buttons[ count ].setVisible( false ); 56 else 57 buttons[ count ].setVisible( true ); 58 59 // re-layout the content pane 60 layout.layoutContainer( getContentPane() ); 61 } 62 63 // execute application 64 public static void main( String args[] ) 65 { 66 BorderLayoutDemo application = new BorderLayoutDemo(); Fig. 12.25 Demonstrating components in BorderLayout(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 19th, 2008
696 Graphical User Interface Components: Part 1 Chapter 12 Each button s actionPerformed event handler executes two statements. For example, line 38 in method actionPerformed for button left uses FlowLayout method setAlignmentto change the alignment for the FlowLayoutto a left-aligned (FlowLayout.LEFT) FlowLayout. Line 41 uses LayoutManager interface method layoutContainerto specify that the content pane should be rearranged based on the adjusted layout. According to which button was clicked, the actionPerformed method for each button sets the FlowLayout s alignment to FlowLayout.LEFT, Flow- Layout.CENTERor FlowLayout.RIGHT. 12.14.2 BorderLayout The BorderLayoutlayout manager (the default layout manager for the content pane) arranges components into five regions: NORTH, SOUTH, EAST, WESTand CENTER(North corresponds to the top of the container). Class BorderLayout inherits from Object and implements interface LayoutManager2 (a subinterface of LayoutManager that adds several methods for enhanced layout processing). Up to five components can be added directly to a BorderLayout one for each region. The component placed in each region can be a container to which other components are attached. The components placed in the NORTHand SOUTHregions extend horizontally to the sides of the container and are as tall as the components placed in those regions. The EASTand WESTregions expand vertically between the NORTHand SOUTHregions and are as wide as the components placed in those regions. The component placed in the CENTER region expands to take all remaining space in the layout (this is the reason the JTextArea in Fig. 12.22 occupies the entire window). If all five regions are occupied, the entire container s space is covered by GUI components. If the NORTHor SOUTHregion is not occupied, the GUI components in the EAST, CENTER and WEST regions expand vertically to fill the remaining space. If the EASTor WEST region is not occupied, the GUI component in the CENTER region expands horizontally to fill the remaining space. If the CENTER region is not occupied, the area is left empty the other GUI components do not expand to fill the remaining space. The application of Fig. 12.25 demonstrates the BorderLayout layout manager by using five JButtons. 1 // Fig. 12.25: BorderLayoutDemo.java 2 // Demonstrating BorderLayout. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class BorderLayoutDemo extends JFrame 12 implements ActionListener { 13 Fig. 12.25 Demonstrating components in BorderLayout(part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 19th, 2008
Chapter 12 Graphical User Interface Components: Part 1 695 79 // process rightButton event 80 public void actionPerformed( ActionEvent event ) 81 { 82 layout.setAlignment( FlowLayout.RIGHT ); 83 84 // re-align attached components 85 layout.layoutContainer( container ); 86 } 87 } 88 ); 89 90 container.add( rightButton ); 91 92 setSize( 300, 75 ); 93 setVisible( true ); 94 } 95 96 // execute application 97 public static void main( String args[] ) 98 { 99 FlowLayoutDemo application = new FlowLayoutDemo(); 100 101 application.setDefaultCloseOperation( 102 JFrame.EXIT_ON_CLOSE ); 103 } 104 105 } // end class FlowLayoutDemo Fig. 12.24 Program that demonstrates components in FlowLayout(part 3 of 3). As seen previously, a container s layout is set with method setLayout of class Container. Line 25 sets the content pane s layout manager to the FlowLayoutdefined at line 21. Normally, the layout is set before any GUI components are added to a container. Look-and-Feel Observation 12.10 Each container can have only one layout manager at a time. (Separate containers in the same program can have different layout managers.) Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »
February 18th, 2008
694 Graphical User Interface Components: Part 1 Chapter 12 27 // set up leftButton and register listener 28 leftButton = new JButton( “Left” ); 29 30 leftButton.addActionListener( 31 32 // anonymous inner class 33 new ActionListener() { 34 35 // process leftButton event 36 public void actionPerformed( ActionEvent event ) 37 { 38 layout.setAlignment( FlowLayout.LEFT ); 39 40 // re-align attached components 41 layout.layoutContainer( container ); 42 } 43 44 } // end anonymous inner class 45 46 ); // end call to addActionListener 47 48 container.add( leftButton ); 49 50 // set up centerButton and register listener 51 centerButton = new JButton( “Center” ); 52 53 centerButton.addActionListener( 54 55 // anonymous inner class 56 new ActionListener() { 57 58 // process centerButton event 59 public void actionPerformed( ActionEvent event ) 60 { 61 layout.setAlignment( FlowLayout.CENTER ); 62 63 // re-align attached components 64 layout.layoutContainer( container ); 65 } 66 } 67 ); 68 69 container.add( centerButton ); 70 71 // set up rightButton and register listener 72 rightButton = new JButton( “Right” ); 73 74 rightButton.addActionListener( 75 76 // anonymous inner class 77 new ActionListener() { 78 Fig. 12.24 Program that demonstrates components in FlowLayout(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Posted in J2EE | No Comments »