AhmadLang / Java, How To Program, 2004
.pdf
Figure 22.23. GridBagConstraints constants RELATIVE and REMAINDER.
(This item is displayed on pages 1043 - 1044 in the print version)
1// Fig. 22.23: GridBagFrame2.java
2// Demonstrating GridBagLayout constants.
3import java.awt.GridBagLayout;
4import java.awt.GridBagConstraints;
5import java.awt.Component;
6import javax.swing.JFrame;
7import javax.swing.JComboBox;
8import javax.swing.JTextField;
9import javax.swing.JList;
10import javax.swing.JButton;
12public class GridBagFrame2 extends JFrame
13{
14private GridBagLayout layout; // layout of this frame
15private GridBagConstraints constraints; // constraints of this layout
17// set up GUI
18public GridBagFrame2()
19{
20super( "GridBagLayout" );
21layout = new GridBagLayout();
22setLayout( layout ); // set frame layout
23constraints = new GridBagConstraints(); // instantiate constraints
25// create GUI components
26String metals[] = { "Copper", "Aluminum", "Silver" };
27JComboBox comboBox = new JComboBox( metals );
29JTextField textField = new JTextField( "TextField" );
31String fonts[] = { "Serif", "Monospaced" };T
32JList list = new JList( fonts );
34String names[] = { "zero", "one", "two", "three", "four" };
35JButton buttons[] = new JButton[ names.length ];
37 |
for ( int count = |
0; |
count < buttons.length; count++ |
) |
38 |
buttons[ count |
] |
= new JButton( names[ count ] ); |
|
39 |
|
|
|
|
40// define GUI component constraints for textField
41constraints.weightx = 1;
42constraints.weighty = 1;
43constraints.fill = GridBagConstraints.BOTH;
44constraints.gridwidth = GridBagConstraints.REMAINDER;
45addComponent( textField );
46
47// buttons[0] -- weightx and weighty are 1: fill is BOTH
48constraints.gridwidth = 1;
49addComponent( buttons[ 0 ] );
50
51// buttons[1] -- weightx and weighty are 1: fill is BOTH
52constraints.gridwidth = GridBagConstraints.RELATIVE;
53addComponent( buttons[ 1 ] );
54
55// buttons[2] -- weightx and weighty are 1: fill is BOTH
56constraints.gridwidth = GridBagConstraints.REMAINDER;
57addComponent( buttons[ 2 ] );
58
59// comboBox -- weightx is 1: fill is BOTH
60constraints.weighty = 0;
61constraints.gridwidth = GridBagConstraints.REMAINDER;
62addComponent( comboBox );
63
64// buttons[3] -- weightx is 1: fill is BOTH
65constraints.weighty = 1;
66constraints.gridwidth = GridBagConstraints.REMAINDER;
67addComponent( buttons[ 3 ] );
68
69// buttons[4] -- weightx and weighty are 1: fill is BOTH
70constraints.gridwidth = GridBagConstraints.RELATIVE;
71 addComponent( buttons[ 4 ] );
72
73// list -- weightx and weighty are 1: fill is BOTH
74constraints.gridwidth = GridBagConstraints.REMAINDER;
75addComponent( list );
76} // end GridBagFrame2 constructor
77
78// add a component to the container
79private void addComponent( Component component )
80{
81layout.setConstraints( component, constraints );
82add( component ); // add component
83} // end method addComponent
84} // end class GridBagFrame2
Figure 22.24. Test class for GridBagDemo2.
(This item is displayed on page 1045 in the print version)
1// Fig. 22.24: GridBagDemo2.java
2// Demonstrating GridBagLayout constants.
3import javax.swing.JFrame;
4
5public class GridBagDemo2
6{
7public static void main( String args[] )
8{
9GridBagFrame2 gridBagFrame = new GridBagFrame2();
10gridBagFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11gridBagFrame.setSize( 300, 200 ); // set frame size
12gridBagFrame.setVisible( true ); // display frame
13} // end main
14} // end class GridBagDemo2
JButton buttons[ 2 ] (lines 5657) has weightx and weighty values of 1. The fill variable is BOTH. This
JButton is the last component on the line, so REMAINDER is used. The JButton is added to the content pane with a call to addComponent.
[Page 1046]
The JComboBox (lines 6062) has a weightx of 1 and a weighty of 0. The JComboBox will not grow in the vertical direction. The JComboBox is the only component on the line, so REMAINDER is used. The JComboBox is added to the content pane with a call to addComponent.
JButton buttons[ 3 ] (lines 6567) has weightx and weighty values of 1. The fill variable is BOTH. This JButton is the only component on the line, so REMAINDER is used. The JButton is added to the content pane with a call to addComponent.
JButton buttons[ 4 ] (lines 7071) has weightx and weighty values of 1. The fill variable is BOTH. This JButton is the next-to-last component on the line, so RELATIVE is used. The JButton is added to the content pane with a call to addComponent.
The JList (lines 7475) has weightx and weighty values of 1. The fill variable is BOTH. The JList is added to the content pane with a call to addComponent.
[Page 1046 (continued)]
22.10. Wrap-Up
This chapter completes our introduction to GUI. In this chapter, you learned about more advanced GUI topics, such as menus, sliders, pop-up menus and the multiple-document interface. All these components can be added to existing applications to make them easier to use and understand. In the next chapter, you will learn about multithreading, a powerful capability that allows applications to use threads to perform multiple tasks at once.
[Page 1046 (continued)]
Summary
JSliders enable the user to select from a range of integer values. JSliders can display major
tick marks, minor tick marks and labels for the tick marks. They also support snap-to ticks, where positioning the thumb between two tick marks causes the thumb to snap to the closest tick mark.
If a JSlider has the focus, the left and right arrow keys cause the thumb of the JSlider to
decrease or increase by 1. The down and up arrow keys also cause the thumb of the JSlider to decrease or increase by 1, respectively. The PgDn (page down) key and PgUp (page up) key cause the thumb of the JSlider to decrease or increase by block increments of one-tenth of the range of values, respectively. The Home key moves the thumb to the minimum value of the JSlider, and the End key moves it to the maximum value.
JSliders have either horizontal or vertical orientation. For a horizontal JSlider, the minimum
value is at the extreme left and the maximum value is at the extreme right. For a vertical JSlider, the minimum value is at the extreme bottom and the maximum value is at the extreme top. The position of the thumb indicates the current value of the JSlider. Method getValue of class JSlider returns the current thumb position.
Method setMajorTickSpacing of class JSlider sets the spacing for tick marks on a JSlider. Method setPaintTicks with a TRue argument indicates that the tick marks should be displayed.
JSliders generate ChangeEvents when the user interacts with a JSlider. A ChangeListener declares method stateChanged that can respond to ChangeEvents.
Every window generates window events when the user manipulates it. Interface WindowListener provides seven window-event-handling methodswindowActivated, windowClosed, windowClosing, windowDeactivated, windowDeiconified, windowIconified and windowOpened.
Menus are an integral part of GUIs. Menus allow the user to perform actions without
unnecessarily cluttering a graphical user interface with extra GUI components. In Swing GUIs, menus can be attached only to objects of classes with method setJMenuBar (e.g., JFrame and
JApplet).
[Page 1047]
The classes used to declare menus are JMenuBar, JMenuItem, JMenu, JCheckBoxMenuItem and JRadioButtonMenuItem.
A JMenuBar is a container for menus. A JMenuItem is a GUI component inside a menu that, when
selected, causes an action to be performed. A JMenu contains menu items and can be added to a JMenuBar or to other JMenus as submenus.
When a menu is clicked, it expands to show its list of menu items. JMenu method addSeparator adds a separator line to a menu.
When a JCheckBoxMenuItem is selected, a check appears to the left of the menu item. When the JCheckBoxMenuItem is selected again, the check is removed.
When multiple JRadioButtonMenuItems are maintained as part of a ButtonGroup, only one item
in the group can be selected at a given time. When an item is selected, a filled circle appears to its left. When another JRadioButtonMenuItem is selected, the filled circle to the left of the previously selected item is removed.
AbstractButton method setMnemonic specifies the mnemonic for an AbstractButton. Mnemonic
characters are normally displayed with an underline.
A modal dialog box does not allow access to any other window in the application until the dialog
is dismissed. The dialogs displayed with class JOptionPane are modal dialogs. Class JDialog can be used to create your own modal or nonmodal dialogs.
Context-sensitive pop-up menus are created with class JPopupMenu. On most systems, the
popup trigger event occurs when the user presses and releases the right mouse button. MouseEvent method isPopupTrigger returns TRue if the pop-up trigger event occurred.
JPopupMenu method show displays a JPopupMenu. The first argument specifies the origin
component, which helps determine where the JPopupMenu will appear. The last two arguments are the coordinates from the origin component's upper-left corner, at which the JPopupMenu appears.
Class UIManager contains nested class LookAndFeelInfo that maintains information about a look- and-feel.
UIManager static method getInstalledLookAndFeels gets an array of
UIManager.LookAndFeelInfo objects that describe the available look-and-feels.
UIManager static method setLookAndFeel changes the look-and-feel. SwingUtilities static
method updateComponentTreeUI changes the look-and-feel of every component attached to its Component argument to the new look-and-feel.
Many of today's applications use a multiple-document interface (MDI) to manage several open
documents that are being processed in parallel. Swing's JDesktopPane and JInternalFrame classes provide support for creating multiple-document interfaces.
A JTabbedPane arranges GUI components into layers in which only one layer is visible at a time.
Users access each layer via a tab similar to those on folders in a file cabinet. When the user clicks a tab, the appropriate layer is displayed.
BoxLayout is a layout manager that allows GUI components to be arranged left-to-right or top- to-bottom in a container.
Class Box declares a container with BoxLayout as its default layout manager and provides static methods to create a Box with a horizontal or vertical BoxLayout.
GridBagLayout is a layout manager similar to GridLayout. It differs in that each component size can vary, and components can be added in any order.
A GridBagConstraints object specifies how a component is placed in a GridBagLayout. Method
setConstraints of class GridBagLayout takes a Component argument and a GridBagConstraints argument and sets the constraints of the Component.
[Page 1048]
Terminology
add method of class JMenuBar
addWindowListener method of class Window
anchor field of class GridBagConstraints border
BOTH constant of class GridBagConstraints
Box class
BoxLayout class
CENTER constant of class GridBagConstraints ChangeEvent class
ChangeListener interface child window
context-sensitive pop-up menu createGlue method of class Box createHorizontalBox method of class Box
createHorizontalGlue method of class Box createHorizontalStrut method of class Box createRigidArea method of class Box createVerticalBox method of class Box createVerticalGlue method of class Box createVerticalStrut method of class Box
Dimension class
dispose method of class Window document
EAST constant of class GridBagConstraints getClassName method of class
UIManager.LookAndFeelInfo
getInstalledLookAndFeels method of class UIManager getPreferredSize method of class Component getSelectedText method of class
JTextComponent
getValue method of class JSlider GridBagConstraints class
GridBagLayout class
gridheight field of class GridBagConstraints gridwidth field of class GridBagConstraints gridx field of class GridBagConstraints gridy field of class GridBagConstraints HORIZONTAL constant of GridBagConstraints horizontal glue
isPopupTrigger method of class MouseEvent isSelected method of class AbstractButton JCheckBoxMenuItem class
JDesktopPane class
JDialog class
JFrame class
JInternalFrame class
JMenu class
JMenuBar class
JMenuItem class
JRadioButtonMenuItem class
JSlider class
JTabbedPane class
line wrapping
LookAndFeelInfo nested class of class UIManager
menu
menu bar menu item
metal look-and-feel
minor tick marks of JSlider mnemonic
modal dialog box
multiple document interface (MDI)
NONE constant of class GridBagConstraints NORTH constant of class GridBagConstraints NORTHEAST constant of class GridBagConstraints NORTHWEST constant of class GridBagConstraints opaque
origin component
pack method of class Window paintComponent method of class JComponent parent window
parent window for a dialog box pluggable look and feel (PLAF) pop-up trigger event
RELATIVE constant of class GridBagConstraints REMAINDER constant of class GridBagConstraints rigid area
scrollbar policies selected text
separator line in a menu
setConstraints method of class GridBagLayout setDefaultCloseOperation method of class JFrame setInverted method of class JSlider
setJMenuBar method of class JFrame setLocation method of class Component
