-
Enhancement
-
Resolution: Won't Fix
-
P4
-
None
-
1.1.5, 1.3.0, 5.0
-
generic, x86, sparc
-
generic, solaris_7, windows_nt, windows_xp
Name: rk38400 Date: 04/17/98
Dynamically removing a component from a JPanel and adding a different component causes the panel to display
both components on top of each other until the panel is subsequently repainted.
Run the attached program, each time you press the "Swap Components" button it replaces the component in the JPanel.
The first time you press it, a ComboBox is displayed. Pressing it again replaces the combo with a label - but the label is
drawn on top of the combo.
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class RedrawTest extends JFrame {
public static void main(String args[]) {
JFrame frm = new RedrawTest();
frm.setSize(300, 300);
frm.setVisible(true);
}
public RedrawTest() {
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JComboBox jcOne = new JComboBox(new Object[] { "Item One", "Item Two", "Item Three" });
jcOne.setSelectedIndex(0);
final JLabel jcTwo = new JLabel("Label with text here");
final JToggleButton btnSwitch = new JToggleButton("Swap Components");
getContentPane().add(btnSwitch, BorderLayout.SOUTH);
btnSwitch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.removeAll();
if (btnSwitch.isSelected()) {
panel.add(jcOne, BorderLayout.CENTER);
btnSwitch.setText("Swap Components (current=Combo)");
}
else {
panel.add(jcTwo, BorderLayout.CENTER);
btnSwitch.setText("Swap Components (current=Label)");
}
panel.validate();
}
});
}
}
(Review ID: 28584)
======================================================================