Java group components

I am creating a GUI in Java using the GridBagLayout . Is there a way to create a group of components so that I can pass a link to the group and have access to all of them?

I considered creating a panel and grouping components in this way, but I was wondering if there is another way that uses the complexity of GridBagLayout .

Many thanks!

+4
source share
3 answers

Well, if you cannot create a bean with JLabel, JTextField and JButton to host your group, you can always use Map in your main ui panel to register items when they are added. Some structure like

 Map<K, List<Component>> 

may work, where K is the group identifier. This way you separate the components from how they fit in ui.

+3
source

You should think of panels as recordings. You clog your components there, everything is configured and with the correct layout restrictions. You (almost) never go in or view components.

Instead, add components to Set (or similar) as you configure. Then you can make a very clean posh for loop over the collection to do the corresponding task. A more advanced technique would be for individual observers (listeners) to update components from the model.

+2
source

The usual way is to use JPanel as you suggested. Remember that JPanel itself may have its own independent layout manager. This way you can use the GridBagLayout on the JPanel to place items on the panel.

0
source

Source: https://habr.com/ru/post/1301003/


All Articles