Take the 2-minute tour ×

I'm creating a GUI in Java using the GridBagLayout. Is there any way for me to create a component group so that I can pass the reference to the group and have access to all of them?

I've considered creating a panel and grouping the components that way, but I was wondering if there was another way that makes use of the complexity of the GridBagLayout.

Thanks so much!

share|improve this question
What do you mean by "have access to all of them?" –  finnw Feb 11 '10 at 14:44
I have a bunch of JLabels, JTextFields, and JButtons. The label describes what's in the textfield, and the button will change what's in the textfield. My question is really as follows: is there a way to know which textfield is associated with a specific button from the button's action listener? –  chama Feb 11 '10 at 14:56
Map<JComponent, JLabel> maybe. –  vickirk Feb 11 '10 at 16:24
add comment

3 Answers

up vote 3 down vote accepted

Well, if you can't create a bean with JLabel, JTextField and JButton for containing your group, you can always use Map in your main ui panel to register the elements while you add them. Some structure like

Map<K, List<Component>>

might work, where K is a identifier for a group. This way you dissociate the components from the way they are placed in ui.

share|improve this answer
add comment

You should think of panels as write-only. You bung your components on there, all set up and with the correct layout constraints. You (almost) never go and search through for components.

Instead, add the components to a Set (or similar) as you set up. Then you can do a very clean posh for loop over the collection to perform the appropriate task. A more advanced technique would be to have individual observers (listeners) refreshing the components from a model.

share|improve this answer
I would never consider panels to be write-only. You must have some pretty static UIs. I quite frequently add and remove components, e.g. I might use the decoration pattern to add/remove things to a widget to indicate an error or indicate something. Also, many applications generate their views dynamically based on data or may populate a manually created UI from data using bean properties or reflection. –  vickirk Feb 11 '10 at 16:26
"add" and "remove" are write/not-read operations. (I'd actually tend to hide/show rather that add/remove, although add/remove is useful for non-table tables.) –  Tom Hawtin - tackline Feb 11 '10 at 17:21
Sorry, I misunderstood your comment, when you said write-only I assumed you meant whack the stuff on the panel and thats it, I apologies. I like gridbags and find replacing what is it there already and replacing it gives required results. –  vickirk Feb 11 '10 at 22:43
add comment

The usual way is to use the JPanel as you suggested. Remember that the JPanel itself may have its own independent layout manager. So you may use a GridBagLayout on the JPanel to position the elements on the panel.

share|improve this answer
the only problem with this solution is that you can have an hard time to align outer and inner panels' components together... –  Guillaume Feb 11 '10 at 14:47
@Guillaume It will take some careful planning to create an effective layout. But the way the components are grouped in the panels and how the panels are placed next to each other will affect the appearance of the alignment that you mention. –  Vincent Ramdhanie Feb 11 '10 at 14:50
add comment

Your Answer


