This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Storing JTextField to variable

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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

 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
System.out.println(text);
}

Please clarify if i have not understood you question.
 
Mark Miranda
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please disregard the private void jTextField1ActionPerformed.... that shouldn't be there...my 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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22507
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags instead of color tags, and also keep your indentation intact.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic