Forums Register Login

Newbie question about transferring data between classes

+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1047 times.
Similar Threads
Interaction between GUI class and "main" class
How to create a flexible jsp page?
handle radio buttons in servlet
Dynamic RadioButton Listeners
How to add action listeners to grid buttons?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:19:43.