Using GridBagLayout with JFrame

K

KingKongBundy

Could anyone tell me how I would modify this code (in the
gridbaglayout if possible) so that when the JFrame pops up, there is
'white space' between the JLabel and JButton. Currently, when this
pops up, the JLabel and JButton are on top of one another. When I
comment the line 'dialog.pack();' out, I get the desired JFrame look
(in terms of white space), but the entire JLabel message does not
display without having to re-sizing the window.

Thanks!!



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class Question
{
public static void main(String[] args)
{
String greetMessage = "Hello world - Hopefully this will work";
GreetingMessage dialog = new GreetingMessage(greetMessage);
dialog.pack();
dialog.show();
}
} //Question

class GreetingMessage extends JFrame
{
public GreetingMessage(String message)
{
Toolkit theKit = getToolkit();

GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Container contentPane = getContentPane();
contentPane.setLayout(layout);


//Set constraints for JLabel
constraints.weightx = 0.0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;

JLabel newLabel = new JLabel(message);
layout.setConstraints(newLabel,constraints);

contentPane.add(newLabel);

constraints.fill = constraints.NONE;
constraints.weightx = 2.0;
constraints.weighty = 2.0;
constraints.gridx = 0;
constraints.gridy = 100;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;

JButton ok = new JButton("Ok");
layout.setConstraints(ok,constraints);
contentPane.add(ok);
setSize(400,400);

} //GreetingMessage constructor
} //GreetingMessage
 
A

Andrew Thompson

Could anyone tell me how I would modify this code (in the
gridbaglayout if possible)

Why? What is the logic of using a GBL in
the first place? A nested Layout is often
simpler to construct, and more reliable.
..so that when the JFrame pops up, there is
'white space' between the JLabel and JButton. Currently, when this
pops up, the JLabel and JButton are on top of one another. When I
comment the line 'dialog.pack();' out,

If removing pack(), 'fixes' a lyout, it has
serious problems..
I get the desired JFrame look
(in terms of white space), but the entire JLabel message does not
display without having to re-sizing the window.

Try this non BGL equivalent, note that the
button traverses the width of the bottom, but
you could change that by adding it inside a JPanel
with a FlowLayout (*nested* inside the current
BorderLayout)

<sscce>
import java.awt.*;
import javax.swing.*;

public class Question {

public static void main(String[] args) {
String greetMessage = "Hello world - Hopefully this will work";
GreetingMessage dialog = new GreetingMessage(greetMessage);
dialog.pack();
// dialog.show(); //deprecated as of 1.5
dialog.setVisible(true); //future proofing ..
}
} //Question

class GreetingMessage extends JFrame {

public GreetingMessage(String message)
{
//Toolkit theKit = getToolkit();
//what's that here for? it does nothing

//GridBagLayout layout = new GridBagLayout();
// no longer used, this is a layout you should rarely need.

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout(15,15));

JLabel newLabel = new JLabel(message);

contentPane.add(newLabel, BorderLayout.NORTH);

JButton ok = new JButton("Ok");
contentPane.add(ok, BorderLayout.SOUTH);
setSize(400,400);

} //GreetingMessage constructor
} //GreetingMessage
</sscce>

BTW, for GUI matter, you'd best post to..
<http://www.physci.org/codes/javafaq.jsp#cljg>

HTH
 
S

Sudsy

KingKongBundy said:
Could anyone tell me how I would modify this code (in the
gridbaglayout if possible) so that when the JFrame pops up, there is
'white space' between the JLabel and JButton. Currently, when this
pops up, the JLabel and JButton are on top of one another. When I
comment the line 'dialog.pack();' out, I get the desired JFrame look
(in terms of white space), but the entire JLabel message does not
display without having to re-sizing the window.

The gridy for the OK button should be 1, not 100. Also, the weightx
and weighty should be something like 0.5 and should be the same for
both elements.
You could clean up your code a lot by only specifying those values
which are changing, i.e. replace all this:

constraints.fill = constraints.NONE;
constraints.weightx = 2.0;
constraints.weighty = 2.0;
constraints.gridx = 0;
constraints.gridy = 100;
constraints.gridwidth = 1;
constraints.gridheight = 1;

with this:

constraints.fill = constraints.NONE;
constraints.gridy++;

Incrementing gridx and gridy instead of setting to absolute values
(excepting when resetting gridx to 0 to start a new row) allows a
bit more flexibility for future modifications IMHO.
 
T

Thomas Fritsch

KingKongBundy said:
Could anyone tell me how I would modify this code (in the
gridbaglayout if possible) so that when the JFrame pops up, there is
'white space' between the JLabel and JButton. Currently, when this
pops up, the JLabel and JButton are on top of one another. When I
comment the line 'dialog.pack();' out, I get the desired JFrame look
(in terms of white space), but the entire JLabel message does not
display without having to re-sizing the window.

....

//Set constraints for JLabel
constraints.weightx = 0.0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;
constraints.insets = new Insets(15,15,15,15);
// See at http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GridBagConstraints.html#insets
JLabel newLabel = new JLabel(message);
layout.setConstraints(newLabel,constraints);

contentPane.add(newLabel);

constraints.fill = constraints.NONE;
constraints.weightx = 2.0;
constraints.weighty = 2.0;
constraints.gridx = 0;
constraints.gridy = 100;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;
constraints.insets = new Insets(15,15,15,15);
JButton ok = new JButton("Ok");
layout.setConstraints(ok,constraints);
contentPane.add(ok);
setSize(400,400);
// BTW: setSize not necessary here. Size will be set by pack() anyway.
 
M

Madhur Ahuja

Sudsy said:
KingKongBundy wrote:
Also, the weightx
and weighty should be something like 0.5 and should be the same for
both elements.

Hello

Why!, If the weightx and weighty are same for both elements, will it make a
difference
what value it is? After all, they are just proportions.


--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
 
S

Sudsy

Madhur Ahuja wrote:
Why!, If the weightx and weighty are same for both elements, will it make a
difference
what value it is? After all, they are just proportions.

From the javadocs:
weightx

public double weightx

Specifies how to distribute extra horizontal space.

The grid bag layout manager calculates the weight of a column to be the
maximum weightx of all the components in a column. If the resulting layout
is smaller horizontally than the area it needs to fill, the extra space is
distributed to each column in proportion to its weight. A column that has a
weight of zero receives no extra space.

If all the weights are zero, all the extra space appears between the grids
of the cell and the left and right edges.

Where it comes into play is if the size of the Container is smaller than
the Components it contains. Giving priority to one of the Components by
specifying a higher weightx or weighty will alter the visual rendering.
In the situation posited by the OP it doesn't seem to make sense to
prioritize the display, any more than it makes sense to specify a gridy
of 100 when there are only two Components... :-0
Having spent a bunch of time with GridBagLayout and GridBagConstraints,
I'm merely suggesting a beneficial approach. Apparently, YMMV.
 
C

Cid

Hello

Why!, If the weightx and weighty are same for both elements, will it make a
difference
what value it is? After all, they are just proportions.

If weightx is 0 (which it is by default) then the components won't
resize at all on that axis (same deal for weighty). If you do set
values for one or more components on an axis, all those values are
normalized by Swing internally (range 0..1). So you could set them
both to .5, 1, or 2534 as long as they're the same.

If you're going to add more components later it can be conceptually
simpler to keep track of if you just start out using normalized
weights to begin with. The Swing tutorial recommends (for this reason)
that you use weights, by axis, that add up to 1.0. Just makes it
easier for a reader to visualize.
 
K

KingKongBundy

Thanks - the inset statements took care of the problem. I wanted to
use the GBL for general practice, but the suggestion to use
BorderLayout above also worked and was a lot simpler. Besten Dank!!
 

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

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top