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


Groups > comp.lang.java.gui > #1406 > unrolled thread

Adding and removing items

Started by"Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this>
First post2011-04-27 15:33 +0000
Last post2011-04-27 15:33 +0000
Articles 2 — 1 participant

Back to article view | Back to comp.lang.java.gui


Contents

  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

#1406 — Adding and removing items

From"Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectAdding and removing items
Message-ID<1175998372.440034.255040@n76g2000hsh.googlegroups.com>
  To: comp.lang.java.gui
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());
  }
}

---
 * 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

[toc] | [next] | [standalone]


#1408 — Re: Adding and removing i

From"Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: Adding and removing i
Message-ID<1175999219.618323.198540@d57g2000hsg.googlegroups.com>
In reply to#1406
  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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web