This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes passing value from jcombobox to other classes? 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 "passing value from jcombobox to other classes?" Watch "passing value from jcombobox to other classes?" New topic
Author

passing value from jcombobox to other classes?

John Towers
Greenhorn

Joined: Jan 18, 2007
Posts: 4
Hi

I have an Jcombobox with an actionlistener set up in a class called "boxSelected", so that when the user clicks on the choice they're after the result is sent to a jtext area.



The action listener class is actually contained within another class, one called Editor.

My question is, can I pass this new string value, boxSelected, for use in another class? I was going to create a new .java file and pass this string value for use with a class there.

I've been at this a wee while and am still very new. Thanks for any help or code you could give me here to sort this, any help would be great.
[ February 02, 2007: Message edited by: John Towers ]
Ryan Giomi
Greenhorn

Joined: Jan 24, 2007
Posts: 9
John,

This is how I did it:
-Built my GUI in one class file
-Built my engine (run all calcs pertinent to GUI selections) in its own class file
-Main Method in its own class file(just how I did it. Not needed obviously)

My engine class extends the GUI class.

In a method inside of my engine class the code that reads the combo box looks like this:


First thing, I only included the relevant code that you are asking about, except the bit with "Age". That was just to show you some transition.

textaReadOut.setText --> textaReadout = text area readout (just explaining my verbage ). This is the JTextArea for displaying your info. Apply the .setText to tell your program to print in that specific JTextArea (.setText is like System.out.println... but for GUIs).

comboSex.getSelectedItem() prints whatever was selected in the JComboBox that I called comboSex (fyi, comboSex is a selection between "male" and "female").

I hope that helps.
John Towers
Greenhorn

Joined: Jan 18, 2007
Posts: 4
Hi Thanks a lot for your reply.

I'm giving that a shot, but I'm coming up against another problem right away- when I try to extend an my editor class like so



I'm getting this error:

cannot find symbol
symbol : constructor Editor()
location: class Editor

So I'm not getting very far. I've read up on inheritance but still don't understand the problem. Can anyone tell me what I'm doing wrong here?

Thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I'm getting this error:

cannot find symbol
symbol : constructor Editor()
location: class Editor


The likely short answer is put in a super(); call.

The likely longer answer is:-

Go and compare the constructor in your superclass (Editor) with what you are calling.
How many constructors do you have in Engine? Are you calling them from one
another using the syntax?
Remember every constructor in your subclass has to have a route (direct or indirect) to call the constructor for the superclass, unless you have a public [or protected] no-argument constructor in your superclass.

SO:- If you have a public no-argument constructor in your superclass, you oughtn't to get that error message.
If you haven't written a constructor for your superclass, your compiler will impute a public no-argument constructor, and you oughtn't to get that error message.

BUT:- If you only have public [or protected] constructors with arguments in your superclass, then every constructor in your subclass must call one of those constructors with a statement. If you have overloaded your subclass constructors, you either have to call the superclass constructor directly with super(something); in each constructor, or use this(); to call another constructor which then uses super();

I hope you get your program to work and I hope you can understnad what I have written.
CR
Ryan Giomi
Greenhorn

Joined: Jan 24, 2007
Posts: 9
John,

I took some old code that I wrote and trimmed it down to show you how I did something similar to what you are asking about. In my example I used your class names Editor and Engine. The main method is nested in class Editor. So scope on my code and tell me what you think:

Here is your Editor:



Here is your Engine code:


If anybody sees useless code that pertains directly to how to accomplish John's question please feel free to help me trim that fat. No need to point out stuff like "int x = 0". It's obvious that that isn't used. Please point out things that are needed, but can be reduced (ie general code format, etc).

Thanks.

John, let me know if this properly addresses your original question.
John Towers
Greenhorn

Joined: Jan 18, 2007
Posts: 4
Thanks again for the replies (and patience) guys; you�ve been helping me get my head round some stuff that normally have me hitting my head against a wall. This is a great forum.

Ryan, I get a strong feeling that that would solve my problem but for one other problem. The Jcombobox I�m using is basically being populated from a file reader in my editor class, through a fairly contrived method of reading its values in line by line, taking a substring of those lines and converting them to a hashset to remove duplicate values. The end result is the combobox in my editor class is populated with an array of strings called �comboarray�.

(it took perseverance, heh)

The thing is though that this is all happening in another action performed method.





So what I end up with is a comboarray, equivalent to the �sex� and �activity� strings your example that you populated your equivalent JComboBoxes with. The problem is, I think, that because my comboarray is being built in another ActionListener / action performed in my Editor class it can�t be accessed outside the action event, as they aren�t capable of returning values. I think.

So that means in the Engine class you showed me,



I don't think it would work for me if 'sex' or 'activity' were replaced with my 'comboarray'.



Does that sound right? It's the only way I can explain this still not working, unless I messed up my call to my superclass constructor.
Thanks again

John.
Ryan Giomi
Greenhorn

Joined: Jan 24, 2007
Posts: 9
Hey, John. I won't be able to think on this for a few days. As soon as I have a chance, I'll consider what you wrote, and see if I can come up with a solution. Hopefully somebody will figure it out before then.

***edit: It's been a little over a month... I did not figure it out since then, but honestly I've been preoccupied with other issue. Have you had any luck?
[ March 14, 2007: Message edited by: Ryan Giomi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing value from jcombobox to other classes?