Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Advice and help on adding components to paels

  1. #1
    ScottM is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Advice and help on adding components to paels

    New(ish) to Java. I'm trying to tack together a gui, with a panel that contains an array of buttons; the buttons are created at runtime. (If they don't all fit, I want the panel to expand, but I'll tackle that later).

    I've started out with the NetBeans 7.1 IDE's design tab, to create the panels for me. Then I add:
    javax.swing.JButton b = new javax.swing.JButton();
    b.setLabel("Foo");
    buttonPanel.add(b);
    b.setVisible(true);
    buttonPanel.revalidate();

    but it does absolutely nothing visible. I can't figure out of this is because .add() isn't the right operation here, or because the layout manager (netbeans generated code usingGroupLayout) is just not the way to go here because it has no idea where I want it added.

    So for help, I'd like to know how to add a button so it's visible. For advice, I'd like to know if I should dump the use of the NetBeans 7.1 design tab and just handwrite all the code (and which layout manager I'm best off with.) Thanks in advance.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Advice and help on adding components to paels

    You must add the buttons to the parent container...I would recommend reading the oracle tutorials on Swing and gain an understanding of the fundamentals of the API, as opposed to relying solely on a GUI designer that can be convenient, but in my opinion not the greatest solution for novices.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

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

    Default Re: Advice and help on adding components to paels

    Yes, just hand-write all your code. As for which layout manager, you will want to experiment with them. Often if you want a grid of JButtons, then a GridLayout works well. FlowLayout works for a very simple line of buttons. Check out the layout tutorials for a more detailed look at the various layouts available.

  4. #4
    ScottM is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Advice and help on adding components to paels

    I was afraid that would be the answer. Since I want one of the panels to expand (expanding the form as needed) as buttons are added, am I going to need my own layout manager, or is there a good one for this purpose? I'm not afraid of doing my own layout; I just didn't want a GUI that's basically a growable grid of buttons and a single list box, to be a significant part of the overall coding effort. :-/

    Thanks.

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

    Default Re: Advice and help on adding components to paels

    Look at the tutorial linked to above. Also note that:

    • Layouts to focus on first include GridLayout, BorderLayout, FlowLayout for its simplicity, and BoxLayout. Avoid the GridBagLayout and GroupLayout, at least when starting out.
    • Remember that you can create complex applications by nesting JPanels that each use a simple layout manager.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Advice and help on adding components to paels

    Quote Originally Posted by Fubarable View Post
    Avoid the GridBagLayout and GroupLayout, at least when starting out.
    Avoid the GridBagLayout when starting out, and GroupLayout forever.

    GroupLayout wasn't designed for human consumption.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Advice and help on adding components to paels

    Quote Originally Posted by DarrylBurke View Post
    Avoid the GridBagLayout when starting out, and GroupLayout forever.

    GroupLayout wasn't designed for human consumption.
    I initially thought the same way, but I've seen folks who've hand-coded GroupLayout quite well. Having said that, they're probably better off downloading and using MigLayout.
    DarrylBurke likes this.

  8. #8
    ScottM is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Advice and help on adding components to paels

    I've switched over to FlowLayout, based on the documentation, which promises that it will add objects in a row until there is no more room, and then start a new row. That's exactly what I want.

    The problem is, the panel I'm adding the objects (buttons in this case) simply grows to the right to get everything on a single row. It does this even if it means the panel extends off the frame. Not what I want.

    Poking around the doc, I don't see as way to convince a panel to stick to the width of the frame, and grow downwards as new rows become necessary. I thought setPreferredSize might have something to do with this, but it didn't change the behaviour. Do I need to write my own layout manager for this? Should I be using a grid layout, and keeping track of the width of the panel myself as I add to the grid? (Trying to avoid this because the buttons might vary in width, so I'd rather not think in terms of fixed sized grid cells.)

    I figure someone must have already dealt with "arbitrary number of buttons to be added to a panel, and they should all be visible" problem before, so I'm looking for a push in the right direction. TIA.

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

    Default Re: Advice and help on adding components to paels

    Consider using a GridLayout and not specifying the row count but only the column count. This can be done by using a 0 in the constructor for the row count parameter, and an int for the column count.

Similar Threads

  1. removing and adding SWING components
    By Athlon* in forum AWT / Swing
    Replies: 1
    Last Post: 12-06-2011, 06:12 PM
  2. Adding multiple components to 1 container index
    By mmcnitt in forum AWT / Swing
    Replies: 3
    Last Post: 03-07-2011, 03:59 PM
  3. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 10:13 PM
  4. Adding components to a panel
    By jboy in forum New To Java
    Replies: 1
    Last Post: 10-10-2009, 01:02 PM
  5. adding components to frame
    By roaan in forum New To Java
    Replies: 4
    Last Post: 07-05-2009, 03:30 PM

Posting Permissions

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