YvesHendseth
YvesHendseth

Reputation: 1181

ActionListener actionPerformed called twice

i have this ActionListener:

logOutButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(getTextFieldContent());

        }
    });

and here i add the button to the panel:

c.fill = GridBagConstraints.RELATIVE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.gridwidth = 1;
this.add(logOutButton, c);

if the content of the JTextField is test, the console output is:

test
test

so i think the actionPerformed() method is called twice but why?

Edit

the hole code:

public GuiPanel(){
    super();
    this.setBorder(new EmptyBorder(10, 10, 10, 10) );
    //Layout:
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    this.setLayout(gridbag);
    setUpJButton();

    //label for textfield
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(labelForName, c);
    this.add(labelForName);

    c.insets = new Insets(5, 0, 0, 0);//padding to top

    //textField
    c.fill = GridBagConstraints.HORIZONTAL;
    setUpTextField();
    c.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(textField, c);
    c.gridwidth = 1;//reset gridwidth
    this.add(textField);

    c.insets = new Insets(10, 0, 0, 0);//padding to top

    //anmelden Button
    c.fill = GridBagConstraints.RELATIVE;
    setUpJButton();
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.LAST_LINE_START;
    this.add(logInButton, c);

    c.insets = new Insets(10, 5, 0, 0);//padding to left

    //abmelden Button
    c.fill = GridBagConstraints.RELATIVE;
    c.anchor = GridBagConstraints.LAST_LINE_END;
    c.gridwidth = 1;
    this.add(logOutButton, c);
}

private void setUpJButton() {
    logInButton.setSize(50, 50);
    logInButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(getTextFieldContent());
        }
    });
    logOutButton.setSize(50, 50);
    logOutButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(getTextFieldContent());

        }
    });
}

Upvotes: 2

Views: 4959

Answers (3)

Vivek MVK
Vivek MVK

Reputation: 1179

Since you instantiate ActionListener() instance using new operator in the addActionListener, it passes two different instance (since you call setUpJButton twice) and it (addActionListener) doesn't know that they are same request.

If you sure don't want to extend ActionListener and use like this, then reference it to an member object and pass that to addActionListener. Something like this:

ActionListener acLs=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
           System.out.println(getTextFieldContent());  //getTextFieldContent??
        }
    };

Then

private void setUpJButton() {
    logInButton.setSize(50, 50);
    logInButton.addActionListener(acLs);
    logOutButton.setSize(50, 50);
    logOutButton.addActionListener(acLs);
}

Upvotes: 0

Predrag Maric
Predrag Maric

Reputation: 24423

You are calling setUpJButton() twice in your code that adds listener twice on same buttons.

Upvotes: 8

lance-java
lance-java

Reputation: 27994

Try printing out e.getActionCommand(). I'm guessing it's two distinct actions on the button.

Upvotes: 0

Related Questions