Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > GridbagLayout basics

Reply
Thread Tools

GridbagLayout basics

 
 
Scott Steiner
Guest
Posts: n/a
 
      07-18-2005
Hi,

I want to get the following arrangment of buttons:

# button 0 #
# button 1 ## button 2#

but I'm getting this instead:

# button 0 #
# button 1 #

here's the code, what am I doing wrong?

pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

JButton button;

button = new JButton("Button 0");
c.weightx = 1;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);

button = new JButton("Button 2");
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(button, c);
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      07-18-2005
Scott Steiner wrote:
> Hi,
>
> I want to get the following arrangment of buttons:
>
> # button 0 #
> # button 1 ## button 2#
>
> but I'm getting this instead:
>
> # button 0 #
> # button 1 #
>
> here's the code, what am I doing wrong?


> button = new JButton("Button 0");
> c.weightx = 1;
> c.gridwidth = 2;
> c.gridx = 0;
> c.gridy = 0;
> pane.add(button, c);
>
> button = new JButton("Button 1");
> c.weightx = 0.5;

c.gridwidth = 1;
> c.gridx = 0;
> c.gridy = 1;
> pane.add(button, c);
> [...]


Tom Hawtin
--
Unemployed English Java programmer
 
Reply With Quote
 
 
 
 
Carl
Guest
Posts: n/a
 
      07-18-2005
Scott Steiner wrote:
> Hi,
>
> I want to get the following arrangment of buttons:
>
> # button 0 #
> # button 1 ## button 2#
>
> but I'm getting this instead:
>
> # button 0 #
> # button 1 #
>
> here's the code, what am I doing wrong?
>
> pane.setLayout(new GridBagLayout());
> GridBagConstraints c = new GridBagConstraints();
> c.fill = GridBagConstraints.HORIZONTAL;
>
> JButton button;
>
> button = new JButton("Button 0");
> c.weightx = 1;
> c.gridwidth = 2;
> c.gridx = 0;
> c.gridy = 0;
> pane.add(button, c);
>
> button = new JButton("Button 1");
> c.weightx = 0.5;
> c.gridx = 0;
> c.gridy = 1;
> pane.add(button, c);
>
> button = new JButton("Button 2");
> c.weightx = 0.5;
> c.gridx = 1;
> c.gridy = 1;
> pane.add(button, c);


One problem I see is that you set the gridwidth to "2", but never set it
back to 1 for the buttons you want to appear side by side. I believe
that will solve your problem.

Carl.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-18-2005
On Mon, 18 Jul 2005 21:13:06 +0200, Scott Steiner
<(E-Mail Removed)> wrote or quoted :

>button = new JButton("Button 0");
>c.weightx = 1;
>c.gridwidth = 2;
>c.gridx = 0;
>c.gridy = 0;
>pane.add(button, c);
>
>button = new JButton("Button 1");
>c.weightx = 0.5;
>c.gridx = 0;
>c.gridy = 1;
>pane.add(button, c);
>
>button = new JButton("Button 2");
>c.weightx = 0.5;
>c.gridx = 1;
>c.gridy = 1;
>pane.add(button, c);


see
http://mindprod.com/jgloss/gridbaglayout.html


You should not be reusing your constraint object. It leads too easily
to errors like this where you set gridwidth=2 for all your buttons.
You need to set it back to 1.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
David Segall
Guest
Posts: n/a
 
      07-19-2005
Roedy Green <(E-Mail Removed)> wrote:

Welcome back Roedy. I have missed your advice.

 
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
Simple GridBagLayout Question AJ Java 6 06-14-2004 05:32 PM
Viewing GridBagLayout's Grid Lines? John Davison Java 6 12-10-2003 10:03 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