0

I have a very simple GUI app. It only consists of a button and a label to display how many times that button has been clicked. I implemented it as follows:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Scratch {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Click  Counter");
        frame.setSize(400,250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        JButton button = new JButton("Click Here");
        final int count = 0;
        final JLabel label = new JLabel("Click Count: 0");
        panel.add(button);
        panel.add(label);
        frame.add(panel):
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                count++;  // Error
                String s = label.getText();
                label.setText("Click Count: " + count);
            }
        }); 
        frame.setVisible(true);
    }
}

The problem is with the count variable. If I don't declare it as final, I cannot access it inside the anonymous class. If I do however, I cannot modify it. So what do I do? I really prefer creating actionlistener classes as anonymous classes as opposed to creating multiple classes in a single .java file.

  • Have a look at this question: http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – Martin M J May 02 '15 at 19:15
  • It would be best if you were to get all that code out of the static main method and instead try to create a proper small OOPs compliant program. – Hovercraft Full Of Eels May 02 '15 at 19:17
  • @HovercraftFullOfEels: Like have all the code inside a non-static method instead and call that method from main after creating the object? – Kane Williamson May 02 '15 at 19:20
  • I usually gear my GUI's towards creating JPanels, sometimes having it extend JPanel. Then I add my components in the constructor. In my main method, I'd create my JFrame and add my current JPanel and display it. For example: [sample code](http://stackoverflow.com/a/25276481/522444). – Hovercraft Full Of Eels May 02 '15 at 19:22

1 Answers1

0

declare variable outside function as a class variable

private static int count = 0;
public static void main(String[] args) {
    JFrame frame = new JFrame("Click  Counter");
    frame.setSize(400,250);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    JButton button = new JButton("Click Here");

    final JLabel label = new JLabel("Click Count: 0");
    panel.add(button);
    panel.add(label);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            count++;
            String s = label.getText();
            s = s.substring(0, s.length()-1);
            label.setText(s + count);
        }
    }); 
    frame.setVisible(true);
}
Farnabaz
  • 4,030
  • 1
  • 22
  • 42