Javas GridBagLayout does not display an (extended) Canvas (AWT / Java 1.4)


J

java

Good evening!

I don't manage to make an AWT-Frame with a GridBagLayout display a
Canvas. This Canvas will be displayed as soon as I use an other than
the damned GridBagLayout. Unfortunately I have to use that
GridBagLayout but do not know how.

An other posting I found suggested to extend the Canvas and override
it's method getPreferredSize. I did so but still the (My)Canvas does
not appear within the GridBagLayout. Can anybody give me a helping
hint?

Regards

Christian

import java.awt.*;
import java.awt.event.*;

public class PaintCanves extends Frame {

public PaintCanves() {

GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);

this.setSize(150, 200);
this.setLocation(100, 100);
this.setBackground(Color.gray);

GridBagConstraints constraints = new GridBagConstraints();
constraints.gridheight = GridBagConstraints.REMAINDER;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;

MyCanvas canvas = new MyCanvas();
canvas.setBackground(Color.black);

this.add(canvas, constraints);

}

class MyCanvas extends Canvas {

public Dimension getPreferredSize() {
// canvas claims at least all the surrounding frame
return new Dimension(getWidth(), getHeight());
}

}

public static void main (String argv[]) {

PaintCanves maler = new PaintCanves();

maler.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e) {
System.exit (0);
}
});

maler.setVisible (true);

}

}
 
Ad

Advertisements

T

Thomas Weidenfeller

the damned GridBagLayout.

Step 1: Lose your attitude.
Unfortunately I have to use that
GridBagLayout but do not know how.

Step 2: Read Sun's UI Tutorial to learn how to.
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridheight = GridBagConstraints.REMAINDER;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;

constraints.weightx = 1.0;
constraints.weighty = 1.0;
MyCanvas canvas = new MyCanvas();

Canvas canvas = new Canvas();

/Thomas
 
C

Chris Uppal

Thomas said:
Step 1: Lose your attitude.

Eh ? So the OP doesn't like GridBagLayout, what's wrong with that ? /Nobody/
likes GridBagLayout...

(Or if they do, then they must be very odd ;-)

-- chris
 
Ad

Advertisements

T

Thomas Hawtin

Good evening!

I don't manage to make an AWT-Frame with a GridBagLayout display a
Canvas. This Canvas will be displayed as soon as I use an other than
the damned GridBagLayout. Unfortunately I have to use that
GridBagLayout but do not know how.

GridBagLayout may have a poor implementation and a terrible API, but
it's the most useful layout manager in the Java library.
An other posting I found suggested to extend the Canvas and override
it's method getPreferredSize. I did so but still the (My)Canvas does
not appear within the GridBagLayout. Can anybody give me a helping
hint?

You have actually called getWidth() and getHeight() on the Canvas rather
than the Frame (if you didn't extend Frame, you might not have fallen
into that trap). There is a misfeature in GridBagLayout (fixed in 1.6 I
believe) where if the preferred size wont fit, the minimum size will be
used instead. Even if you had used the Frame, you haven't accounted for
the insets, so it would be too big to lay out.

If you want to make a GridBagLayout row and column fill the available
space, you should add a component with weightx and weighty set to
something positive (usually 1.0). It's also possible to directly set the
rowWeights and columnWeights of the layout manager.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top