I try to do the following thing:
- Add a component to a
JFrame
with aBorderLayout
- Add another component to the
JFrame
I would expect the new component to 'overwrite' the old component, since I'm using a BorderLayout
. This works if I overwrite the old component before I call pack()
. Now if I add the 2nd component after I call pack()
, both components remain visible. Example:
public class Test extends JFrame{
public Test(){
setLayout(new BorderLayout());
add(new JLabel("Quite a long text"));
setVisible(true);
pack();
add(new JLabel("Another text"));
}
}
Result:
public class Test extends JFrame{
public Test(){
setLayout(new BorderLayout());
add(new JLabel("Quite a long text"));
setVisible(true);
add(new JLabel("Another text"));
pack();
}
}
Result:
I tried adding validate();
and repaint();
, but that wouldn't help. What's going wrong here?