Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using GridBagLayout with JFrame

Reply
Thread Tools

Using GridBagLayout with JFrame

 
 
KingKongBundy
Guest
Posts: n/a
 
      08-24-2004
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      08-24-2004
On 24 Aug 2004 09:16:32 -0700, KingKongBundy wrote:

> 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

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      08-24-2004
KingKongBundy wrote:
> 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.

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      08-24-2004
KingKongBundy wrote:

>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/...ts.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.

--
Thomas<dot>Fritsch<squiggle>ops<dot>de

 
Reply With Quote
 
Madhur Ahuja
Guest
Posts: n/a
 
      08-24-2004
Sudsy wrote:
> 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





 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      08-24-2004
Madhur Ahuja wrote:
<snip>
> 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.

 
Reply With Quote
 
Cid
Guest
Posts: n/a
 
      08-24-2004
On Wed, 25 Aug 2004 01:43:20 +0530, "Madhur Ahuja" <(E-Mail Removed)> wrote:

>Sudsy wrote:
>> 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.


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.

 
Reply With Quote
 
KingKongBundy
Guest
Posts: n/a
 
      08-25-2004
Thomas Fritsch <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

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!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can I Get One JFrame To Appear Above Another JFrame clusardi2k@aol.com Java 2 08-27-2012 03:01 PM
start a new JFrame from an existing one, and when old JFrame closes new one does not jakester Java 3 04-02-2007 11:13 PM
GridBagLayout Example David McCallum Java 0 10-23-2003 04:12 PM
GridBagLayout and JTextField me Java 2 07-21-2003 02:59 PM
grid or gridbaglayout Passero Java 3 07-02-2003 09:21 PM



Advertisments