Java Article 1 Code: GridBagForm.java

This code has been superceded in two ways.

First it has been split into two classes, one whose instances each represent a whole form, and another whose instances represent individual components on a form

Second, it has been redesigned to permit the whole range of GridBagLayout constraints options to be used.


////////////////////////////////////////////////////////////////////////////////
//	HelpIndex: (c) 1996 PHD Computer Consultants Ltd
//
//			see http://www.phdcc.com/helpindex/
////////////////////////////////////////////////////////////////////////////////
//	GridBagForm	statically represents a whole form
//				instances represents individual components on form
////////////////////////////////////////////////////////////////////////////////
//	Version history
//	23-Sep-96	1.2.1	Split from HelpIndex.java file
//	2-Oct-96	1.2.4	Font parameter for component
//	3-Oct-96	1.2.5	statusText added
////////////////////////////////////////////////////////////////////////////////

import java.awt.*;
import java.util.Vector;

class GridBagForm
{
////////////////////////////////////////////////////////////////////////////////
// Class constants and variables

	public static final short FORM_LABEL	= 1;
	public static final short FORM_TEXTFIELD= 2;
	public static final short FORM_TEXTAREA	= 3;
	public static final short FORM_LIST		= 4;
	public static final short FORM_BUTTON	= 5;
	public static final short FORM_SCROLLBAR= 6;
	public static final short FORM_CHOICE	= 7;

	private static boolean			inited		= false;

	private static Container		container	= null;
	private static GridBagLayout	gridbag		= null;
	private static GridBagConstraints constraints= null;
	private static Vector			itemList	= null;

	private static int TabCurrent = -1;
	private static int itemCount = 0;

	private Component	component;

////////////////////////////////////////////////////////////////////////////////
// Instance variables

	private int			id;
	private short		type;
	private String		name;
	private short		size;
	private Color		bgColour;
	private Color		fgColour;
	private String		statusText;

////////////////////////////////////////////////////////////////////////////////

private GridBagForm( int _id, short _type, String _name, short _size, Color _bgColour, Color _fgColour, String _statusText)
{
	component = null;

	id = _id;
	type = _type;
	name = _name;
	size = _size;
	bgColour = _bgColour;
	fgColour = _fgColour;
	statusText = _statusText;
}

////////////////////////////////////////////////////////////////////////////////

public static void init( Container _container, Font font, int anchor, double weightx, double weighty)
{
	container	= _container;

	itemList	= new Vector();
	gridbag     = new GridBagLayout();
	constraints = new GridBagConstraints();
	constraints.anchor = anchor;
	constraints.weightx = weightx;
	constraints.weighty = weighty;

	if( font!=null)
		container.setFont(font);

	container.setLayout(gridbag);

	TabCurrent = -1;
	itemCount = 0;
	inited = true;
}

////////////////////////////////////////////////////////////////////////////////
//	to do: add other form components

public static Component addComponent( int id, short type, String name, int size, Color bgColour, Color fgColour, Font font, String statusText, int gridwidth, int fill)
{
	if( !inited) return null;

	GridBagForm newItem = new GridBagForm( id, type, name, (short)size, bgColour, fgColour, statusText);
	if( newItem==null) return null;

	switch( type)
	{
	case FORM_LABEL:
		newItem.component = new Label( name);
		break;
	case FORM_TEXTFIELD:
		newItem.component = new TextField(size);
		break;
	case FORM_LIST:
		newItem.component = new List(Math.abs(size),(size<0));
		break;
	case FORM_BUTTON:
		newItem.component = new Button(name);
		break;
	}

	if( newItem.component==null) return null;

	newItem.component.setBackground( bgColour);
	newItem.component.setForeground( fgColour);
	if( font!=null)
		newItem.component.setFont(font);

	constraints.gridwidth = gridwidth;
	constraints.fill = fill;
	gridbag.setConstraints( newItem.component, constraints);
	container.add( newItem.component);

	itemList.addElement( newItem);
	itemCount++;

	return newItem.component;
}


////////////////////////////////////////////////////////////////////////////////

public static int getIdFromComponent( Object component)
{
	for( int i=0; i<itemCount; i++)
	{
		GridBagForm thisItem = (GridBagForm)itemList.elementAt(i);
		if( component==thisItem.component)
			return thisItem.id;
	}
	return 0;
}

////////////////////////////////////////////////////////////////////////////////

public static Component getComponentFromId( int id)
{
	for( int i=0; i<itemCount; i++)
	{
		GridBagForm thisItem = (GridBagForm)itemList.elementAt(i);
		if( id==thisItem.id)
			return thisItem.component;
	}
	return null;
}

////////////////////////////////////////////////////////////////////////////////

public static void start()
{
//	browsers complain that neither TabCurrent nor itemCount can be accessed
//	TabCurrent = itemCount-1;
	Tab( true);
}

////////////////////////////////////////////////////////////////////////////////

public static void Tab( boolean onwards)
{
	if( !inited || itemCount==0 ) return;

	// look for next item for focus
	for( int i=0; i<itemCount; i++)
	{
		if( onwards)
		{
			if( ++TabCurrent >= itemCount)
				TabCurrent = 0;
		}
		else
		{
			if( --TabCurrent < 0)
				TabCurrent = itemCount-1;
		}

		GridBagForm thisItem = (GridBagForm)itemList.elementAt(TabCurrent);
		if( thisItem.type != FORM_LABEL)
		{
			thisItem.component.requestFocus();
			break;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////

public static String GotFocus( Object component)
{
	if( !inited) return null;

	// serach for the new focus
	for( int i=0; i<itemCount; i++)
	{
		GridBagForm thisItem = (GridBagForm)itemList.elementAt(i);
		if( component==thisItem.component)
		{
			TabCurrent = i;
			return thisItem.statusText;
		}
	}
	return null;
}

////////////////////////////////////////////////////////////////////////////////

public static String statusText()
{
	GridBagForm thisItem = (GridBagForm)itemList.elementAt(TabCurrent);
	return thisItem.statusText;
}

////////////////////////////////////////////////////////////////////////////////
// GridBagForm class end curly bracket
}
////////////////////////////////////////////////////////////////////////////////

Back to Practical Java article