GridBagConstrainst fill error

A

Asra

Hello,
I'm using a JPanel and putting a component in it with
GridBagConstraints' fill value set to BOTH. However, the component
doesn't fill the entire Panel and remains in the center. Where should
I check for errors?
-Asra
 
C

Chris Smith

I'm using a JPanel and putting a component in it with
GridBagConstraints' fill value set to BOTH. However, the component
doesn't fill the entire Panel and remains in the center. Where should
I check for errors?

In some cases (for example, a label) component may technically fill an
entire panel but not look like it because of the way the component draws
itself. Use something like setBackground(Color.RED) and setOpaque(true)
to find the real dimensions, and determine if the fill is working or
not.

If it's really not working, you'll need to provide the code before
anyone can help.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

MikL

I think you might need to set a weightx value to let the GridBagLayout know
what columns can be resized.
 
S

Sudsy

Asra said:
Here is a sample code:
javax.swing.JTextArea area = new javax.swing.JTextArea("Nebula Client");
javax.swing.border.EtchedBorder bord = new javax.swing.border.EtchedBorder();
area.setBackground(Color.RED);
area.setOpaque(true);
area.setBorder(bord);
java.awt.GridBagConstraints cons = new java.awt.GridBagConstraints();
cons.fill = java.awt.GridBagConstraints.BOTH;
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 1;
cons.gridheight = 4;


this.add(area, cons);

The code is incomplete. Where is the GridBagLayout defined and set as
the layout manager? I expected to see code like this:

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

....

layout.setConstraints( area, cons );
add( area );

I assume that your class extends a Container of some sort?
 
B

Babu Kalakrishnan

Asra said:
Here is a sample code:
javax.swing.JTextArea area = new javax.swing.JTextArea("Nebula Client");
javax.swing.border.EtchedBorder bord = new javax.swing.border.EtchedBorder();
area.setBackground(Color.RED);
area.setOpaque(true);
area.setBorder(bord);
java.awt.GridBagConstraints cons = new java.awt.GridBagConstraints();
cons.fill = java.awt.GridBagConstraints.BOTH;
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 1;
cons.gridheight = 4;


this.add(area, cons);

Provide a non-zero weight in the axis in which you want fill to take
place. (Set both weightx and weighty to non-zero values for fill in both
axes)

BK
 
A

Asra

Here is a sample code:
javax.swing.JTextArea area = new javax.swing.JTextArea("Nebula Client");
javax.swing.border.EtchedBorder bord = new javax.swing.border.EtchedBorder();
area.setBackground(Color.RED);
area.setOpaque(true);
area.setBorder(bord);
java.awt.GridBagConstraints cons = new java.awt.GridBagConstraints();
cons.fill = java.awt.GridBagConstraints.BOTH;
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 1;
cons.gridheight = 4;


this.add(area, cons);
 
A

Asra

Thanks a lot for your help. Yes, weightx and weighty work but the
GridBagConstraints object is given by the client and can not be
changed. Is there anything in the Pane or something that we can set to
get this effect?
 
C

Chris Smith

(e-mail address removed) says...
Thanks a lot for your help. Yes, weightx and weighty work but the
GridBagConstraints object is given by the client and can not be
changed. Is there anything in the Pane or something that we can set to
get this effect?

No there isn't. The GridBagConstraints is one place where things like
that are specified. You will need to change the GridBagConstraints
somehow. Why aren't you able to change it? Would it be okay if you
change it and just put it back the way it was afterwards?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top