• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Layouts

 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I want to create a form using swing like this:

Upgrade(Label) + Description(Textbox) + At(Label) + City(ComboBox) + Add(Button).


I have tried gridlayout, flowlayout, and many other layouts but I don't like them.
GridLayout, for example, gives as much space to the "AT" field as it does to the description textbox, which looks horrible.

In short, on the left is how it looks, and on the right is how I want it to look. I have to use layout managers,
because it is going to be an application for mobile devices, so the screen size is unpredictable.



I have put all the code here to keep this post short: http://pastebin.com/RAZnNZYP
Untitled.png
[Thumbnail for Untitled.png]
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GridLayout gives each component the exact same size, no matter what the content is. You definitely do not want this. A simple FlowLayout appears to be appropriate, but if it isn't there are more powerful layout managers around; GridBagLayout is of course the most notorious of those because of its complexity and programmer unfriendliness.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to say I LOVE GridBagLayout. Its the only layout that hasn't driven me crazy! But I don't think you need it for this.

I just quickly made the GUI you were trying to do and I just stuck everything on a JPanel and added the Panel to a JFrame. Using the default layouts.

Edit: Although you will have to change the JFrame layout if you plan on adding more of these little form things to it.
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Flow layout was okay, but when I resize vertically, it doesn't adjust the components, unlike gridlayout. Any suggestions on how I can make the label smaller in the gridlayout? Maybe use a gridlayout in a gridlayout? Please help, I am not experienced with this, at all
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code that you have written for the one in the picture?

Edit: I wasn't paying attention, i see you have posted the code!
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about if you add the "at" label and the comboBox to another JPanel and then add the JPanel? Does that work? You do the same thing with the other label and textfield.
 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think a BoxLayout.HORIZONTAL for each individual JPanel, added to a container with BoxLayout.VERTICAL might do the trick.

Oh, and if you might be adding a large number of these sub-panels, you might want to add them to a JPanel with the vertical layout, and place that outer panel in a JScrollPane which is added to the frame.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disclaimer: I am still on my first coffee.
I don't see why a FlowLayout won't work.
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all these helpful suggestions. I am going to try them now
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, Darryl Burke, that was perfect

Thanks to all the others, too.
There are still a couple of problems though.

This is what I did:

mainPane: GridLayout(NumberOfItems + extraSpace, 1 column).
BuildingPane: BoxLayout(BuildingPane, BoxLayout.X_AXIS)

details about extraSpace are found in the code. It's much neater and very well commented now, so it should be easy to understand.

Here is how it looks now:



It also works when I put in more than one, but I think I really need to use scrollbars.

and place that outer panel in a JScrollPane which is added to the frame.

That is exactly what I am trying to do. I have tried setting mainPane to a JScrollPane instead of a JPanel,
and then I set the layout to a ScrollPaneLayout() instead of a GridLayout().

But when I run the program, I just get this:


This is the first time I am using scrolling. Should I try to use a JScrollBar instead of scrollPane?
I don't want to mess up my layouts just to add a scrollbar


Here is all the code, as it was when I took the first screenshot: http://pastebin.com/yMd7L16B


 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried setting mainPane to a JScrollPane instead of a JPanel,
and then I set the layout to a ScrollPaneLayout() instead of a GridLayout().

No, no, NO! Time for a tutorial or two.
http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I understand correctly after reading that, should I try to create a new scrollpane, then put the mainPane in it?
Like this:

JScrollPane myScrollPane = new JScrollPane();
myScrollPane.add(mainPane);

Then I need to add a JScrollBar and use a scrollable interface?

It's a good tutorial and I read it, but it's a bit hard to apply, and some parts are hard to understand.
I would appreciate a little more help if possible.
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woot! it works

After playing around a bit, here's what it looks like:

All that was needed was a nice little

JScrollPane spane = new JScrollPane(mainPane);
window.add(spane); //add mainPane to window


And this, to make it look better when resized:
this.setPreferredSize((new Dimension(getPreferredSize().width, (int)Math.round(getPreferredSize().height * 1.5))));
(Note, the above code was in the BuildingPane class, "this" refers to each separate pane.)

Solved
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!