aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Using a variable inside an action listener that is created / edited in the main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using a variable inside an action listener that is created / edited in the main class" Watch "Using a variable inside an action listener that is created / edited in the main class" New topic
Author

Using a variable inside an action listener that is created / edited in the main class

Richard Alves
Greenhorn

Joined: Mar 03, 2012
Posts: 12
I am creating an app for fun that takes the name of a JCheckBox to add into a list that I want to do stuff with. Currently because
I am creating the list in the action listener I think it is re-initializing it. I tried setting up a variable in the main class but the action listener cannot pick up on it. I'm guessing that I need to pass it to the action listener but I am not sure how to do that. Here is my action listener code, its an item state changed since I'm looking at JCheckBox's.

To clarify I am trying to make the array out_kitchen_bros available to the itemstatechanged



and here is where I declare the variables that I think to try and make them available.



Appreciate the help. I am newish to java mainly just haven't done much of it recently and having trouble remembering some of the basics.
Richard Alves
Greenhorn

Joined: Mar 03, 2012
Posts: 12
So to anyone who answers this. I found out that using ArrayList is much better. They are dynamic so are much better since I do not know the size of the array and have methods to add and remove items easily so don't have to build your own methods to do so.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

Works a lot better now.
Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
So let me get this straight. You want to add the checkbox text to be added or removed to a list, which in your case is an Array or an ArrayList, depending on the selected or deselected state?

I cannot see your checkbox objects, but did you remember to setPropertyChangeListener to the listeners you have built?

Also I agree with you on changing it to an ArrayList. It'll make the logic easier for your experiment.


regards,
Victor M. Pereira
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Richard Alves wrote:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

Please don't post links to the API for old Java versions which are long past EOL.

Here's the Java SE 6 API: http://docs.oracle.com/javase/6/docs/api/
And this is for Java SE 7: http://docs.oracle.com/javase/7/docs/api/

Also, it would have been nice if you had acknowledged Michael's help in your earlier thread before asking a follow-up question.

To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

This may be what you need ... or not.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Victor M. Pereira wrote:did you remember to setPropertyChangeListener to the listeners you have built?

Which class has a method setPropertyChangeListener? I couldn't find a method with that name anywhere in the Swing API.

Also, what do property changes have to do with the question?
Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
Darryl Burke wrote:what do property changes have to do with the question?


Sorry about the setProperty I was doing it from my own memory. The method name actually is: createActionPropertyChangeListener(Action a) . Is a factory method that creates the propertyChangeListener and it works every time it gets from true to false the JCheckBox. Also if you watch the JComponent you'll see the addPropertyChangeListener(PropertyChangeListener pcl). Meanning that it'll react to just not clicking, but also to the change of value making it easy to test.

And it has to do with the point that I haven't seen the part of the code where he sets its actionlisteners or propertychangelisteners. And also the GUI part where he creates his JCheckBox.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a variable inside an action listener that is created / edited in the main class