It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Getting text from JTextField/JComboBox? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting text from JTextField/JComboBox?" Watch "Getting text from JTextField/JComboBox?" New topic
Author

Getting text from JTextField/JComboBox?

John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

I am trying to get text from either the JTextField or the JComobBox in the program below. I understand I will need to add actionlisteners, and use the .getText() method, but how do I put that into the variable answer.
I have tried multiple things and can never get the text into answer. Also how organized do you think my code is? Is it easy to read/understand?

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
I understand I will need to add actionlisteners, and use the .getText() method, but how do I put that into the variable answer.

Something like:


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Why are you using a MessageDialog? If you want to prompt the user to enter text then you should be using an InputDialog.

See How to Make Dialogs for more examples of using a JOptionPane.
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

I want the message dialog to show the answer that the user typed in the OtherField text box. The problem is I can't get that text into a variable.

I tried


But I get the error OtherField Cannot be resolved to a type.

Here is my current code.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
The problem is OtherField is local to the color() method. It needs to be an instance variable.

BTW please use variable names that conform to the Java naming conventions ie otherField and not OtherField
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Tony Docherty wrote:The problem is OtherField is local to the color() method. It needs to be an instance variable.

BTW please use variable names that conform to the Java naming conventions ie otherField and not OtherField


How do I make it an instance variable? I don't want to change the code to much if possible.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
How do I make it an instance variable?

Instance variables are also known as member variables and/or fields if that helps you, if not read this http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

I don't want to change the code to much if possible.

I am a little confused that you have code that doesn't work and you are concerned about not having to change it too much to get it working.
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Tony Docherty wrote:
How do I make it an instance variable?

Instance variables are also known as member variables and/or fields if that helps you, if not read this http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

I don't want to change the code to much if possible.

I am a little confused that you have code that doesn't work and you are concerned about not having to change it too much to get it working.


Thanks for the link, but I still don't quite understand how to change my code. And what I said about not changing code I meant not changing it drastically, I'd like to keep it all in one class. I am willing to change my code, just not alter it to much if possible, but I'm still learning so I will do what's necessary to make my programs run. Thanks for the help.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Do you understand what the scope of a variable is or what the difference between a local and instance variable is?
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Tony Docherty wrote:Do you understand what the scope of a variable is or what the difference between a local and instance variable is?


I somewhat understand this, the scope of the variable is basically the limits of the variable like where it can/can't be used in the program, correct?

A local variable is variable only usable in a class (used one time) I believe, while an instance variable is an instance of an object (can be used multiple times).
I'm not 100% sure as I'm learning but this is what I think these things are.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
A local variable is a variable declared in a method or block of code (ie in a while loop, if statement etc). It only exists and can only be used within that block of code, after it's declaration. A class variable is a field that is declared as static, it can be used as many times as you like but the variable is shared by all instances of this class.

The problem in your code is that the variable OtherField is declared in the color() method and so is only visible in the color() method. Hence it can't be referenced in the ActionListener.
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Tony Docherty wrote:
The problem in your code is that the variable OtherField is declared in the color() method and so is only visible in the color() method. Hence it can't be referenced in the ActionListener.


So what you're saying is I need to create the OtherField outside of the color() method and use an instance of it in the method so it can accessed in other parts of the program. And whenever I try to define it outside of color() it gives me an error, do I need to put static?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
What error does it give you and what does your code look like?
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Tony Docherty wrote:What error does it give you and what does your code look like?


Actually I got no errors this time, before when I tried to create the text field it would throw something at me but this time it worked fine. For some reason I was under the impression that the objects used in the window needed to be created in the constructor of the class. Here is my finished code for the program. Much thanks for your help.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

You need to learn about Concurrency in Swing and to respect Swing's single threaded rle. All Swing constructors and methods (with very few exceptions) should always be invoked on the EDT. Failure to adhere to this can lead to intermittent problems that are virtually impossible to debug.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting text from JTextField/JComboBox?