• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Need Help with GridBagLayout  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a JDialog using GridBagLayout and cannot get it right. Am I perhaps using the wrong layout manager? This is how I create the dialog:

This is how I am running it:

The problem is the 2nd row. The field for "Last" is off the panel. I have attached an image of the result. Why is the size not automatically set wide enough to show all the components? I was led to beleive that I should not be using 'setPreferredSize' with GridBagLayout. TIA.

layout.png
[Thumbnail for layout.png]
Dialog Panel
 
Marshal
Posts: 59765
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you find oiut about Cai Horstmann's GBC class instead of plain simple constraints. It is easier to use. I think the problem may be that you have not given the text field an initial size, which you can do by passing the number of characters you want it to contain, nor have you given it a fill constraint. Also call pack() before calling setVisible().
 
Campbell Ritchie
Marshal
Posts: 59765
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: your no‑arguments constructor looks like iffy design to me. You will be able to create an instance which isn' in a consistent state; it will have null for its dialog field.
Actually, you are setting the fill, but you have it well hidden in that makeGbc() method.
 
Sam Ritter
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. The empty constructor is just for the SSCCE. The real code does a lot more. I'll look into the GBC class.
 
Sam Ritter
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am giving GBC a shot but obviously I don't understand what I am doing. The gridy parameter does not seem to do anything. I changed that part of the code to

Now I am getting this:
layout.png
[Thumbnail for layout.png]
 
Sam Ritter
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've given up on using GridBagLayout. Using Eclipse WindowBuilder I got what I want using GridLayout and making each row a separate JPanel. Easy with WindowBuilder but messy looking and lengthy code.
 
Campbell Ritchie
Marshal
Posts: 59765
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:. . . . The gridy parameter does not seem to do anything.  . . .

Oh, yes it does!

You are adding two components at position 0, 0. Setting the x‑span for the email label at 2 means it takes up 2 units of width in that particular row. How much do all the x‑spans in that row add up to?
Yes multiple panels with a different layout on each is a valid alternative way to set up your GUI.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!