Adding the Components to the Container


Adding the Components to the Container

Once we have selected the GUI components we will use, we can then add the components to the container. Java maintains a certain amount of control over how components are positioned inside a container. You don't position the GUI components yourself; you define something called a layout manager that arranges the components for you. If the GUI window is resized or changed in some other manner, the layout manager will reposition the GUI components. Even with layout managers, you still have a lot of control over the look of your GUI display. You can specify a component's preferred size, its orientation, and the margins between it and neighboring components, to name a few.

Most of the Java layout manager classes can be found in the java.awt package. The CardLayout class is a layout manager that stacks components like a deck of cards. The FlowLayout class places the components in a line from left to right. When the right edge of the container is reached, the layout continues on the left-hand side below the previous row. The GridLayout class is a layout manager that places the components in a two-dimensional grid. Each cell in the grid has the same size. The GridBagLayout class is the most sophisticated layout manager in the java.awt package and gives you the most control over the size and position of your components.

The layout manager we will use for the AtmGUI class is an instance of the BorderLayout class. This type of layout manager was chosen to keep things simple and because we have a relatively small number of components to place. A BorderLayout object places components in five sections of the container ”north, south, east, west, and center.

A container can change its current layout manager by invoking the setLayout() method, passing it a reference to an instance of the desired layout class. Components are added to the container using the add() method. When a BorderLayout layout manager is used, the add() method takes two arguments ”the component to be placed and the quadrant of the container in which to place it. We will place the JComboBox , JTextField , and JButton in the north section and the JTextArea in the center section.

If you remember, with higher level Swing containers you don't add GUI components directly to the container. If you try to do this, you will generate an error. Components are added to one of the panes associated with the container, usually the content pane. A higher level container can get a reference to its content pane using the getContentPane() method. The setLayout() and add() methods are called on the content pane object.

There is one more issue to consider before we conclude this section. The BorderLayout layout manager has a quirk ” it will resize components to fill the available space in a given section (north, center, etc.). This can result in really large, strange looking components. The FlowLayout layout manager does not do this resizing; instead it maintains the component's preferred size. To prevent the BorderLayout manager from resizing our components, we can first place the components on a JPanel (the default layout manager of JPanel objects is FlowLayout ) and then place the JPanel on the content pane of our JFrame . Because a JPanel is a low-level container, it does not have the various subpanes. You can add components directly to a JPanel .

To lay out our GUI components according to the previous discussion, we will add the following lines of code to the AtmGUI class immediately after the GUI component definition statements. The blank JLabel components are used for spacing.

 //  The components are placed on a JPanel before //  being added to the content pane to maintain //  their preferred sizes. JPanel northPanel = new JPanel(); northPanel.add(comboLabel); northPanel.add(comboBox); northPanel.add(new JLabel("     ")); northPanel.add(textFieldLabel); northPanel.add(textField); northPanel.add(new JLabel("     ")); northPanel.add(runButton); JPanel centerPanel = new JPanel(); centerPanel.add(textAreaLabel); centerPanel.add(textArea); //  Add the JPanel objects to the content pane getContentPane().setLayout(new BorderLayout()); getContentPane().add(northPanel, BorderLayout.NORTH); getContentPane().add(centerPanel, BorderLayout.CENTER); 

When you add this code to the AtmGUI class and compile and run the code, a frame with all of the GUI components appears on your screen. Figure 26.1 at the end of this chapter is a typical screen shot.

Figure 26.1. Atmosphere modeling tool GUI

graphics/26fig01.jpg



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net