1

Have a little problem with some code i have writing to try out something. I have made a frame with a single button in it. When i click on this button, a new frame opens, which it should. I close down the new frame, and then click on the button again, to try see if it still works. The problem starts here, corse insted of opening a single new frame, it opens two new frames. Third time i click it opens 4 frames and so on. I have tried quite a few things, but sadly cant seem to find the reason why it is opening more frames. Please help.

package budget;

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

public class GUI extends JFrame {

    String labelPrefix;
    JButton button;
    JButton button2;
    JLabel label;

    public static void main(String[] args) {
        JFrame f = new GUI();
        f.setExtendedState(f.MAXIMIZED_BOTH);
        f.setVisible(true);
    }

    public GUI() {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        button = new JButton("Click Me");
        label = new JLabel(labelPrefix);
        p.add(button);
        this.setTitle("Try");
        getContentPane().add(p);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        button.addActionListener(new MyActionListener());
    }

    class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            button.addActionListener(this);
            labelPrefix = "Try";
            JFrame f2 = new GUI(label, labelPrefix);
            f2.setExtendedState(f2.MAXIMIZED_BOTH);
            f2.setVisible(true);

        }
    }

    public GUI(JLabel label, String labelPrefix) {
        JPanel p2 = new JPanel();
        button2 = new JButton("Close");
        p2.add(label);
        p2.add(button2);
        this.setTitle("Try");
        getContentPane().add(p2);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        button2.addActionListener(new MyActionListener2());
    }

    class MyActionListener2 implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            button2.addActionListener(this);
            dispose();
        }
    }
}
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 1
    *"a new frame opens, which it should"* Beg to differ. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Oct 15 '12 at 11:20

2 Answers2

3

Clearly, the problem is here:

button.addActionListener(this);

Every time you click the button, it adds the listener yet another time to the button.

Simply remove that line and the error will go away. Once a listener is added to a button, it stays there. It isn't "consumed" after being triggered.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Check the first line in the actionPerformed of MyActionListener which states:

button.addActionListener(this);

This line should be removed.

publysher
  • 11,214
  • 1
  • 23
  • 28