• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

changing vector from GUI events

 
kenji mapes
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I can explain this without posting code.

I have a GUI class that extends JFrame and implements listeners, so the calss itself is the listener. The handlers are overriden in this calss.

In my driver class, I create a vector to hold instances of of type derived Vehicles (my abstract class), with the specific instances being "vanilla" cars, family cars, economy cars, and motorcycles.

I create the vector outside of the GUI calss so that I can reference it outside of a GUI instance. I pass the vector to the GUi constructor, and it adds the vector to a JList and a JComboBox. That is a bit reundant, but I wanted to play with both components so I could learn them.

Now for the event handling, I wanted to be able to add vehicles, remove vehicles, and sell vehicles from the handles on the buttons named "sell," "remove," and "add."

I'd like for the vector to be updated with these actions. I have code in the definition of actionPerformed.

For example, when a user wants to add a vehicle, I have input to select the vehicle type, and then prompt the user for info to store, and then pass to a new vehicle instantiation. I wanted this new object to be added to the vector, and then added to the combo box and list.

However, in my class definition it says that I cannot access the vector refeence variable that is declared in main, and then passed to the GUI contructor.

What gives? Thanks.

Kenji
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> declared in main

declare it as a class member

 
kenji mapes
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael, I will try that. I was thinking that the vector would be accessible in the same folder, same package, but I guess my idea of scope is not fully refined.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic