Java - AWT/Swing

GridBagLayout

Again and again you hear that the GridBagLayout is so complicated. It is really very extensive, but as a general rule you only need a few methods. The GridBagLayout works fundamentally with a thought grid. Each array in this grid can be addressed by its position (x,y). To define the size of the grid, you can outline the desired layout on a piece of paper. With the class GridBagConstraints, the qualities of a single element are determined.



Example
The desired dialogue:


The beyond lying grid:

0,0 1,0
0,1 1,1

The list on the left side situated the two grid arrays 0,0 and 0,1. Therefore, the height of the list is „2“. To assign a control (here of the list) to details of position and height, the class GridBagConstraints is used.

The go with it source looks like this:

package com.sowas.javademo.awt;
 
import java.awt.*;
import java.awt.event.*;
 
public class GridBagLayoutDemo extends Frame implements ActionListener {
	Button btClose;
 
	public GridBagLayoutDemo(){
		super("GridBagLayoutDemo");
 
		GridBagLayout gbl=new GridBagLayout();
		setLayout(gbl);
		GridBagConstraints gbc=new GridBagConstraints();
 
		// Determine, that the GUI-elements fill out
                // the grid arrays in horizontal direction:
		gbc.fill=GridBagConstraints.HORIZONTAL;
 
		// Determine the distances of the single 
                // GUI-elements to the thought grid lines:
		gbc.insets = new Insets(2,2,2,2);  
 
		gbc.gridx = 0;  // x-position in the thought grid
		gbc.gridy = 0;  // y-position in the thought grid
		gbc.gridheight = 2;  // two grid-arrays high
		List list = new List(3);
		gbl.setConstraints(list, gbc);
		add(list);
 
		gbc.gridx=1; 
		gbc.gridy=0;
		gbc.gridheight = 1;
		Label label = new Label("Only a demo");
		gbl.setConstraints(label, gbc);
		add(label);
 
		gbc.gridx=1; 
		gbc.gridy=1;
		gbc.gridheight = 1;
		btClose = new Button("close");
		btClose.addActionListener(this);
		gbl.setConstraints(btClose, gbc);
		add(btClose);
 
		pack();
	}
 
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == btClose)
			dispose();
	}
 
	public static void main(String[] args){
		GridBagLayoutDemo demo = new GridBagLayoutDemo();
		demo.setVisible(true);
	}
}

Of course, GridBagLayout has more possibilities as well. GridBagConstraints-parameter are also used often, too.

weightx

and

weighty

. These can take values between 0.0 and 1.0 and indicate how the lingering freedom will be divided up. Example: A dialogue has an available width of 600 pixel. There are laid out two elements A and B in this dialogue. With it, element A has a minimal place requirement of 300 pixel and element B has a minimal place requirement of 100 pixel. Therefore, there are 200 pixel left. These are divided up for weightx proportional to both values. Assuming that element A has a weightx of 0.75 and element B a weightx of 0.25, the grid field for A will contain a width of 450 pixel (300+0.75*200) and the grid field B will contain a width of 150 (100+0.25*200).

To be continued…


Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007