• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

GridBagLayout Question: Irregular Spacing

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a GUI that uses GridBagLayout, and I am having some difficulties getting things laid out the way I want them. The GUI consists of six JTextFields, six JLabels, and two JButtons
I want the two buttons to be half the width of the GUI, and the rest of the GUI laid out as appropriate for the widths of the fields and labels. Unfortunately, one of the JLabels is wider than half the area, and it seems to be defining the grid cell width (the edge of the buttons line up with the edge of the is label). I've tried adding extra cells in the grid, but that doesn't solve the problem.
Attached is a copy of the code that does my GUI layout. The GUI has a few other problems, but they are along the same lines. If anyone can give me a suggestion as to how to fix this problem, I should be able to get the rest of it to work.
Thanks for the help,
Don
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
you did a really good job with the two buttons. They are of quite the same size, even if the window is resized.
If you want them really independent of the GridBagLayout you should place them in their own panel and use another layout that will provide you some mean to make them equally sized. That might be BoxLayout or GridLayout.
What I just thought was: Why don't you place the "Choose File" Button behind the textfield displaying the file, reducing the button text to ("Choose" or "Browse" or even "..."). That is quite a common view in guis. you could separate the row containing the file choosing stuff from the rest of the text fields and place the run button equally separated in the lower right corner. Just a suggestion!!
 
Don Gardner
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the complement! I've worked hard on it, but it just not quite the way I want it.
If you look at it, you will notice that the leading edge of heightField and filenameField line up, and the trailing edge of heightField and chooseFileButton line up. As a result, widthLabel is too narrow (I would like it to be the same width as heightLabel), and chooseFileButton is too wide (just a little bit).
I've thought about putting each line in it's own panel, but I would rather not do that unless there is no other way. My gut feeling is that GridBagLayout should be capable of doing what I want it to do, but that I don't have my GridBagConstraints set correctly, or something else. I wouldn't think that moving to a different layout scheme would help becuase GridBagLayout is the most powerful layout scheme (so I've heard), so I would think the other scheme would be more limited.
I originally had the chooseFileButton onthe same line as the filenameField, but I moved it because I thought it was too crowded, and I wanted to collect the buttons together. I think I might do as you suggested and replace "Choose File" with "...", and put it all on the same line again. That way I will be able to add some other button on the bottom.
Thanks again for the complement, and thanks for the advice,
Don
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean. Actually, I suspect that GridBagLayout isn't meant to handle the things this way. What you want to do is, make the components equally sized, indepent of their actual sizes. GridLayout works that way, but using this again, cells can't span more than one row/column but all cells are of the same width/height.
GridBagLayout provides a more "content-oriented" layout. you just say which components shall align and where things can expand and where not in case the frame gets resized. you don't want to specify any exact sizes: GridBagLayout should handle that if it's configured in the right way.
What I found very usefull is: with GridBagLayout you can create forms very dynamically without bothering much with the single components. You can add rows and columns just by incrementing the gridx and gridy value.
Maybe you are interested in how I would code it:

I like the way you set the components in your layout but I think it did cost you much work and if you decide to add more buttons or worse - more textfield and labels with different sizes you might get even more work to adapt the layout.
Even if the textfields are way to big for the values they are meant to display (and that can be changed if the labels get weightx = 1), I prefer to have them all aligned without bothering about the individual size of each of them.
but, that are just ideas - I'm still searching for the ultimate layout.
cheers!
 
Don Gardner
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the code! I'm always interested in how different people attack the same problem.
You are right, creating and modifying the GUI the way I have done it can be time consuming, and my time would probably be better spent on other parts of the program, but laying out GUIs is fun.
I think the difference between your GUI layout and what I am trying to achieve is mainly philosophical. Both GUIs would work, and neither is difficult for the user to understand. However, I am also trying to satisfy aesthetic (and educational) needs as well as the functional needs. If the GUI was more complicated or I thought I would need to change it often, I might write it differently. As it is, I enjoy the distraction from the rest of the program.
Thanks again,
Don
 
Don Gardner
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I resorted to two panels, one with a GridBagLayout and one with a GridLayout. It looks pretty good, except that the GridLayout buttons and textfields are taller , and it looks kinda goofy that all of the GridLayout stuff is the same size. Pick pick pick. I'm never satisfied.
Well, if you are interested here is what I have now.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Don,
sorry that I didn't answer earlier.
On the one hand, I find the interface - hm, better? The GridLayout part looks very clean and neat. But you're right, it looks different to the GridBagLayout part.
On the other hand, I guess, the buttons in the last row don't have anything to do with the textfields in the row above this last one, do they? but it looks that way.
That is why I am a fan of spaces to group logical units in a gui. I prefer bigger frames with relatively less content.
cheers
Chantal
 
Don Gardner
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chantal Ackermann:

On the other hand, I guess, the buttons in the last row don't have anything to do with the textfields in the row above this last one, do they? but it looks that way.


Actually, the text fields do have something to do with the buttons on the bottom. The text fields show the max and min frame position, and the current frame field shows the current frame. If you push play, the current fram field would increment as the frames are shown. If you push REV or FWD, the current frame would decrement or increment. If you paused, and typed a number in the current frame, the animation would jump to that frame.
Now maybe the current frame text field is the only one that really belongs in the control panel, but I thought it would be useful to have the bounds right there.
Does that not makes sense?
Thanks for the feedback,
Don
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what I meant with whether the buttons have something to do with the textfields was: has each textfield something to do with the corresponding button underneath. as you explain it, all three of the fields have the same relation to all three of the buttons. but it does rather look like each button would have a special relation to the field above it.
hm. was that clear?
c
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic