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.
Related
i'm a bit confused about the behavior of my java application :)
A JPanel has an AncestorListener in which a method x() is called if the ancestor is added
The JPanel is added to a JScrollPane as the first tab
The JScrollPane has another AncestorListener
If the scroll panel gets visible, the method x() is executed twice
The first call of x() is triggered by adding the panel
The second call of x() is triggerd by showing the panel
The confusing part is, that the second call is caused by the AncestorListener of the ScrollPane, even if the listener has an empty implementation.
There is no second call, if i remove the listener.
Can anyone explain how a listener with an empty implementation can result in a method call, or how i can debug/profile this problem?
Thank you in advance
Best regards
Okay currently I am trying to make a multi-window program.
And from seeing other forums, it seems for you to do that in Java JFrame you must update its content pane by adding the new JComponent(new window/layout/idk), set the current window visibility to false, set the new one visibility to true and validate &/ repaint content pane:
contentPane.add(newWindowPanel);
currentWindowPanel.setVisible(false);
newWindowPanel.setVisible(true);
contentPane.validate();
contentPane.repaint();
now, what I am trying to do and partially have done is created a class that extends JPanel, and this class stands to be the top of an hierarchy for many other JPanel classes that I am going to create.
Within that class I have this method :
public void updateContentPane(Container contentPane, JPanel currentPanel, JPanel nextPanel){
contentPane.add(nextPanel);
currentPanel.setVisible(false);
nextPanel.setVisible(true);
contentPane.validate();
contentPane.repaint();
}
When I call this method within one of the child classes, it doesn't work.
updateContentPane(WindowMain.contentPane, this, mainMenuClass);
Each of the child class inherits the JPanel characteristic.
"WindowMain" is a class that extends JFrame, and "contentPane" is a static container variable that holds the frame contentPane.
"this" represents the current class (inherits JPane), but "this" don't actually work new Object() works.
"mainMenuClass" also inherits JPanel and has already been instantiated in this class.
My goal is to simply jump from one scene to the other by calling that method. But it goes through the code (debug) but nothing happens. But, if I take the code within the method and place it inside a button listener it works fine.
(Sorry for all this writing, it will probably bring some confusion at that, but I need to figure this out nonetheless, and I will set condition for when the contentPane already contains a class, so no need to mention it)
You could just update the whole frame by doing
frame.revalidate();
frame.repaint();
I have a class MyPanel that extends JPanel.
MyPanel is a child panel: his size is dinamically calculated by the LayoutManager of his parent.
The problem is that I need MyPanel to be notified as soon as the LayoutManager of his parent calculates his size.
It needs to be notified because I need to initialize some variables according to the panel's size.
If I call getSize() in the MyPanel constructor i get (0,0) because the panel is not "ready".
When should i call getSize()?
I'd like to have something like this:
/** Lays out the panel and sets his size according to his layout */
#Override
public void onReady() {
System.out.println(getSize()); //output is (0,0);
super.onReady();
System.out.println(getSize()); //output is (600,500);
//here i can initialize my variables
}
Is there something similar? Maybe doLayout()?
The only way i could find is calling getSize() in the paintComponent() method... it surely works because the panel is surely displayed when paintComponent() is called but I can't do this because i need to initialize some variables as soon as i know the panel size... but only once! paintComponent() will be called several times...
It's ComponentListener.componentResized().
Best way is to use a ComponentListener.
See ComponentListener.componentResized(ComponentEvent)
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.
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();