Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Method Call Expected

Tags:

java

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);

}
}
like image 329
Seb Welch Avatar asked May 07 '13 11:05

Seb Welch


3 Answers

incListener and declListener are classes, not methods.

Try

increase.addActionListener(new incListener());

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

like image 194
Arnaud Denoyelle Avatar answered Oct 24 '22 07:10

Arnaud Denoyelle


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.

like image 5
PurkkaKoodari Avatar answered Oct 24 '22 06:10

PurkkaKoodari


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

like image 1
Manitra Avatar answered Oct 24 '22 06:10

Manitra