Re: resizing problems.

Julien Chastang wrote:
> Hi.
> 
> The code below is meant to show two visad displays in one container. I am
> interested in having one display take up 60% of the container's width, and
> the other 40%. I am using GridBagLayout to achieve this specification with
> the "weightx" option to specify the 60% and 40% values. If you run this
> code, however, you will note that it does not consistently preserve these
> proportion before and after resizing. You can test this with a ruler. Try
> resizing the window and you will see what I mean. 
> 
> (Note: the visad displays are contained within JPanels. The JPanels
> are then added to the container.) 
> 
> If panel1 and panel2 are empty, however, the correct proportions seem to
> be maintained before and after resizing. You can comment out
> 
> panel1.add((new DisplayImplJ2D("2")).getComponent());
> panel2.add((new DisplayImplJ3D("3")).getComponent());
> 
> to test this.
> 
> Does anyone know what is going on, and how to fix this problem? Is this a
> visad specific problem?

Nope, not a VisAD problem, just layout manager idiosyncrasies.

The first problem is that the default FlowLayout manager is getting
in your way by keeping the Displays at their preferred size rather
than resizing them to something more appropriate.  Setting the panel
layout managers to BorderLayout (or any other resizing layout manager)
will fix that.

The other problem is that, if at all possible, GridBagLayout will
try to give windows their preferred size.  When trying to find the
optimal layout for your window, it will give each Display *exactly*
as much space as requested by its preferred size and since they're
both requesting 256x256, they both end up the same size.

Try running your example program after fixing the layout manager
problem.  Note how when the window comes up, both Displays have
exactly half of the available space.  If you make the window
just a bit narrower, the 3D Display shrinks down to 40% of the
space as requested.  Also, as you widen the window past the
initial width the ratio keeps creeping closer to 40%.

To fix this, you just need to make sure the preferred size really
reflects the size you'd like to see.  You can do this by getting
the preferred size for the 2D Display, then setting the 3D
Display's preferred size to 2/3rds of that.

Here's a code snippet which should fix all your problems:

        JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());

        Component dpy2d = new DisplayImplJ2D("2").getComponent();
        panel1.add(dpy2d);

        panel1.setBorder(raisedBevel);
        c.weightx = 0.6;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 0;
        gridbag.setConstraints(panel1, c);
        contentPane.add(panel1);

        Dimension dim2d = dpy2d.getPreferredSize();
        int w = (int )(dim2d.getWidth()*(2.0/3.0));
        int h = (int )(dim2d.getHeight()*(2.0/3.0));

        JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());

        Component dpy3d = new DisplayImplJ3D("3").getComponent();
        ((JComponent )dpy3d).setPreferredSize(new Dimension(w, h));
        panel2.add(dpy3d);

        panel2.setBorder(raisedBevel);
        c.weightx = 0.4;
        c.weighty = 1.0;
        c.gridx = 1;
        c.gridy = 0;
        gridbag.setConstraints(panel2, c);
        contentPane.add(panel2);