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

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.

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.

Java: single frame vs multiple frames

Think about the classic installation process, where you have a "next" button and when you click it the content of the window changes. To represent this situation I thought of two possible solutions:
-when "next" is clicked destroy the current JFrame and create a new JFrame, maybe passing to his constructor useful information (e.g. actual window size, content inserted by the user in the current frame, ...)
-when "next" is clicked remove all the components from the current JFrame and add new components as needed
The first solution looks way better about OOprogramming, because I can keep separate classes for different frames and I can avoid huge methods that empty the frame and repopulate it. However the first solution sounds a bit "dirty" and I should pass lots of parameters to the new frame. To represent this situation I would choose the second solution.
Now think about a menu with an "option" component: in this situation I would create a new JFrame when "option" is clicked, so that I can populate it with option items. Is this a correct solution? Is there a way I can always know which one is the best solution? Are there any solutions I didn't think about?
Destroying the main JFrame would be silly -- not to mention jarring for the user. Just use a single JFrame and change its contents.
To implement an installer wizard, use a single JFrame containing one large JPanel on top and a smaller one containing the "Next", "Back", "Cancel" buttons along the bottom. When the Next or Back buttons are pressed, you replace the large JPanel. You can have many different JPanel subclasses, one for each "page" of the wizard.
There's a LayoutManager called CardLayout which is ideal for implementing this scenario -- it manages a "stack" of components, and only shows one of those components at a time. Use a BorderLayout in the JFrame. Into the center position put a JPanel with a CardLayout. Then add the individual pages of the wizard to that JPanel, so the CardLayout can manage them.
The CardLayout is well suited for this. You just swapout the JPanel contents when the "Next" button is pressed.

Refresh Panel on CardLayout Swap

How do you refresh a JPanel on the panel change from container CardLayout?
Use the show() method. From the Java API:
Flips to the component that was added to this layout with the specified name, using addLayoutComponent. If no such component exists, then nothing happens.
CardLayout#first(), next(), and previous() do something similar.
Sometimes, when I've made a panel swap like this (though not that I can remember on CardLayout, but it's been a while since I used it), I've also needed to pack the frame again. If that doesn't work, you can call revalidate(), which lets Swing know that the component needs to be redrawn.
You may also want to consider using a tabbed pane, as it does a lot of this for you; I started out a project trying to use CardLayout and decided to use a the tabbed pane instead. Depends on what you want to do, of course.
Is there an actionlistener or something that I can have it reload/refresh my data on that screen?
Assuming you have a model that supplies data to your view (panel), two approaches are common:
Arrange for your model to extend Observable and your view to register as an Observer.
Arrange for your model to manage an EventListenerList, in effect creating you own analog of ActionEvent.
In either approach, use the listener of the control that switches views tell the model to update its registered observers/listeners. The example in How to Use CardLayout uses a JComboBox and itemStateChanged(). There's additional discussion here.
Is there an actionlistener or
something that I can have it
reload/refresh my data on that screen?
You can use an AncestorListener. It fires when a component is added to a Container. So when you swap cards the event is fired. You would need to add the listener to each panel you add to the CardLayout:
Another problem with the CardLayout is that the panel doesn't gain focus when it is swapped. I use this approach to set focus on the panel. Check out Card Layout Focus.
panel.addAncestorListener(...);
The show() method does the trick of switching panels, but it doesn't "refresh" the data. Is there an actionlistener or something that I can have it reload/refresh my data on that screen?

Java Swing group of Jbuttons without raised portion

I wanted to know how to display a group of JButtons to look like smooth panel without raised portion.
thanks
button.setBorder(null);
You may want to look at some of the other "setXXX" method that control painting as well.
I've often just used standard JLabels and added mouseListeners to make them clickable. Alternatively, you could get more advanced and create your own ButtonUI class if you want to really fine-grained control over the rendering of the buttons.
If you want the buttons to be in a row, you can put them in a JToolBar and set Rollover to true. This will make flat buttons that, with mouse over, look raised.

Group java components

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.

Categories