This question already has an answer here:
Only one component shows up in JFrame
(1 answer)
Closed 4 years ago.
I am creating a tabbed pane with tabs in the WEST or a borderlayout and the content in the middle. This is working great the first time the menu item is click setting the content in the pane I want. But once I have clicked a menu item once, that menu item with no longer repopulate the pane in the middle. Below is my set active function;
public void setActive()
{
panelShowLocation.setAllMenuItemsAsInActive();
active = true;
setBackground(color_panelHover);
menuText.setForeground(color_textHover);
panelShowLocation.add(content, BorderLayout.CENTER);
//content.setVisible(true);
panelShowLocation.revalidate();
}
panelShowLocation.add(content, BorderLayout.CENTER);
panelShowLocation.revalidate();
When you add a component to the panel the existing component is not removed.
Swing painting logic paints the last component added first. So the newly added component gets painted, but then the old components gets painted over top of the newly added component
So you need logic like:
panel.remove( theOldPanel );
panel.add(theNewPanel, BorderLayout.CENTER)
panel.revalidate();
panel.repaint();
The other option is to use a CardLayout. The CardLayout allows you to add multiple components to the same panel. Only one of the components is ever visible at the same time. Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Related
I am trying to create a JTabbedPane that will always fill the top part of the Component with the tabs. Like so:
And this is how it look now:
And the code for this part:
JTabbedPane tab = new JTabbedPane();
tab.addTab("Items", items);
tab.addTab("Categories", categories);
setContentPane(tab);
//Also tried to create a JTabbedPane class and see if i could remove the labels and manually add two buttons to the top but without success.
I want the tabs to use up as much space as possible without actually shrinking the content of the panels.
So could anyone tell me how to customize the JTabbedPane, the look and feed or just the tabs themselves in order to do that.
The easiest approach is probably to create your own component:
Create a "main" panel that uses a BorderLayout.
Create a panel that uses a GridLayout and add your buttons to this panel. Then add this panel to the "main" panel using BorderLayout.PAGE_START. Each of these button will display a specific panel when clicked.
Create a second panel that uses a CardLayout. Add this panel to the "main" panel using BorderLayout.CENTER. Each of these panels will represent a tab.
The other option is to look at the TabbedPaneUI and find a method that paints each tab and modify the code for your requirement.
So I have a program that at the start only contains an 'add movie' button at the bottom of the frame.
Above it I inserted a scrollpane.
I also made a seperate JPanel form which contains labels and textfields where you have to input the data of the movie.
Every time I click the 'add'-button I want a form to appear inside the scrollpane (next to previously made forms).
So I figured I just needed to do this:
private void AddMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {
MovieForm movie = new MovieForm();
MovieScrollPane.add(movie);
}
But nothing new appears.
I tried validate() and repaint(), but so far these don't seem to work.
I made the interface in Eclipse btw.
Anyone who can help me?
Thanks anyway!
MovieScrollPane.add(movie);
Don't add components directly to the scrollpane. Normally a JPanel is added the the viewport of the scrollpane.
Then, whenever you add a component to a visible GUI the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
This makes sure the layout manager is invoked to the preferred size can be recalculated.
Also, follow Java naming conventions. Variable names should NOT start with an upper case characters.
I am creating a Swing based application , which actually consist of two buttons as shown below -
now what i want is when first button is clicked it must perform a action like showing a panel containing label, textfields , and some buttons in the same frame as shown below -
and when second button is clicked it will show another panel in the same frame as shown below ..
the thing is i am not understanding how to make this interface in action by providing event handler and action listener .. So please letme me know how can i archive this . Any help would be appreciated .. Thanks
There are 2 approaches.
CardLayout based.
Create all the panels (empty, panel with fields, panel with list) and add them in a container with CardLayout (the empty one is default). On click buttons swap visible cards (panels) showing necessary one.
Recreation based.
On click button create a new panel with new content. Remove the old one from container and add the newly created pane. After then call:
container.revalidate();
container.repaint();
I have a State Manager for a game that contains a stack of States. Now in one of my states ("Menu"), I have two JButtons in a JPanel (the JPanel is added to the main JPanel that displays everything). When I click the JButton "Play", I remove the Menu state from the stack. However, the JButtons stay on the screen (even though when i peek() I see that the Menu has been removed from the stack).
I don't want to remove the buttons from the State, because I want to be able to go back to Menu and see my buttons there again. How can I remove the buttons along with the state?
Have you considered using a CardLayout?
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.
Assuming each state corresponds to a single JComponent, you can use the .setVisible(false); method to hide the item when the state is removed.
In this case, when you initialize the main component, you'd want to make sure all components have been added - then when you add/remove states, you can just toggle the visibility.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How can I stack/overlay jPanels in Java?
I want to create a panel over another so that after clicking next button from first panel second panel gets open above it in same frame.
How can I do this using Netbeans GUI builder?
What you really want is a CardLayout , Have a look at this : How to Use CardLayout.
If you are using the Matisse feature of Netbeans, which uses GroupLayout, you may have to write some code to overcome your problem.
While creating your GUI keep both the panels on top of each other, keep the default visibility of your 2nd panel as false. Then when the button is clicked, set the 1st panel's visibility to false and change the 2nd panel's visibility to true.