• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Alignment Problem with GridBagLayout

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've come across a somewhat strange alignment behaviour when using GridBagLayout:

I am writing an Applet which should show a number of Buttons in a Panel (all AWT stuff).

Now when adding two Buttons to the left-hand Panel which is set to be using GridBagLayout, I get the following behaviour when trying to align the two Buttons at the top of the Panel:

The first Button is positioned correctly, the second isn't, as it remains centered vertically.



Possibly I'm just being stupid, but setting GridBagConstraints' weightx and weighty values to 1.0f helped fixing the problem with the first Button but didn't affect the other Button.

I'd be grateful for any suggestion on how to solve this!

Thanks in advance,


Steffen

Applet.jpg
[Thumbnail for Applet.jpg]
Screenshot of the Applet
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use GridBagLayout when other easier to use layouts might work better? How about a simple BoxLayout?


Though myself, I'd not use AWT but would prefer Swing.
 
Steffen Schmitt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your advice,Pete! I tried BoxLayout for the controlPanel and it did the job perfectly. The reason I considered using AWT (not one of my favourites) was that I wanted to use an Applet to control a MySQL database I have registered with my web hoster, who limits the use of the database to scripts/apps/Applets located on the database host itself. Sounded like an Applet (or a couple of Applets for that matter) could do the job nicely meaning that to ensure browser acceptance I was forced to choose java.applet.Applet over javax.swing.JApplet ... Now I've come up with a scheme of using WebStart, thus enabling use of swing libraries.

But you're perfectly right that BoxLayout is the one to use for alignment of a single column of components on a panel.

Thanks for your efforts!

Cheers,

Steffen
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steffen Schmitt wrote:to ensure browser acceptance I was forced to choose java.applet.Applet over javax.swing.JApplet ...


Why? JApplet extends Applet, and the browser shouldn't care what the applet contains, as long as the JVM supports the code. If a browser supports Applet I don't see why it wouldn't support JApplet as well.
 
    Bookmark Topic Watch Topic
  • New Topic