aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes validate(), remove()- what do they REALLY do? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "validate(), remove()- what do they REALLY do?" Watch "validate(), remove()- what do they REALLY do?" New topic
Author

validate(), remove()- what do they REALLY do?

Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
When a swing-app is running, some action puts a new item into a frame's contentPane, should validate always be called on the contentPane??
As I understand it, validate() "recomputes" the
containers layout, and does what it needs show the user it's new contents.
Next: remove(Component c) setVisible(bool flag).
Does remove make the component never again, obtainable?? If it doesn't, is remove() the best way to "hide" something(then calling add()), or should it be setVisible(false)???
Maybe I should understand this from the API, but...no...
Anyone??
(Being a swing-beginner takes some patience, indeed)


Steffe
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
usually in awt times, after u added some components to the frame after it is visible, u needed to make sure to call validate() in order to "validate" (make sure) the components were added (if u didnt u needed to resize the window in order to see the components).
however, in swing it is rarly necessary and all components will be shown most of the time with no use of validate(). instead u can use sometimes revalidate() which is the swing version for validate() (but again rarly used in my experience).usually u will use setVisible(true) or false to hide or show containers like dialogs or frames. u can use it also for components.
remove() however removes the component from the container. that means the u can add it again later if u wish but for right now u removed it. (the opposite of add).
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
Thanks!
In my appl. I have 5 JPanels(each with a Jtable)
Only one should be visible at a time in the JFrame(using BorderLayout). So the best way to switch would be to simply keep adding/removing, or adding
all 5 and then using setVisible(). Still I little
unclear...
(I gave up with CardLayout, never worked)
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
...sorry, forgot...
if now, I use add/remove, should i reValidate()?
Or if using setVisible(true/false), reValidate()?
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
my suggestion to u is that if u switch the 5 panels a lot, use a cardLayout instead.
make another panel and set it to CardLayout.
add to it your 5 other panels and then u can switch easily!
usually it is not very good design to add and remove components all the time, but if u dont see the NEW panel after adding or removing it then use revalidate(). if u see them then u dont have to.
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
Could you please give me some sample-code of using
CardLayout correctly. I've tried with it for days,
but it never worked. I do see that's the way to attack the problem, but HOW!!!???
This is the way I tried: JFrame gets a JPanel cont. cont gets another 5 JPanels holding 5 JTables. Which component should be managed by CardLayout, and how???
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
here is what i came up with:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ranch extends JFrame implements Runnable
{
JPanel panels[];
JButton buttons[];
CardLayout card;
public Ranch(String t)
{
super(t);
card=new CardLayout();
getContentPane().setLayout(card);
panels=new JPanel[5];
buttons=new JButton[5];
for(int i=0;i<5;i++)
{
panels[i]=new JPanel();
buttons[i]=new JButton(i+"");
panels[i].add(buttons[i]);
getContentPane().add(panels[i],String.valueOf(i));
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setBounds(50,50,600,300);
validate();


}
public void run()
{
while(true)
{
card.next(this.getContentPane());
try{
Thread.sleep(2000);
}
catch(InterruptedException e) {}
}
}

public static void main(String[] args)
{
Ranch x=new Ranch("cool");
Thread y=new Thread(x);
y.start();
}
}
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
In your code JFrame is managed by CardLayout.
In my appl. I have several JPanels(in the JFrame),
managed by BorderLayout. I mean, in my case CardLayout should NOT be "used" by the JFrame, but
of one of it's panels(BorderLayout.EAST in the frame). Should this make a difference?
Obvious difference: JFrame-(getContentPane.setLayout(card))
JPanel panel...panel.setLayout(card...).
In frames case you add the manager to the frames contentPane, in panels case you "just" add it.
Do these two concepts differ???(panel has no container)
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
no, its exactly the same.
panels are containers just like frames, so u have no problem.
just add the cardlayout to the panel directly (without the content pane thing).
its actually easier your way !
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
Thanks a lot!!!
Quick responses to problems and serious discussions make the Ranch the BEST!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validate(), remove()- what do they REALLY do?