Java Reference
In-Depth Information
application, so all the components will use the default. Line 40 calls our utility method
addComponent (declared at lines 69-78). The JTextArea object, the row, the column, the
number of columns to span and the number of rows to span are passed as arguments.
JButton button1
JButton button1 is the next component added (lines 43-44). By default, the weightx and
weighty values are still zero. The fill variable is set to HORIZONTAL —the component will
always fill its area in the horizontal direction. The vertical direction is not filled. The
weighty value is zero, so the button will become taller only if another component in the
same row has a nonzero weighty value. JButton button1 is located at row 0, column 1.
One row and two columns are occupied.
JComboBox comboBox
JComboBox comboBox is the next component added (line 48). By default, the weightx and
weighty values are zero, and the fill variable is set to HORIZONTAL . The JComboBox button
will grow only in the horizontal direction. The weightx , weighty and fill variables retain
the values set in constraints until they're changed. The JComboBox button is placed at
row 2, column 1. One row and two columns are occupied.
JButton button2
JButton button2 is the next component added (lines 51-54). It's given a weightx value
of 1000 and a weighty value of 1 . The area occupied by the button is capable of growing
in the vertical and horizontal directions. The fill variable is set to BOTH , which specifies
that the button will always fill the entire area. When the window is resized, button2 will
grow. The button is placed at row 1, column 1. One row and one column are occupied.
JButton button3
JButton button3 is added next (lines 57-59). Both the weightx value and weighty value
are set to zero, and the value of fill is BOTH . JButton button3 will grow if the window is
resized—it's affected by the weight values of button2 . The weightx value for button2 is
much larger than that for button3 . When resizing occurs, button2 will occupy a larger
percentage of the new space. The button is placed at row 1, column 2. One row and one
column are occupied.
JTextField textField and JTextArea textArea2
Both the JTextField textField (line 62) and JTextArea textArea2 (line 65) have a
weightx value of 0 and a weighty value of 0. The value of fill is BOTH . The JTextField
is placed at row 3, column 0, and the JTextArea at row 3, column 2. The JTextField oc-
cupies one row and two columns, the JTextArea one row and one column.
Method addComponent
Method addComponent 's parameters are a Component reference component and integers
row , column , width and height . Lines 72-73 set the GridBagConstraints variables gridx
and gridy . The gridx variable is assigned the column in which the Component will be
placed, and the gridy value is assigned the row in which the Component will be placed.
Lines 74-75 set the GridBagConstraints variables gridwidth and gridheight . The
gridwidth variable specifies the number of columns the Component will span in the grid,
 
Search WWH ::




Custom Search