Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1408

Re: Adding and removing i

From "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this>
Subject Re: Adding and removing i
Message-ID <1175999219.618323.198540@d57g2000hsg.googlegroups.com> (permalink)
Newsgroups comp.lang.java.gui
References <1175998372.440034.255040@n76g2000hsh.googlegroups.com>
Date 2011-04-27 15:33 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
On Apr 7, 7:12 pm, "Daniel Pitts" <googlegrou...@coloraura.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.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Adding and removing items "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
  Re: Adding and removing i "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000

csiph-web