// Fig. 13.26: GridLayoutDemo.java // Demonstrating GridLayout. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GridLayoutDemo3 extends JFrame implements ActionListener { private MyButton buttons[]; private final String names[] = { "one", "two", "three", "four", "five", "six" }; private boolean toggle = true; private Container container; private GridLayout grid1, grid2; // set up GUI public GridLayoutDemo3() { super( "GridLayout Demo" ); // set up layouts grid1 = new GridLayout( 2, 3, 5, 5 ); grid2 = new GridLayout( 3, 2 ); // get content pane and set its layout container = getContentPane(); container.setLayout( grid1 ); // create and add buttons buttons = new MyButton[ names.length ]; for ( int count = 0; count < names.length; count++ ) { buttons[ count ] = new MyButton( names[ count ] , count ); buttons[ count ].addActionListener( this ); buttons[ count ].addActionListener( new ButtonHandler (count) ); container.add( buttons[ count ] ); } setSize( 300, 150 ); setVisible( true ); } // end constructor GridLayoutDemo // handle button events by toggling between layouts public void actionPerformed( ActionEvent event ) { if ( toggle ) container.setLayout( grid2 ); else container.setLayout( grid1 ); toggle = !toggle; // set toggle to opposite value container.validate(); } // inner class for button event handling private class ButtonHandler implements ActionListener { private int handlerNumber; public ButtonHandler (int val) { super(); handlerNumber = val; } // handle button event public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( null, "You pressed: " + event.getActionCommand() +" from hander " + handlerNumber ); // user pressed Enter in JTextField textField1 MyButton buttonUsed = (MyButton) event.getSource(); JOptionPane.showMessageDialog( null, "Message 2: You pressed: " + buttonUsed.getText() + " from hander " + buttonUsed.getNumber() ); for ( int count = 0; count < names.length; count++ ) { if (buttonUsed == buttons [count] ) JOptionPane.showMessageDialog( null, "Message 3: You pressed: " + buttonUsed.getText() + " from hander " + count ); } } } // end private inner class ButtonHandler public static void main( String args[] ) { GridLayoutDemo3 application = new GridLayoutDemo3(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class GridLayoutDemo class MyButton extends JButton { private int buttonNumber; public MyButton (int val) { super (); buttonNumber= val; } public MyButton (String text, int val) { super (text); buttonNumber= val; } public void setNumber (int val) { buttonNumber= val; } public int getNumber () { return buttonNumber; } } /************************************************************************** * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and * * Prentice Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/