3

I just made my first event based GUI in java, but i don't understand where i am wrong. the code worked fine when there was no event handling applied..

Here's the code.

package javaapplication1;

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

 class Elem implements ActionListener{

    void perform(){
        JFrame frame = new JFrame();
        JButton button;
        button = new JButton("Button");
        frame.getContentPane().add(button) ;
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(this);
     }

    public void actionPerformed(ActionEvent ev){
        button.setText("Clicked");
    }
}

 public class JavaApplication1 {

  public static void main(String[] args) {
   Elem obj = new Elem();
   obj.perform();
  }  
}
3
  • 2
    Well, what's supposed to happen and what's happening now? Commented Nov 12, 2013 at 10:48
  • What is the error ? does this code compile ? Commented Nov 12, 2013 at 10:48
  • yes, it compiles and runs too, but as soon as i click the button, there are around 10 errors thrown :\ Commented Nov 12, 2013 at 10:49

2 Answers 2

2

There is a problem with variable scope. Move the JButton button; definition outside of the perform() method so that actionPerformed() can access it.

JButton button;

void perform(){
    JFrame frame = new JFrame();
    button = new JButton("Button");
    frame.getContentPane().add(button) ;
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    button.addActionListener(this);
 }

When you define an object inside of a method (e.g. perform()), its scope is limited to that method (inside the curly braces). This means that other methods inside your class cannot access that variable.

By moving the object definition outside of the method, it now has class level scope. This means that any method within that class can access the variable. You're still defining its value inside of perform(), but now it can be accessed by other methods, including actionPerformed().

See here for more explanation.

Sign up to request clarification or add additional context in comments.

Comments

2

As you used button object inside actionPerformed method. So declare button globally.

class Elem implements ActionListener{
     JButton button;// Declare JButton here.
    void perform(){
        JFrame frame = new JFrame();

        button = new JButton("Button");
        frame.getContentPane().add(button) ;
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(this);
     }

    public void actionPerformed(ActionEvent ev){
        button.setText("Clicked");
    }
}

 public class JavaApplication1 {

  public static void main(String[] args) {
   Elem obj = new Elem();
   obj.perform();
  }  
}

1 Comment

This means visibility. Declaration is not visible outside of block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.