ActionListener button actionPerformed method

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1193 Views - Last Post: 26 February 2013 - 04:55 AM Rate Topic: -----

#1 voh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 13

ActionListener button actionPerformed method

Posted 22 February 2013 - 08:01 AM

Hi, I need when a button (initialState) is click the value of a variable changes accordingly outside of the "public void actionPerformed" method

e.g.


System.out.println(variable);
            initialState = new JButton("Initial state");
            initialState.setToolTipText("Initial state");
           // initialState.addActionListener(actionListener);
            initialState.addActionListener(new ActionListener(){
            	
            	public void actionPerformed(ActionEvent e) {
            		JOptionPane.showMessageDialog(null, "ActionListener is working-- VOH");
            		int variable=100;
            	//	env = new EightPuzzleEnvironment(board);
            	}
            	
            });
            toolbar.add(initialState);



So it should print out 100 although now i get all that "create local variable" rubbish from my IDE where is System.out.println(variable); ....because its outside blah blah

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: ActionListener button actionPerformed method

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:12 AM

You seem to already know what the problem is. Your int is local to the actionPerformed method. Put it outside to both have access to it.
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:22 AM

I guess you want
variable = 100;
not
int variable = 100; // creates you local variable within {}
Was This Post Helpful? 2
  • +
  • -

#4 voh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 13

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:25 AM

Ok Ryano:

int variable=1;
            System.out.println(variable);
            initialState = new JButton("Initial state");
            initialState.setToolTipText("Initial state");
           // initialState.addActionListener(actionListener);
            initialState.addActionListener(new ActionListener(){
            	
            	public void actionPerformed(ActionEvent e) {
            		JOptionPane.showMessageDialog(null, "ActionListener is working-- VOH");
            		int variable = 100;
            	}            	
            });
            toolbar.add(initialState);




but my problem was/is what when I click the initialState button (I wanted the variable becomes 100 instead of 1) although right now it just doesnt "make variable value = 100" it remains 1.
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:29 AM

The value can't be 100 as the actionPerformed hasn't been executed until the button is pressed. That cannot happen until you add the listener which happens after you println the number. Even if it was after it wouldn't work as the value would be printed before the button could ever be pressed.

You are now doing event driven programming. It doesn't execute line by line anymore (well it does but not completely). The code inside the actionPerformed() is only executed when you press the button, which happens only after you initialise everything.
Was This Post Helpful? 1
  • +
  • -

#6 voh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 13

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:30 AM

Mr pbl:

when I do that :

int variable=1;
            System.out.println(variable);
            initialState = new JButton("Initial state");
            initialState.setToolTipText("Initial state");
           // initialState.addActionListener(actionListener);
            initialState.addActionListener(new ActionListener(){
            	
            	public void actionPerformed(ActionEvent e) {
            		JOptionPane.showMessageDialog(null, "ActionListener is working-- VOH");
            		variable = 100;
            	}            	
            });
            toolbar.add(initialState);



it doesn't compile with error and my IDE (eclipse) telling me to "change modifier to final

so it (variable) becomes:
final int variable=1;

which still doesn't compile with error "The final local variable variable cannot be assigned, since it is defined in an enclosing type"

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:31 AM

He meant create it as a field.
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:37 AM

This is why I never use Anonymous class :)

You need it as instance variable
how can you have code

    ...
    int x = bla;
    Anonymous Class {
        x = something else
    }

   } // end of method



as Anonymous Class will eventually be executed, what is the use of changing x as the method using x has finished to run since a while ?
Was This Post Helpful? 1
  • +
  • -

#9 voh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 13

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:50 AM

Thanks guys I now get it. I solves it....

but I (wrongly) presumed that my post could solve my real problem I thought they would work the same way for...


int[] variable = new int[9];
            	 
            	public void actionPerformed(ActionEvent e) {
            		
                	String input = JOptionPane.showInputDialog("Tiles?", "0 "+ "8 " +"7 " +"6 " +"5 "+ "4 " +"3 " +"2 " +"1");
                	String[] inputs = input.split(" ");
                  	for (int j = 0; j< 9 ; j++) 
                  	{   
      					variable[j] = 
                  	            Integer.parseInt(inputs[j]);
      					//System.out.println(variable);
                  	}
                 
            	}            	
            });
            toolbar.add(initialState);
           System.out.println(variable);



where variable outside the method in

System.out.println(variable);



will be recognised.. is there a way?

Thanks again
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 08:55 AM

if int[] variable is declared in the instance variables yes it will

System.out.println(variable) won't work though. You will have to

for(int i = 0; i < variable.length; ++i) {System.out.println(variable[i]);}
Was This Post Helpful? 1
  • +
  • -

#11 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 10:00 AM

View Postpbl, on 22 February 2013 - 03:37 PM, said:

This is why I never use Anonymous class :)/>

You need it as instance variable
how can you have code

    ...
    int x = bla;
    Anonymous Class {
        x = something else
    }

   } // end of method



as Anonymous Class will eventually be executed, what is the use of changing x as the method using x has finished to run since a while ?


I don't get it.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 11:00 AM

class MyClass {
     
    MyClass() {   // constructor
       
        int x = 10;
        JButton b = new JButton("cccc|);
        b.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              x = 100;    // <--- what would be the use of changing x here
          }
         });

        int[] z = new int[x];    // <-- x at constructor time value is 10
    }  // <--- and x stops to exist here



so completly useless to trying to change the value of a method/constructor local variable
Was This Post Helpful? 0
  • +
  • -

#13 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 12:52 PM

Not that. :) Why don't you use anonymous classes?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 01:05 PM

For a lot of reasons, and you do not have to agree with my reasons :)

First it you have multiple buttons added to a constructor it makes the constructor too long. I prefer to shorten the constructor

Second it kind of mix code executed only once and code that can be executed multiple time
the code of a constructor will be executed only once... if you have anonymous ActionListener in it it means that while following code excution path, you need to go back and found in code that was executed only once the embedded actionPerformed()

I always addActionListener(this) on in an inner class
It regroups together all the actionPerformed or even I can use a switch statement based on a int set at coinstruction time


       b1.addActionListener(new Foo(1));
       b2.addActionListener(new Foo(2));
       b3.addActionListener(new Foo(3));


       class Foo implements ActionListener {
          int action;
          Foo(int action) {
             this.action = action;
          }

          public void actionPerformed(ActionEvent e) {
               switch(action) {



Again personal preferences
Was This Post Helpful? 2
  • +
  • -

#15 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: ActionListener button actionPerformed method

Posted 22 February 2013 - 01:09 PM

I think you will like Lambdas in Java 8, when they come. We'll be able to do:

setActionListener( ()-> buttonPushed() );



from what I understand.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2