Results 1 to 10 of 10
  1. #1
    Somelauw is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Somelauw is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You should read the < I can't post URL's :-(> for more info on how to use layouts. In particular check out the < I can't post URL's :-(> to really see what they do.
    I already read them and it says that SpringLayout is suitable, which is very lowlevel and difficult.

    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.
    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Somelauw View Post
    I already read them and it says that SpringLayout is suitable, which is very lowlevel and difficult.
    I would avoid SpringLayout if I were you.

    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.
    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.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The form is as simple as a couple of labels each followed by a textfield.
    This is a common layout and I wish it was supported better in the JDK.

    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.

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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);
    }}

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Box and BoxLayout are great for this sort of problem....
    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?

  8. #8
    Somelauw is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    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.

  9. #9
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 6
    Last Post: 09-01-2010, 12:29 PM
  2. Which is the most suitable IDE ?
    By javaamateur in forum NetBeans
    Replies: 2
    Last Post: 10-11-2009, 12:21 PM
  3. Replies: 13
    Last Post: 03-16-2009, 10:37 PM
  4. What database is more suitable to a java app?
    By hittite in forum New To Java
    Replies: 6
    Last Post: 11-14-2008, 05:33 AM
  5. Replies: 2
    Last Post: 04-28-2008, 11:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •