26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13 Graphical User Interface Components: Part 2 779<br />

of each component in the grid is instantiated with line 26. Lines 23 through 30 instantiate<br />

each of the GUI components that will be added <strong>to</strong> the content pane.<br />

JTextArea textArea1 is the first component added <strong>to</strong> the GridBagLayout<br />

(line 44). The values for weightx and weighty values are not specified in gbConstraints,<br />

so each has the value zero by default. Thus, the JTextArea will not resize<br />

itself even if space is available. <strong>How</strong>ever, the JTextArea spans multiple rows, so the vertical<br />

size is subject <strong>to</strong> the weighty values of JBut<strong>to</strong>ns but<strong>to</strong>n2 and but<strong>to</strong>n3. When<br />

either but<strong>to</strong>n2 or but<strong>to</strong>n3 is resized vertically based on its weighty value, the<br />

JTextArea is also resized.<br />

Line 43 sets variable fill in constraints <strong>to</strong> GridBagConstraints.BOTH,<br />

causing the JTextArea <strong>to</strong> always fill its entire allocated area in the grid. An anchor<br />

value is not specified in constraints, so the default CENTER is used. We do not use<br />

variable anchor in this program, so all components will use the default. Line 36 calls our<br />

utility method addComponent method (defined at lines 81–95). The JTextArea<br />

object, the row, the column, the number of columns <strong>to</strong> span and the number of rows <strong>to</strong> span<br />

are passed as arguments.<br />

Method addComponent’s parameters are a Component reference component<br />

and integers row, column, width and height. Lines 85–86 set the GridBagConstraints<br />

variables gridx and gridy. The gridx variable is assigned the column in<br />

which the Component will be placed, and the gridy value is assigned the row in which<br />

the Component will be placed. Lines 89–90 set the GridBagConstraints variables<br />

gridwidth and gridheight. The gridwidth variable specifies the number of columns<br />

the Component will span in the grid and the gridheight variable specifies the<br />

number of rows the Component will span in the grid. Line 93 sets the GridBagConstraints<br />

for a component in the GridBagLayout. Method setConstraints of<br />

class GridBagLayout takes a Component argument and a GridBagConstraints<br />

argument. Method add (line 94) is used <strong>to</strong> add the component <strong>to</strong> the content pane.<br />

JBut<strong>to</strong>n object but<strong>to</strong>n1 is the next component added (lines 48–49). The values of<br />

weightx and weighty are still zero. The fill variable is set <strong>to</strong> HORIZONTAL—the<br />

component will always fill its area in the horizontal direction. The vertical direction is not<br />

filled. The weighty value is zero, so the but<strong>to</strong>n will become taller only if another component<br />

in the same row has a nonzero weighty value. JBut<strong>to</strong>n b1 is located at row 0,<br />

column 1. One row and two columns are occupied.<br />

JComboBox comboBox is the next component added (line 54). The weightx and<br />

weighty values are zero, and the fill variable is set <strong>to</strong> HORIZONTAL. The JComboBox<br />

but<strong>to</strong>n will grow only in the horizontal direction. Note that the weightx,<br />

weighty and fill variables remain set in gbConstraints until they are changed. The<br />

JComboBox but<strong>to</strong>n is placed at row 2, column 1. One row and two columns are occupied.<br />

JBut<strong>to</strong>n object but<strong>to</strong>n2 is the next component added (lines 57–60). JBut<strong>to</strong>n<br />

but<strong>to</strong>n2 is given a weightx value of 1000 and a weighty value of 1. The area occupied<br />

by the but<strong>to</strong>n is capable of growing in the vertical and horizontal directions. The fill<br />

variable is set <strong>to</strong> BOTH, which specifies that the but<strong>to</strong>n will always fill the entire area. When<br />

the window is resized, b2 will grow. The but<strong>to</strong>n is placed at row 1, column 1. One row and<br />

one column are occupied.<br />

JBut<strong>to</strong>n but<strong>to</strong>n3 is added next (lines 64–66). Both the weightx value and<br />

weighty value are set <strong>to</strong> zero, and the value of fill is BOTH. JBut<strong>to</strong>n but<strong>to</strong>n3 will

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!