Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting text from JTextField/JComboBox?

 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2610
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error does it give you and what does your code look like?
 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic