// Author: Graham Freeman. July 2008 // Copyright (c) Graham Freeman. g-freeman@adfa.edu.au // The author accepts no responsibility for any problems caused by use of // this software. It is supplied free of charge, and usage is unrestricted, // provided this copyright notice remains intact, regardless of how the // software may be modified by other people. import javax.swing.*; import java.awt.*; import java.awt.event.*; /** This class demonstrates some of the features of GridBagLayout.java and how it handles a JScrollPane which gains or loses scrollbars. */ public class TestGBLayout extends JFrame implements ActionListener { JButton reject, accept, quit; JTextArea addr; JScrollPane jsp; public static void main( String[] arg ) { new TestGBLayout().setVisible(true); } public TestGBLayout() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cpane = getContentPane(); GridBagLayout gbag = new GridBagLayout(); cpane.setLayout( gbag ); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.ipadx = 7; gbc.ipady = 7; gbc.insets = new Insets(3,3,3,3); GridBagConstraints gbclast = (GridBagConstraints) gbc.clone(); gbclast.gridwidth = gbc.REMAINDER; GridBagConstraints gbceast = (GridBagConstraints) gbc.clone(); gbceast.anchor = gbc.EAST; gbceast.fill = gbc.NONE; JLabel lab1 = new JLabel("Name: ") ; gbag.setConstraints( lab1, gbc ); cpane.add( lab1 ); JLabel lab2 = new JLabel("Address: "); JTextField name = new JTextField(" ",50); gbag.setConstraints( name, gbclast ); cpane.add( name ); gbag.setConstraints( lab2, gbc ); cpane.add( lab2 ); addr = new JTextArea(" ",3,50 ); jsp = new JScrollPane( addr, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); gbag.setConstraints( jsp, gbclast ); cpane.add( jsp ); cpane.add( new JLabel("") ); JLabel lab4 = new JLabel("Adult "); gbag.setConstraints( lab4, gbceast ); cpane.add( lab4 ); JTextField adults = new JTextField(" ",3); gbag.setConstraints( adults, gbc ); cpane.add( adults ); JLabel Ch = new JLabel("Child"); gbag.setConstraints( Ch, gbceast ); cpane.add( Ch ); JTextField children = new JTextField(" ",3); gbag.setConstraints( children, gbc ); cpane.add( children ); JLabel Con = new JLabel("Concession"); gbag.setConstraints( Con, gbceast ); cpane.add( Con ); JTextField concess = new JTextField(" ",3); gbag.setConstraints( concess, gbclast ); cpane.add( concess ); JPanel pane = new JPanel(); accept = new JButton("Accept"); pane.add( accept ); reject = new JButton("Reject"); pane.add( reject ); accept.addActionListener( this ); reject.addActionListener( this ); quit = new JButton("Exit"); pane.add( quit ); gbag.setConstraints( pane, gbclast ); cpane.add( pane ); quit.addActionListener( this ); pack(); } public void actionPerformed( ActionEvent e ) { Object source = e.getSource(); if (source == reject) { System.out.println("\nReject"); jsp.setVisible(false); pack(); } else if (source == accept) { System.out.println("\nAccept"); addr.setText("First a very long line of text that should force a " +"horizontal scroll bar. We need a fair bit of text; the font" +"is surprisingly narrow.\n"); pack(); } else if (source == quit) { System.out.println("\nQuit"); jsp.setVisible(true); addr.append("and\nthen\nseveral\nlines\nto force\na " +"vertical\nscrollbar\n"); pack(); } } }