I have an application where I want that the user is able to choose between normal and advanced settings. Now if the user checks a JCheckBox and the advanced settings should disapper the problem starts.
My idea was to set all unnessecary swing components (JScrollPane, JLabel...) invisible and then find a method of JFrame which fits the window to the VISIBLE components.
My question is if there is such a method?
... and then find a method of JFrame which fits the window to the VISIBLE components. My question is if there is such a method?
Yes, there is such a method, and it is called pack().
This will cascade through the layout managers of all the containers held by the top-level window, asking them to re-lay out their visible components, resizing components to their preferred sizes as based on the components and the layout manager requirements, and eventually resizes the top-level window to fit the containers and their components.
1. You can use setVisible(boolean b), to make the component visible and invisible.
2. You can check that if the component is visible or not using isVisible()
3. You can then use the pack() method, pack() method gives sets the frame size as per need
I think you can wrap the advanced content in a panel (if possible) and remove that panel from frame using this.remove(component) then use this.pack()
and you can do the opposite on showing them, this.add(...) then this.pack() again
Related
I am now writing code simple GUI that's for start the game window. I only need Do you want to start game message and start button on the window. But I have a confusing concepts for the JFrame and JPanel. Actually, I thought I need to add JPanel to JFrame to add the other components such as JLabel, JButton,...etc. But I realized I don't actually need JPanel. I can just add the components simply use add(button), add(label) to JFrame. So why I need JPanel. And I think JFrame doesn't need JPanel but JPanel need JFrame. Am I understand correctly?
No, not always. A simple graphical user interface may be implemented by just adding components "directly" to a JFrame. But in order to get more flexibility, you would always use JPanels. For example, to employ different layouts in different parts of the GUI, to group certain components together, etc.
A JFrame is backed by a JRootPane, a part of which is a contentPane.
(image from Oracle Javadoc)
When you add components to a JFrame, you are really adding them to the content pane, e.g.: frame.getContentPane().add(Component).
A JFrame is a common starting scene of a Swing GUI application, while a JPanel is intended to be put in another scene (container). Since both content pane and a JPanel inherit from the same class (Container) you may use them in a similar manner, as far as adding components to them goes.
Do I need JPanel always?
No. Well, unless you need a Swing GUI. Then yes.
Another answer replied words to the effect. "No, you can add components direct to a frame" What they missed was that components added to a JFrame are added to the content pane (automatically). The content pane is a JPanel.
Having said that:
I (and many others) would recommend designing an app based around a main content panel, then adding that panel to a top-level container as needed. The top level container might be a JFrame, JWindow, JDialog, JOptionPane ..
What prompted the question? A JPanel is a very 'light weight' container (in more ways than one). A GUI can contain 1000s and not be burdened by doing so. Of course, that's a rare requirement, but just saying .. use panels as needed and don't worry about it.
How to make my JFrame auto-resizable? Size depends on existing components in the frame, but the user can add more components dynamically.
You could use the JFrame.pack() method, that according documentation:
Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents.
See the Nested Layout Example for both dynamically added components (labels added using a button - in a scroll pane), as well as a resizable GUI.
When is the size of the JComponent is calculated? After being shown in the screen or before that?
if I send .getSize() message before .setVisible(true), would it give me the right answer?
Thanks
I sometimes check the sizes of my components when debugging to find out why I can't see them for instance. In most cases, the sizes will be realized when the GUI has been rendered. This can occur when pack() or setVisible(true) has called on the top-level window. My usual sequence of method calls is to call pack() first as this tells the layout managers to lay out the components that they are responsible for, and sets the sizes of the components and the GUI, then call setLocationRelativeTo(null) to center my GUI, then call setVisible(true) to display it.
The layout manager is responsible for determining the size of a component, so you don't know its actual size until the component has been added to the frame and the frame has been pack()ed ore made visible.
If you use a layout manager that respects the preferred size of a component then you can use:
component.getPreferredSize();
Why do you think you need to know the size? Generally you don't worry about sizes and let the layout manager do its job.
In addition to the usual pack() > setVisible(true) > getPreferredSize() sequence, you can validate() the relevant Container to preview the geometry, as shown here.
If I understand properly, the reason why you want to know the size of a component is to reset the size of a JWindow once a user click on the "More options" button, isn't it?
I would suggest to do the following: when the user clicks on that button, update your UI adding the extra component, and the execute pack() on the JWindow. It should resize to the proper size.
I have a JPanel with one component that I want to place in an absolute sense, whereas the rest of the components are placed according to a layout manager.
Is there a simple way to do this?
Are you saying you want a component painted over top of all the other components? If so then you would need to use a JLayeredPane.
Why don't you post a SSCCE that demonstrates what you want to do?
You can add components to a frame as you would do normally and make the frame visible. Then you can add this random component and use setBounds on the component. As long as you don't revalidate() the panel or resize the frame we will be able to see how you intend to position this component relative to all the other components.
You might also want to look at OverlayLayout, seen here. For some reason it's excluded from the conventional gallery, but it may be of interest.
You can do this with only needing one JPanel using MigLayout
I'm building a GUI application, and within a JFrame i have 2 jcombobox's and a JPanel to view certain data. Now when i call the pack() methode in the main class it puts the two jcombobox'es next to my JPanel, which i dont want, because I want them North. Ofcourse I've tried to hard-code it in my code, but it doesn't work after I've called the pack() method.
Are there any alternatives to this method?
Only one component can be NORTH, so if you want both ComboBoxes to be NORTH you have to add them into a separate container. This separate container can then be put NORTH.
(Post the source for more exact help.)
All pack does is resize the Window (in this case JFrame) to its preferred size and the preferred sizes of its sub-components. To control the actual location of the sub-components relative to one another you need to use an appropriate LayoutManager.
You might want to check out the Using Layout Managers tutorial.
The pack() method just causes the layouting to happen, it has abolutely nothing to do with what is put where.
Most likely you're not using layout managers correctly. Show us your code and we can tell you waht exactly you're doing wrong.
You can avoid using pack by explicitly setting the frame size with setSize and setBounds. However, using pack is usually the preferred way as it leaves the frame layout manager in charge of the frame size.
That being said, the problem you are describing appears to be related to the correct use of a layout manager rather than the sizing of the frame. Have a look at the various layout managers for Swing and how to use them: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html.