Results 1 to 9 of 9
- 02-18-2012, 06:41 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
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.
- 02-18-2012, 06:57 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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)
-
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.
- 02-18-2012, 07:11 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
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.
-
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.
- 02-18-2012, 08:53 PM #6
-
Re: Advice and help on adding components to paels
- 02-19-2012, 05:12 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
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.
-
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
-
removing and adding SWING components
By Athlon* in forum AWT / SwingReplies: 1Last Post: 12-06-2011, 06:12 PM -
Adding multiple components to 1 container index
By mmcnitt in forum AWT / SwingReplies: 3Last Post: 03-07-2011, 03:59 PM -
Adding and removing components from a GridBagLayout
By peterhabe in forum New To JavaReplies: 4Last Post: 09-19-2010, 10:13 PM -
Adding components to a panel
By jboy in forum New To JavaReplies: 1Last Post: 10-10-2009, 01:02 PM -
adding components to frame
By roaan in forum New To JavaReplies: 4Last Post: 07-05-2009, 03:30 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks