Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Storing JTextField to variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Storing JTextField to variable" Watch "Storing JTextField to variable" New topic

Storing JTextField to variable

Mark Miranda

Joined: Feb 25, 2011
Posts: 24

I made this GUI using netbeans and I was wondering, how can I store the JTextFields to a local variable?

My event handler is in a different package

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172

Mark Miranda wrote:
I made this GUI using netbeans and I was wondering, how can I store the JTextFields to a local variable?

jTextField1 is a local(instance) variable (On a side note, it is a good idea to provide more informative names for variables, like firstNameField, or emailField or whateverField)
Is this what you were asking? Or did you mean how to store the value as an instance variable. In that case use something like

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
Hi Mark,

According to the below code which you have coded i think you know how to get the value from the JTextField,

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
String text = jTextField1.getText();

Please clarify if i have not understood you question.
Mark Miranda

Joined: Feb 25, 2011
Posts: 24
Please disregard the private void jTextField1ActionPerformed.... that shouldn't be mistake

My overall goal is to create a form and once the 'Add' button is clicked I would like to display, using System.out.println() (for debugging purposes), each JTextField all at once.

I would like to use one event handler and that is AddressEntryEventHandler...

- i hope this makes sense..
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
above mentioned goal can be accomplished in this way- make an array of JTextFields at class level, no need to have any ActionListener for the text fields, you need to have an ActionListener added to the JButton.

Here printtextFieldData() is a class method and is called from the inner anonymous class as jTextField array is not accessable form the inner anoymous class.

Hope this will answer your query
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

Please UseCodeTags instead of color tags, and also keep your indentation intact.

How To Ask Questions How To Answer Questions
Roberto Perillo

Joined: Dec 28, 2007
Posts: 2258

Howdy, Mark!

Champ, just a small observation on this. I think you want to store the value just to get it in a lower layer, isn't it? Well, here are some tips that I think that will help you:

1. In your JFrame, you have an instance level variable (private JTextField name) and you have a getter method that returns its content (for instance, getName -> return name.getText());
2. You have an ActionListener that listens to some button (for instance, a menu item). When you click it, something happens (for instance, another window is opened). It has an instance-level variable that stores the window that has the text field;
3. When you are building the window that has the menu item, you instantiate the ActionListener and pass the current window to this ActionListener (new YourActionListener(this));
4. In the actionPerformed method, you just call window.getName method from the window and then you'll have the value that is currently filled in the text field.

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
I agree. Here's the link:
subject: Storing JTextField to variable
Similar Threads
Convert working program to display in GUI
Problem With GUI of Net Beans
Reading RS232 and displaying contents on Frame
Pressing a button to open a new window
null exception in java swing code