Chapter 13 Graphical User Interface (Domain and web hosting) Components: Part 2
Chapter 13 Graphical User Interface Components: Part 2 779 of each component in the grid is instantiated with line 26. Lines 23 through 30 instantiate each of the GUI components that will be added to the content pane. JTextArea textArea1 is the first component added to the GridBagLayout (line 44). The values for weightx and weighty values are not specified in gbConstraints, so each has the value zero by default. Thus, the JTextArea will not resize itself even if space is available. However, the JTextAreaspans multiple rows, so the vertical size is subject to the weighty values of JButtons button2 and button3. When either button2 or button3 is resized vertically based on its weighty value, the JTextArea is also resized. Line 43 sets variable fillin constraintsto GridBagConstraints.BOTH, causing the JTextArea to always fill its entire allocated area in the grid. An anchor value is not specified in constraints, so the default CENTER is used. We do not use variable anchor in this program, so all components will use the default. Line 36 calls our utility method addComponent method (defined at lines 81 95). The JTextArea object, the row, the column, the number of columns to span and the number of rows to span are passed as arguments. Method addComponent s parameters are a Component reference component and integers row, column, width and height. Lines 85 86 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 89 90 set the GridBagConstraints variables gridwidth and gridheight. The gridwidthvariable specifies the number of columns the Component will span in the grid and the gridheight variable specifies the number of rows the Component will span in the grid. Line 93 sets the GridBagConstraints for a component in the GridBagLayout. Method setConstraints of class GridBagLayout takes a Component argument and a GridBagConstraints argument. Method add (line 94) is used to add the component to the content pane. JButton object button1 is the next component added (lines 48 49). The values of weightx and weighty 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 b1 is located at row 0, column 1. One row and two columns are occupied. JComboBox comboBox is the next component added (line 54). 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. Note that the weightx, weighty and fill variables remain set in gbConstraints until they are changed. The JComboBox button is placed at row 2, column 1. One row and two columns are occupied. JButton object button2 is the next component added (lines 57 60). JButton button2 is 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, b2 will grow. The button is placed at row 1, column 1. One row and one column are occupied. JButton button3 is added next (lines 64 66). Both the weightx value and weighty value are set to zero, and the value of fillis BOTH. JButtonbutton3 will
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.