Adding and removing items in a JPanel, why not showing up?  
Author Message
Daniel Pitts





PostPosted: 2007-4-8 10:13:00 Top

java-programmer, Adding and removing items in a JPanel, why not showing up? I'm trying to remove a button, and replace it with another component,
when a specific event happens.

The following code doesn't seem to work correctly, what am I missing?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test implements Runnable {
JPanel[] panels;
JButton[] buttons;
public void run() {
panels = new JPanel[3];
buttons = new JButton[panels.length];
for (int i = 0; i < panels.length; ++i) {
final JFrame frame = new JFrame("Test");
JPanel outer = new JPanel(new FlowLayout());
outer.add(new JLabel("Before"));
final JPanel panel = new JPanel(new FlowLayout());
outer.add(panel);
outer.add(new JLabel("After"));
frame.add(outer);
final JButton button = new JButton("Click to remove");
panel.add(button);
panels[i] = panel;
buttons[i] = button;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeAllButtons();
}
});
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
private void removeAllButtons() {
for (int i = 0; i < panels.length; ++i) {
panels[i].remove(buttons[i]);
panels[i].add(new JLabel("Added"));
panels[i].repaint();
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Test());
}
}

 
Daniel Pitts





PostPosted: 2007-4-8 10:27:00 Top

java-programmer >> Adding and removing items in a JPanel, why not showing up? On Apr 7, 7:12 pm, "Daniel Pitts" <email***@***.com> wrote:
> I'm trying to remove a button, and replace it with another component,
> when a specific event happens.
>
> The following code doesn't seem to work correctly, what am I missing?
>
> import javax.swing.*;
> import java.awt.*;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
>
> public class Test implements Runnable {
> JPanel[] panels;
> JButton[] buttons;
> public void run() {
> panels = new JPanel[3];
> buttons = new JButton[panels.length];
> for (int i = 0; i < panels.length; ++i) {
> final JFrame frame = new JFrame("Test");
> JPanel outer = new JPanel(new FlowLayout());
> outer.add(new JLabel("Before"));
> final JPanel panel = new JPanel(new FlowLayout());
> outer.add(panel);
> outer.add(new JLabel("After"));
> frame.add(outer);
> final JButton button = new JButton("Click to remove");
> panel.add(button);
> panels[i] = panel;
> buttons[i] = button;
> button.addActionListener(new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> removeAllButtons();
> }
> });
> frame.setLocationByPlatform(true);
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.pack();
> frame.setVisible(true);
> }
> }
> private void removeAllButtons() {
> for (int i = 0; i < panels.length; ++i) {
> panels[i].remove(buttons[i]);
> panels[i].add(new JLabel("Added"));
> panels[i].repaint();
> }
> }
>
> public static void main(String[] args) {
> EventQueue.invokeLater(new Test());
> }
>
> }

Never mind, I needed to call revalidate.