I have problem with panels. I have one main panel main_panel, at top I have ComboBox,
based on selected item in combobox I need to show one of two panels under (bith have lot of buttons on positions x,y. if selected_item_1 then panel_1 visible,if selected_item_2 thenpanel two visible). How to add button to child_panel on exact position locX,locY?
CardLayout is ideal for this type of behaviour.
A CardLayout object is a layout
manager for a container. It treats
each component in the container as a
card. Only one card is visible at a
time, and the container acts as a
stack of cards. The first component
added to a CardLayout object is the
visible component when the container
is first displayed.
It's a job for CardLayout. besides, if content must change according to a selection, maybe you should go the JTabbedPane way ...
Related
I want to design a jFrame where there are three jButtons and a set of three jpanels one above the other. When we call a jPanel from the respective jButton , that pane will be displayed. It would appear to the user as if the same portion of the jFrame is displaying the content to be shown on clicking each jButton.But when i am trying to set the jPanels one above the other, they are being shown side by side thus elongating the jFrame horizontally. What should i do to put one jPanel over the other? Any other idea than jPanel which should do the work i intend to do would also be help !!
Your behavior sounds like you are using a FlowLayout. This will not "layer" anything. Instead us a CardLayout, which does exactly what you are trying to accomplish. You call method like show, next, and previous to navigate the panels. See How to Use CardLayout for more details.
Also there are probably hundreds of other examples here on so. Go through the cardlayout questions.
[Tip: navigate the different tabs like "votes" and "frequent" to filter to some of the better posts]
Here's one that uses the show() method to switch between two panels by name.
I'm pretty new to programming Java based GUI applications. Here's what I have in mind, I want to divide the window into two areas.
The first are contains buttons (or a list), and based on which button was clicked, or which item was selected, the second area changes. (finite number of buttons)
Something like this:
I can think of many ways to do this, but I am not sure what is the best practice. Do I have several invisible panels and make only 1 panel visible at a time, or do I change the ordering (bring panel x to front), or is there some other way?
Appreciate any help I receive!! Thanks in advance!
Although this is a primarily opinion-based answer I'd go with a Nested Layout approach:
Main panel with BorderLayout. Or you can use the frame's content pane which already has BorderLayout as default layout manager.
Left panel with BoxLayout (or GridBagLayout).
Right panel with CardLayout.
Note: Buttons in the left panel should switch right panels cards.
See Lesson: Layoing Out Components within a Container tutorial.
For complex GUIs you have also third-party layout managers available, listed in this answer:
MiG Layout
DesignGridLayout
FormLayout
I have a problem where I have several similar JPanels, that are contained vertically in a main JPanel. My issue comes where I will have buttons that will only interact with the child JPanel that is currently selected (clicked on).
I have a controller that takes the main JPanel, how can I have a method that will return only the selected JPanel?
This is very difficult. By default, JPanels are not focusable (they can't receive keyboard focus).
You could try ascertain the current panel that contains the current focusable component by using KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); and using the resulting Component's getParent method, but this is no guarantee, as the focused component may be contained in another container, contained within the container your interested in...
A better idea might be to have some kind of model that connected the buttons or actions to the child panels...?
I have two JPanels that needs to add a JPanel into it, however only the last added JPanel does it show the JPanel. As shown below:
holderPanel1.add(dataPanel);
holderPanel2.add(dataPanel);
only holderPanel2 shows the dataPanel, but holderPanel1 does not show.
UI Components (such as JPanel) are the underline representation of things you see on the screen (location, parent, sub-components etc.), so each panel that you see on the screen has to have a separate underline representation, so you cannot add a panel to two different panels, you need to create two separate panels.
So I am working on a GUI application using the swing framework. In short, I have 3 JPanels that act as different views of my application. Now the problem is that no matter the order I add the JPanels to my JFrame, only the final JPanel I add resizes when I switch to that view.
Some relevant bits of code:
When creating the window, I first create each individual JPanel, and add it to the JFrame:
JPanel newPanel = new SomeClassExtendingJPanel();
this.jframe.add(newPanel);
Next, whenever I switch between views of the application, I hide the panel that is currently active:
jframe.validate();
jframe.repaint();
oldPanel.setVisible(false);
And then activate the to be shown panel:
jframe.validate();
jframe.repaint();
newPanel.setVisible(true);
Does anyone know what could be wrong?
To resize the JFrame with each swap, you could call pack() on it, but this is kludgy having a GUI resize all the time. A better solution is to use the mechanism that Swing has for swapping views -- a CardLayout. This will size the container to the largest dimension necessary to adequately display all of the "card" components.
Check out the CardLayout tutorial and the CardLayout API for more on this.
JFrame's ContentPane has implemented BorderLayout by Default, and there is possible to put only one JComponents to the one Area,
you have to change used LayoutManager or put another JPanels to the other Areas