• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Richard Alves
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic