Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

add/remove components in layout

843805Jan 4 2006 — edited Jan 4 2006
I have some painting problems when I try to remove a certain componen from my container and add a new one.
I tried invoking the doLayout() and repaint() method wich seem to repaint the frame properly but whenever I resize the window (by pulling at the edge) the background turns back to white from my JTextArea.

Here is my current code:
public class ToolBarActionListener implements ActionListener {
	private JFrame parent;
	private Component component;
	public ToolBarActionListener(JFrame parent) {
		this.parent = parent;
	}
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() instanceof ToolBarButton) {
			ToolBarButton btn = (ToolBarButton)event.getSource();
			if (btn.getName().equals("toolbar.stakes")) {
				if (component != null) {
					parent.getContentPane().remove(component);
				}
				component = new JTextArea();
				parent.getContentPane().add(component, BorderLayout.CENTER);
			}
			if (btn.getName().equals("toolbar.contacts")) {
				if (component != null) {
					parent.getContentPane().remove(component);
				}
				component = new SomeCustomJPanel();
				parent.getContentPane().add(component, BorderLayout.CENTER);
			}
			parent.repaint();
			parent.getContentPane().repaint();
			parent.doLayout();
			parent.getContentPane().doLayout();
		}
	}
}
any idea how I can properly do this?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 1 2006
Added on Jan 4 2006
1 comment
141 views