12

This is a java program with two buttons used to change an integer value and display it. However in IntelliJIDEA the two lines with

increase.addActionListener(incListener());
decrease.addActionListener(decListener());

keep displaying errors 'Method call expected'.

I am not sure what to do to fix this.

Any help will be greatly appreciated

Thanks

Note: the full code is attached below.

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

public class Main extends JDialog {
public JPanel contentPane;
public JButton decrease;
public JButton increase;
public JLabel label;

public int number;

public Main() {
    setContentPane(contentPane);
    setModal(true);

    increase = new JButton();
    decrease = new JButton();
    increase.addActionListener(incListener());
    decrease.addActionListener(decListener());

    number = 50;
    label = new JLabel();
}

public class incListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        number++;
        label.setText("" + number);
    }
}

public class decListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        number--;
        label.setText("" + number);
    }
}

public static void main(String[] args) {
    Main dialog = new Main();
    dialog.pack();
    dialog.setVisible(true);
    System.exit(0);

}
}
Seb Welch
  • 179
  • 1
  • 1
  • 13
  • Instead of `incListener()` and `decListener()` make them `new incListener()` and `new decListener()`. – skuntsel May 07 '13 at 11:47

7 Answers7

22

incListener and declListener are classes, not methods.

Try

increase.addActionListener(new incListener());

btw, rename your classes names to make them start with an uppercase

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
6

It's simple: use new incListener() instead of incListener(). The later is trying to call a method named incListener, the former creates an object from the class incListener, which is what we want.

PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
1

incListener and decListener are a classes but not a methods, so you must call new to use them, try this:

increase.addActionListener(new incListener()); decrease.addActionListener(new decListener());

sorry for my bad english

Manitra
  • 111
  • 5
0

substitute the lines with

increase.addActionListener( new incListener());
decrease.addActionListener( new decListener());
Simulant
  • 19,190
  • 8
  • 63
  • 98
0

Make these changes:

 public Main() {
    contentPane = new JPanel();
    setContentPane(contentPane);
    setModal(true);

    increase = new JButton("inc");
    decrease = new JButton("dec");
    contentPane.add(increase);
    contentPane.add(decrease);
    increase.addActionListener(new incListener());
    decrease.addActionListener(new decListener());

    number = 50;
    label = new JLabel(number+"");
    contentPane.add(label);
}
hamid
  • 2,033
  • 4
  • 22
  • 42
0

It's sad but I had to Google this same error... I was staring at a method that returned a class. I left off the new operator.

return <class>(<parameters>) vs return new <class>(<parameters>)

Cory
  • 196
  • 2
  • 8
-1

Whenever a string object is created using new operator a new object is created which is what your program is looking for. The following link is useful in learning about the difference between a string and a new string. What is the difference between "text" and new String("text")?