Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie question about transferring data between classes  RSS feed

 
Mike Johanon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
My first post, yah
Anyways, I'm a bit new to java and have been having problems with integrating a GUI into an already written program.
The problem is that when a user picks a radio button, how do I share the response with other classes? I've made an ActionListener to listen to the radio group, and input a value from 1 - 10 depending on what is chosen, but now I'm a bit stuck on how to share this.
Thanks in advance....
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
welcome to JavaRanch. You should write a bit more about your problem. Is your GUI in a modal Dialog or ist it inside of an JInternalFrame or is it simply a JavaBean, which can be integrated anywere?
In fact, there are several ways to inform listening classes. The simplest way is to create a method in your GUI getSelectedNumber() which returns the number of the selected JRadioButton.
This you could take in a modal Dialog, because you can ask for the user input after closing the Dialog.
A bit more difficult is notifying the Class, which waits for the information.
Here you have also different solutions. First you can add to your GUI a PropertyChangeSupport and two methods add/removePropertyChangeListener, where you add or remove a PropertyChangeListener to/from your PropertyChangeSupport.
Later, when your action performed, you can use your PropertyChangeSupport to notify the listeners.
This method is simple and I like it, if I haven't to deal with lot's of information.
Another way is to implement the MVC concept (Model View Controller). There you have a model, which is an Observable, you have one or many Views, which are Observers and you have one or many Controllers, which are able to set Values from the Model over set methods. The model itself notifies it's observers, that something has changed. So each Observer's update method is called automatically.
This is only a short preview of possibilities for you. I'm shure, there are some other possibilities.
Hope this helps

Rene
 
Mike Johanon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions...not quite sure how to implement into my program.
The original program called for the user to input flags from the CLI. The new main class(GUI) that I am trying to integrate is a JFrame that calls the original main class when you hit the start button. So far this has worked out pretty good.
Now the radio button group that is being implemented needs to chose what method another class in a different package calls.
It's a bit difficut to explain.....

GUI(hit start to run) --> originalMain --> anEvaluateClass(int value returned to originalMain dependant on GUI radio choice) --> originalMain continues to run
hmmm, hope this isn't more confusing
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Mike, here is a little sample:
Let's assume this is your main class:

and this is your new JFrame with some JRadioButtons:

Just try it, you will find out, how it works.
 
Mike Johanon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rene, you've been a big help...
I think I'm gonna have to alter the flow of my program a bit, but it should work out ok.
This program I'm attempting to add a GUI to was ported to JAVA from C so it is structured a bit odd.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!