This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JTextField Question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi you java ppl,

I have a simple form with a couple of JTextFields.  Currently when I click a JButton the values in those JTextFields are sent to System.out.println() for testing purposes.  How do you pros go about of getting those values into your java code?  I have my ideas and can think of a few but I don't want to lead the witness.  I'd just like to hear what you good people have to say about this.
 
Rancher
Posts: 461
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean get the values into your code?  You mean getting the value of the text field or what?
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Juancito Matos wrote:Currently when I click a JButton the values in those JTextFields are sent to System.out.println() for testing purposes.  How do you pros go about of getting those values into your java code?



The existing Java code would already be doing that, i.e. getting the value from the JTextField before writing it to the console. Can you find that code? If not, then let us know and we'll work on solving that problem first.
 
Juancito Matos
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can get the value because i can display it in on the console. i mean do you use getter , setter propertie of a class. or do you intantiate an object of a class and send the value through a constructor or do use some kind of static strategy so something else, that sort of thing.
 
Juancito Matos
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt write the class yet because i not sure what is a good approach for associating the Jtextfields with the class.  the existing form code is just bare bones. im on a cell now so cant show code now. its just a textfield on s panel which is in turn on a frame.
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh okay. It sounded like there was already some code to look at.

The answer to your question is, to get the data out of a JTextField you call its getText() method.
 
Al Hobbs
Rancher
Posts: 461
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your business logic should be separate from your gui.  So if your text field takes an id then you get some data with it then you have a class that will take the id and give back the data.
 
Juancito Matos
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm familiar with getText() as I'm using that to help me get the information displayed on the console.  What I'm trying to do is insert a record into a SQLite database.  I'm trying to get the value from the JTextField into a table in an SQLite database.  I wanted to get some feed back from the java community on how they go about associating getting the data from the JTextField into the class/object  that ultimately, gets the data into the database.    Setter property?  Through a constructor?  Static method method call, no need to create an object of a class?  Create an object of a class?  This super general idea is what the question is all about.  No code is being requested.  Just want to know generally which is the way you in the know do it.  
 
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a class that represents the data you want to move.
Up to you whether you use setters or a constructor to populate it.
A constructor has the advantage of allowing you to make the class immutable.

Then you can pass that to your persistence code.
 
Rancher
Posts: 3063
23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What I'm trying to do is insert a record into a SQLite database.



So you have a button like "Update Database" and you add an ActionListener to the button.

When the button is clicked you validate that all the text fields have data (by using the getText() method). Then you use the data to update the database by using a PreparedStatement.

the SQL code might be something like:


 
Juancito Matos
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks fellas, i appreciate the help.
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see from Dave and Rob's answers that there are a variety of ways to put data into databases in Java. There is no One Best Way to do that.

Also there are no special ways to put data into databases when the data comes from Swing components. It really makes no difference where the data comes from. In fact when you ask the question about how to get data from Swing components into a database, you are making an unnecessary relationship between two parts of the design which should be kept apart. In an MVC design (which should be used for applications which use Swing) the components are part of the View, the database is part of the Model, and there should be part of the Controller which connects the two.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!