Results 1 to 10 of 10
- 12-03-2009, 09:02 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
What layoutmanager is suitable for forms.
I need to use java to create a form.
Unfortunately I'm not allowed to use some graphic builder.
I think this is a very hard assignment and I keep having problems using layoutmanagers.
There is a null-layout which doesn't work when resizing the frame and is hard when you want to extend the frame/panel. You also need to create a helpvariable to give the position of the components.
There is FlowLayout which gives you very little control over what you are doing.
There is BorderLayout which I don't really understand. You can put one object in the Center and one on each side of the frame. So what if I have more than 5 components. What happens when I put more than one object at the center or one of the sides?
There is GridLayout, which appears to be simple, but when you run it, suddenly it resizes your components and makes them ugly.
Then there is SpringLayout, which seems to be best for forms, but it's very lowlevel and I''m not able to get all my components created inside one for-loop.
So how to get a nice form?
I'm by the way very new to creating GUI's, so please give me a simple LayoutManager which actually listens to whatever I tell it to display.
-
You should read the Sun Layout Manager tutorial for more info on how to use layouts. In particular check out the Visual Guide to Layout Managers to really see what they do.
For me the key to using the layout managers was to learn that JPanels can be nested one inside the other, each using a different layout manager, and that this will allow you to create complex layouts using relatively simple layout managers.
The other secret is to start playing with code and seeing what you can come up with. With perseverance you will succeed!
Best of luck.
- 12-04-2009, 03:17 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
I already read them and it says that SpringLayout is suitable, which is very lowlevel and difficult.
Please tell me exactly what panels and layouts I need to use.For me the key to using the layout managers was to learn that JPanels can be nested one inside the other, each using a different layout manager, and that this will allow you to create complex layouts using relatively simple layout managers.
The form is as simple as a couple of labels each followed by a textfield.
And below it is a commandbutton to submit the form.
-
I would avoid SpringLayout if I were you.
I can't tell you "exactly what panels and layouts" you will need as I don't have exact specifications, but what's more, I don't want them as I don't want to ruin the fun for you. I suggest you try the layouts out and see what works best. Often I use BorderLayout as the outer-most layout and then other layouts such as BoxLayout, GridLayout or FlowLayouts inside of this, but again, try them, and see what works best.Please tell me exactly what panels and layouts I need to use.
The form is as simple as a couple of labels each followed by a textfield.
And below it is a commandbutton to submit the form.
- 12-04-2009, 09:31 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
This is a common layout and I wish it was supported better in the JDK.The form is as simple as a couple of labels each followed by a textfield.
The obvious choice would be a GridLayout, but the problem is all component get resized to the same size, which may (or may not) be what you want.
So to solve the above problem you could try creating 2 vertical GridLayouts, one for the labels and one for the text fields.
Next is the GridBagLayout. It would reasonably well for add components in a row/column fashion. The main problem is when you resize the frame smaller the textfields will disappear. So my suggestion here is to set the frame unresizable and you don't have to worry about this.
I also would avoid creating a SpringLayout by hand. But if you look at the second example in the tutorial, I believe it uses a supplied utility "makeGrid" to do the complex work for you. Try it and see if you like it.
I would keep the button in its own layout.
There are some ideas for you to try out.
- 12-04-2009, 10:58 PM #6
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Box and BoxLayout are great for this sort of problem.
The outer JPanel should be a vertical box with four horizontal boxes.
Each row has its components and then a horizontal glue box to deal with window expansion.
The bottom row is highly flexible Box.Filler to take up all unallocated space.
Java Code:class Choices extends Box { Choices() { super(BoxLayout.Y_AXIS); Box row1 = Box.CreateHorizontalBox(); Label r11 = new Label(...); TextField r12 = new TextField(...); row1.add(r11); row1.add(r12); row1.add(Box.createHorizontalGlue()); Box row2 = Box.CreateHorizontalBox(); ... Box row3 = Box.CreateHorizontalBox(); ... Box row4 = Box.Filler(new Dimension(0,0), new Dimension(0,0), new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); add(row1); add(row2); add(row3); add(row4); }}
- 12-04-2009, 11:42 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Typically, when a label/textfield form is created people like to have the label and textfield aligned in columns to make the form look tidy. As far as I know, a BoxLayout will just add the component horizontally like the FlowLayout does which means the components won't be aligned in columns unless you massage all the labels to be of the same size. Or am I missing something?Box and BoxLayout are great for this sort of problem....
- 12-05-2009, 12:44 AM #8
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
Thanks for all your advice.
I think I will combine a BorderLayout with a GridLayout.
The center contains a panel with a GridLayout containing all the fields.
The south contains the submit button.
When I expand it the results have to be shown.
So I put everything made before in a panel in the north and then I put the results in the center.
- 12-05-2009, 06:56 AM #9
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
The BorderLayout/GridLayout solution looks good.
camrickr is correct that Box will not automatically line up both rows and columns.
The right way is to use a GridLayout for the labels and fields;
the GridLayout could be housed in boxes or in a BorderLayout.
-
Also, it may be worth your while to look at MiGLayout which can be found here: MiG Layout Java Layout Manager for Swing and SWT
Similar Threads
-
java.sql.SQLException: No suitable driver
By Heather in forum JDBCReplies: 6Last Post: 09-01-2010, 11:29 AM -
Which is the most suitable IDE ?
By javaamateur in forum NetBeansReplies: 2Last Post: 10-11-2009, 11:21 AM -
java.sql.SQLException: No suitable driver .... when using Custom ClassLoader
By tomer1960 in forum Advanced JavaReplies: 13Last Post: 03-16-2009, 09:37 PM -
What database is more suitable to a java app?
By hittite in forum New To JavaReplies: 6Last Post: 11-14-2008, 04:33 AM -
How to make online jsp forms from Microsoft word forms in java
By jiten.mistry in forum Advanced JavaReplies: 2Last Post: 04-28-2008, 10:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks