Re: GUI components creation idioms

Gregg Wonderly <[email protected]>
Newsgroups gmane.comp.java.netbeans.user-interface
Message-ID <[email protected]>

King Lung Chiu wrote:
> Hi again,
> 
> Usually when I create a form, there are multiple components of the same
> type, eg. 5 combo boxes, or 5 text fields, and Netbeans creates a new
> variable for each of those components.
> 
> I'd like to have them created as items in a data structure (eg. an array or
> ArrayList or HashMap of combo boxes),to make it easier to reference them
> later. Is there an automatic way to do that?
> 
> My current workaround is to create the data structure myself (eg.
> EnumMap<FieldAttribute, JTextField>), and manually add the components to
> that data structure in each comopnent's post-creation code (in this case,
> maping each component to its corresponding enum value). However, this method
> doesn't scale when I have a large number of components to add. (lots of code
> copy + paste + small-edit).

In this case, I don't use Matisse to create such panels. I write all the helper 
methods and do all of the layout by hand using tools like my Packer 
(http://packer.dev.java.net) layout manager which provides a friendlier 
interface to GridBagLayout.

public int makeFields( EnumMap<FieldAttribute,JTextField> map,
		FieldAttribute[] flds, Packer pk, int y ) {
	for( int i = 0; i < flds.length; ++i ) {
		// See http://www.artima.com/weblogs/viewpost.jsp?thread=98193
		// for more information on what its ++y and not y++ here.
		pk.pack( new JLabel(flds[i].desc) ).gridx(0).gridy(++y);
		JTextField fld;
		pk.pack( fld = new JTextField( flds[i].default )
			 ).gridx(1).gridy(y).fillx();
		map.put( flds[i], fld );
	}
	return y;
}

public void init() {

	... some initialization ...

	JPanel fldPan = new JPanel();
	Packer fldPk = new Packer( fldPan );
	int y = makeFields( map, flds, fldPk, -1 );

	... more initialization ...
}

You can use Matisse for other things, and then just add such a mechanically
generated panel into an empty panel that is a place holder created in matisse.

Gregg Wonderly
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.