Group java components - java

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!

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.

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.

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.

Related

JFrames inside JFrames

I currently have build an application where I use multiple frames.
But it would be nice if I could use the frames I used all in just 1 frame.
Like in the image below.
So if you press the left button "Speler Overzicht" that it will show the users in the right panel and I still have my buttons in the left panel.
Generally speaking, it's a very bad idea to base you UI classes on JFrame, as it locks you into a single use case, meaning you can't add the UI component (frame) to other containers.
I better solution is to base your UI components on JPanels, which then allows you to add them to where ever you need them. It also makes life easier to extend them, but that's another story.
To allow the user to move between multiple views, you can use either a CardLayout or JTabbedPane depending on your needs
See How to Use CardLayout and How to Use Tabbed Panes for more details
Use JPanels instead.
buttonPanel=new JPanel();
overzichtPanel=new JPanel();
buttonPanel.add(button);// do this for every button
overzichtPanel.add(componentsYouWantToAdd);// replace with your variables of course
frame.add(buttonPanel, BorderLayout.WEST)
frame.add(overzichtPanel, BorderLayout.CENTER)
You cannot put one JFrame inside another. You have a some design choices here. You can change your JFrames to JPanels. This is probably the easiest change. On the other hand, you can look at using Internal Frames instead.

How to change visibility of ALL items in a JFrame at once?

I wonder whether there is a possiblity to change the visibility of more than one item (textbox, button, chart ...) in a JFrame in one simple(?) command.
Thanks for answers and ideas!
(Hiding the whole JFrame is no option ;))
You could use a CardLayout, as shown here.
Obviously, one of the two panels shown in the demo would have no components.
Place all into a panel and hide/show the panel. That should propagate to all child components as well
Place components into Collection, then you can use simple iteration to set/clear any flags. You need to create a Collection and add objects to it, but this allows to separate visibility control from the component layout.

Auto resize swing elements to fit to the container's size

Not sure if what I need is possible.
I have a container (JPanel) that contains some internal elements.
I was wondering if it is possible to force internal elements to fit into the container's size.
I need them to be fully visible i.e., resize to fit inside the Panel's size and not cut some parts of the internal elements.
Scrolling is not an option.
Is this possible by using a Layout or something?
EDIT: Important clarification:
The thing is that I do not have access to the internal elements neither to their properties so I would say that a Layoutmanager capable of resizing child elements to fit to its size is needed. I tested BorderLayout and GridBagLayout but the result is always the same, the internal elements are cut out.
It's for exactly that reason that LayoutManagers exist. All the LayoutManagers work for simple containers directly, excluding GridBagLayout which is to able to handle most complete GUIs directly.
For most complete GUI's you have some choices as follows:
Look for a 3rd party layout such as MigLayout or here
Use GridBagLayout
Very easy way is use nested layout, where there is more than one JPanel and each has child JPanels with the same or different LayoutManager
Or custom layout, should be hard..., but same as using GridBagLayout
You could set the JPanel layout to border layout, then add the single child to the center. If there are multiple children, this approach becomes less useful since components added to the the NORTH, SOUTH, EAST, and WEST will remain statically sized while the centre resizes to fill the remainder.
In short, this isn't an ideal solution. All layouting in Swing is made all the more complex by the fact that different components behave in different ways, so you really need to provide further details of the child components you wish to add to your panel, and any behaviour that has been overridden on those components.
The best way is to try a couple of simple examples to see what mileage you get and whether subtle redesign of your child component nesting could help.
you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.
this sounds to me like you should just peek an appropriate layout manager and use it. For example, look at BorderLayout - put your component in the CENTER and it will occupy all the area. Its up to each concrete layout manager to decide what will be the size of the components.
Mark
I was using a JInternalFrame inside JDesktopPane. I wanted the internal_frame to auto resize as desktop pane is resized, so I had to implement the AncestorResized event for the internal frame where I placed the following code:
this.setPreferredSize(this.getParent().getPreferredSize());
this.pack();

Adjusting positions of swing components

I have a JFrame in which i have to insert JLabels, textfields and JButtons. I am able to these but how can i adjust them to the required position, i want to add one label and textfield in one row and then nxt label and textfield in the next row but they are coming in the same horizontal line. i have used flowLayout with the JFrame. please tell me how to adjust them accordingly. thanks
The key to distributing components in a Container in Swing is the Layout Manager. There are various types out there. To do what you are looking for, you might want to consider the GridLayout. It is pretty easy to set up. You first need to create the layout. The following will create a two columned layout with as many rows as you provide:
GridLayout gl = new GridLayout(0,2);
Then you apply it to your panel:
JPanel panel = new JPanel(gl);
Then you add your items:
panel.add(textfield1);
panel.add(button1);
panel.add(textfield2);
panel.add(button2);
The GridLayout will handle moving from row to row after you fill in the columns with components.
had a look at gridbaglayout? Should serve your purpose.
A GridLayout may be what you want, or a combination of a GridLayout and a FlowLayout. Look at the LayoutManager tutorial to get a better idea of when and how to use and combine the various layout managers.
You need to study the various types of layouts swing provides.
Also you can have a look at FormLayout,provide by JGoodies. I prefer to use this than swing layouts as i find it easy to code and less lines of code
You are using the default Swing Layout Manager. If you want different behaviour (which is very reasonable) then you need to use another LayoutManager. Several exists both from Sun and "out there".
In order for you to be able to choose, you need to know how they work. I can strongly recommend using the Java Tutorial for this:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
Let us know if you need more than provided by e.g. nested BorderLayout's or TableLayout.
If you want to be able to position the UI elements in your application (nearly) absolutely, consider using a decent GUI builder like Matisse in NetBeans or Swing UI Designer in IntelliJ IDEA.

Best way of subclassing a JPanel in Swing

I am currently trying to build an expanding panel in Swing (akin the WPF's Expander control) and I'd like to retain the usual methods for manipulating it (i. e. setLayout, add, etc.). Only they should be routed to an embedded panel (the one being shown or hidden).
How would one do that? Overriding every method of JComponent and re-routing that to an embedded JPanel would be cumbersome, but that's the only way I see.
Or should I rather make the embedded panel visible to the outside and force users to use something like ExpanderPanel.getInnerPanel() instead. But then it's no drop-in replacement for JPanel which I think would be nice to have.
Take a look at the JXTaskPane from Swingx project. It already does what you need.
In 1.5(ish) Swing routed a few methods to the content pane in JFrame, JApplet, etc. Whilst there appeared to be some usability benefits for those just starting, it doesn't actually fix the problem. So everyone has to deal with a very strangely behaving API. So my advice is to avoid this approach.
If you have a Container widget which holds a panel you want to show and hide, why not layout your inner panel however you want, then add it to the Container panel, then use static methods against the Container to say
JPanel p = new JPanel();
//do something with the JPanel...
ContainerWidget.setContent(p);
ContainerWidget.expandPanel(p,true);
Would somethign like this work?

Categories