[Prev][Next][Index][Thread]

Re: DUIM question



Scott McKay wrote:
> 
> Maybe Carl Gay can regale us with the stories of his recent battles
> with the Java GridBug class.  He has some Java code to do this,
> and it's literally three screenfuls long.
> 
> Oh, I meant GridBag.

Actually after much time wasted with GridBug I found that there
_is_ a way to do this in Java that isn't too bad _once you know
how to do it_.  Basically you use two columns, each with a 
GridLayout.  The fact that GridLayout ensures its cells are
all the same size makes the rows line up correctly:

    /**
     * Make a JPanel that has its components layed-out in two columns
     * each with an equal number of rows.  This is useful for preferences
     * panes and dialogs.
     * @param title  If non-null, a border with this title will be added to the panel.
     * @param spacing  Amount of spacing between the border and panel contents.
     * @param components  An array of Component objects, of which the even
     *        numbered elements will appear in the left column and the odd
     *        in the right column.
     */
    public static JPanel make_2column_panel (String title, int spacing, Component[] components) {
	JPanel panel = new JPanel(new BorderLayout());
	JPanel columns = new JPanel(new BorderLayout());
	JPanel east = new JPanel(new GridLayout(0, 1, 5, 5));
	JPanel west = new JPanel(new GridLayout(0, 1, 5, 5));
	for (int i = 0; i < components.length; i++) {
            ((i % 2 == 0) ? west : east).add(components[i]);
	}
	columns.add(east, BorderLayout.EAST);
	columns.add(west, BorderLayout.WEST);
	if (title != null)
	    panel.setBorder(Util.make_border(title, spacing));
	panel.add(columns, BorderLayout.WEST);
	panel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
	return panel;
    }

Unfortunately, as soon as you want to put anything of a different
height in one of the cells the above completely breaks down, and
that's exactly what I needed to do.  So it's back to GridBugLayout.

GridBugLayout is great for showing off deficiencies in the Java
language.  Because there are no keyword arguments it's hard to
create new GridBagConstraint objects with just the constraints
you want (so people end up reusing the same one, with the obvious
result).  Because of the dichotomy between Object and primitive
types it's hard to roll your own keyword args.  i.e., this doesn't
work:

  make_gbconstraint(new Object[] { "gridx", 1, "gridy", 2 });

Because there are no macros you can't do something like DUIM's 
tabling macro.  It's just an all-around Bad Scene.

I'm hoping I'll find the time to convert this app to Dylan for
a full comparison, but you know how that goes.

> Andy Armstrong wrote in message ...
> >The class <table-layout> is the way to do this. Try the following in
> >the Dylan Playground:
> >
> >  contain(make(<table-layout>,
> >               children: vector(make(<label>, label: "Short"),
> >                                make(<text-field>),
> >                                make(<label>, label: "Much longer"),
> >                                make(<text-field>)),
> >               columns: 2,
> >               x-spacing: 2, y-spacing: 5,
> >               x-alignment: #[#"right", #"left"]));
> >
> >The DUIM GUI test suite has a number of examples of this, try
> >the 'Borders' example for starters, in:
> >
> >  [FD]\examples\duim\duim-gui-test-suite\borders.dylan
> >



References: