I have one main JPanel container and three JPanels inside. How to empty this panel and add new panels? I tried with remove(Component) but it doesn't work. Can anybody give me advice?
This will do it. The trick is to call revalidate.
mainPanel = ...
mainPanel.removeAll();
mainPanel.add(newPanel1);
mainPanel.add(newPanel2);
mainPanel.add(newPanel3);
mainPanel.revalidate();
But really, consider using CardLayout, if you want to change what appears in a JPanel.
Here in this link i found a simple tutorial on how to add and remove elements from panels.
The other panels inside your main panel, are also elements, so the same principle applies to them.
A good practices when adding something new in the panel is not just to use the method add():
we might also want to use revalidate() and repaint() They should be called when some event occurs(button clicked or similar...)
Also i want to mention that in the tutorial remove() i being used to remove elements, you are doing it corretly. Maybe calling again revalidate() and repaint() for the other panels make the removed panel dissapear from the GUI(The object is deleted just the GUI is not refreshed)
Note: I suppose that the elements of your inner panels are visible = true. If some of the inner elements struggle to render try to call also revalidate() and repaint() at them.
I think this way should work.
# Harry Joy
if you added or removed (already visible container) then you have to call
revalidate();
repaint(); // not required in all cases
# Damir
if JComponents isn't public (or private) static then you can just call
myContainer.removeAll();
myContainer.revalidate();
nyCOntainer.repaint();
possible is remove JComponent(s) by some parameter(s) with Component[] a = myContainer.getComponents(); then you could call if (components[i] instanceof JComboBox) { ...
Try the other remove method remove(int index);
this works 100%
this.panelname.Controls.Clear();
I too had the same problem. All I did to resolve the issue was
panelName.setVisible(false);
mainPanel.remove(panelName);
In my case, panelName is a JPanel which lies inside mainPanel.
Related
The following method has the end result I need. The problem is, when the method is called, the starting pnlMain stays visible until the new pnlMain is created and replaces the original.
The point of this method is to change the panel by creating a new one but this process takes a little time, so I am trying to have the "load" panel show up during that time.
public void changePanel() {
remove(pnlMain);
add(load);
repaint();
pnlMain = new HunterPanel(settings); // HunterPanel extends JPanel
remove(load);
add(pnlMain);
repaint();
pnlMain.requestFocus();
}
"Changing Panels during runtime"
The correct way is to use a CardLayout that will allow you to switch between views. See How to Use CardLayout for more details. See a simple example here
But just to let you know where you're going wrong in your code, if you remove and add components at runtime, you need to revalidate(). But in your case, go with the CardLayout. After you learn it, you be happy you did :-)
I have made a simple GUI using a GridLayout(5,3) , it is action performed and it implements action listener as well. The are some calculation and algorithms that working according to what inputs or buttons the user provides. Everything works just fine up to this point.
At some point in my code, the user gets a pop up massage that he is correctly logged in to the system using this common method JOptionPane.showMessageDialog(....) . All i want is, after he press the OK button, is to create an additional form that pop ups, and looks similar to the one above i made with GridLayout(5,3) so that my user can store additional info about him.
I really cant get it to work, and i have no idea how to start this.
Any ideas are very welcomed! Cheers and thanks in advance :)
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You state:
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You have at least three options if you want to swap "views" on the JFrame.
If you want to use the same GUI with the same JTextComponents but have the components empty of text, then you'll need to go through your text components and call setText("") on all of them. If you want to keep the same JButtons and labels but change their text, then similarly you will need to go through all of them calling setText("something else").
If you want totally new components to replace the old ones, the most straight forward way I believe is to use a CardLayout to hold your JPanel that has all your components. When you want to swap the JPanel for another, make sure that the new JPanel has been added to the CardLayout-using JPanel and then call next() on the CardLayout object.
Another way is to manually swap out JPanels held by the JFrame's contentPane by calling removeAll() on the contentPane, then add(nextJPanel) on it, then revalidate(), then repaint().
I have a JFrame inside of which is a jpanel that im using as the content pane.
So basically im using the jpanel to load content into on click. New content is returned as a Jpanel also so its ends up being jpanel -> inside jpanel -> inside Jframe. When i need to load in new content i clear the panel, load the new content and validate() the jframe & jpanel and the new content displays.
My problem is that when the new content displays its clear that the validate method is working because i can see the new interface but i can also see the old interface as if its become the background; i can resize the window and it just disappears and looks as it should.
Is this just the way validate works or can i fix it?
Edit: this worked. The problem was i wasn't calling repaint manually.
public BaseWindow setContent(JComponent comp){
contentPane.add(comp);
contentPane.revalidate();
contentPane.repaint();
return this;
}
Generally the code for adding/removing one or two components from a panel is:
panel.remove(..);
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
However, if you are replacing all the components on the panel, then the better approach is to use a Card Layout.
You have already stated the revaliate() followed by repaint() doesn't work so the only suggestion I have is to post your SSCCE that demonstrates the problem.
Don't use validate. Use revalidate() instead.
Revalidate first calls invalidate() followed by a validate().
In Swing, you would rarely use validate().
Note: I also feel that maybe the old panel is not cleared/removed.Check again!
Validate() is for causing components to re arrange themselves according to the layoutmanager that you have installed. This is not really what you should be using.
I can't see your code, so I'm not sure exactly what you are doing. I could speculate that calling repaint() on your "inner panel" will solve the problem you are having...but really, if you are doing things properly, you shouldn't need to call repaint() or validate().
Make two JPanels, one with content A (e.g. your buttons), and one with content B (e.g. your "static" field). Use the "add()" and "remove()" methods on the parent container (the JFrame's content pane?) to swap these two JPanels with each other whenever you want to switch the content that is displayed in that part of the JFrame.
Then you shouldn't need to do anything else; it should just work.
I don't know if validate() makes any promise about fully repainting the container. You might have to call repaint() yourself to make it behave as you want to.
Here's another possible solution:
Put both JPanels in at the same time, side by side, and then make sure only one of them is ever visible at any one time:
JPanel p = new JPanel(new BorderLayout());
p.add( panelA, BorderLayout.EAST );
p.add( panelB, BroderLayout.WEST );
panelA.setVisible(true);
panelB.setVisible(false);
Then when the user clicks the button to switch panels:
panelA.setVisible(false);
panelB.setVisible(true);
The setVisible() method and BorderLayout should take care of validating, layout, and calls to repaint() for you.
I ended up fixing my issue (display not shown, buttons would stay clicked/weren't unclicking) by changing which panels were added/removed.
Problem:
frame.removeAll();
frame.add(getNewPanelDisplay());
frame.revalidate();
frame.repaint();
Solution:
//initializer()
mainPanel = new JPanel();
frame.add(mainPanel());
// ...
//update()
mainPanel.remove(0);
mainPanel.add(getTablePanel(), 0);
frame.revalidate();
frame.repaint();
I have a JPanel called parentPanel.
Also I have other sonPanels in parentPanel.
I want to remove the sonPanels and add them in an arraylist<JPanel>
Can you help me??
Thanks in advance!!! :)
Since JPanel inherits from Container, you can use the getComponents() method to get the list of your sonPanels.
After getting them all, you can clear you parentPanel by calling the removeAll() method.
If you had a deleteRows method, simply call the first method on your JPanel, lets call it contentPane, and then call the second method to remove.
public Component[] getAndClearSonPanels() {
Component[] currentComponents = contentPane.getComponents();
contentPane.removeAll();
return currentComponents;
}
If you need to traverse even more deeply into each of the JPanels, you would need to, recursively do so.
private JButton btnTask = new JButton();
...
TaoGlobal.taskbar.add(btnTask);
How to remove btnTask from JToolBar?
Thanx.
i tried remove, but forgotten for
repaint
Well the general code should be:
panel.remove(...);
panel.revalidate();
panel.repaint();
The revalidate() is important because it tells the panel to layout the components. Your code may work if your are removing the last component, but I doubt is will work when you remove the first component.
JToolBar is a Container, and hence removal can be achieved via toolbar.remove(btnTask).
If you look at that javadoc you'll see other useful methods, like remove(index) and removeAll().
Maybe this would be useful for you:
http://java.sun.com/docs/books/tutorial/uiswing/components/toolbar.html
and
http://java.sun.com/j2se/6/docs/api/javax/swing/JToolBar.html
The last link shows you all the methods that you can use.