Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

GridbagLayout basics

Thread view: 
Scott Steiner - 18 Jul 2005 20:13 GMT
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);
Thomas Hawtin - 18 Jul 2005 20:28 GMT
> Hi,
>
[quoted text clipped - 9 lines]
>
> here's the code, what am I doing wrong?

> button = new JButton("Button 0");
> c.weightx = 1;
[quoted text clipped - 5 lines]
> button = new JButton("Button 1");
> c.weightx = 0.5;
c.gridwidth = 1;
> c.gridx = 0;
> c.gridy = 1;
> pane.add(button, c);
> [...]

Tom Hawtin
Signature

Unemployed English Java programmer

Carl - 18 Jul 2005 20:41 GMT
> Hi,
>
[quoted text clipped - 34 lines]
> 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.
Roedy Green - 18 Jul 2005 22:25 GMT
>button = new JButton("Button 0");
>c.weightx = 1;
[quoted text clipped - 14 lines]
>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.

Signature

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/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

David Segall - 19 Jul 2005 15:29 GMT
Welcome back Roedy. I have missed your advice.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.