Solved

Seeing textfields

Posted on 2013-11-20
10
169 Views
Last Modified: 2013-12-02
following is my code to populate some textfields.  The textfields are defined in the main program so I get errors saying can't find the textfields.  How do I let this class know about the textfields.  Thanks

public class populateInvName {
    ConnectDatabase connectDatabase = new ConnectDatabase();    
    public Connection con;
       
       
    public void populateInvName() {

         try {

            con = connectDatabase.getConnection();
            Statement st = (Statement) con.createStatement();
            String command = "SELECT * FROM seller WHERE name = " + "'" + entry +"'";
                 ResultSet rec = st.executeQuery ( command );
                 
            rec.next();            
           
            jTextField_dailySellerNumber.setText(rec.getString("number"));
            jTextField_dailySellerName.setText(rec.getString("name"));
            jTextField_dailySellerAddress1.setText(rec.getString("address1"));
            jTextField_dailySellerAddress2.setText(rec.getString("address2"));
            jTextField_dailySellerCity.setText(rec.getString("city"));
            jTextField_dailySellerState.setText(rec.getString("state"));
            jTextField_dailySellerZip.setText(rec.getString("zip"));
            jTextField_dailySellerMortgage.setText(rec.getString("mortgage"));
            jTextField_dailySellerComment.setText(rec.getString("comment"));
            jTextField_dailySellerHeadIn.setText(rec.getString("head"));
           
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,
                "SQLException Reading Daily Seller: " + ex.getMessage());
        }
    }      
   
}
0
Comment
Question by:sargent240
  • 5
  • 4
10 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 39665047
First set the value in some object like Person.
put it in list.

Call this class from main class and get the list.
Iterate that list in main class and set it in jTextField.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39665639
You could pass a Map to that method
    public void populateInvName(Map<String, JTextComponent> fieldsMap) {
        final String[] FIELD_NAMES = {
                "number", "name", "address1", "address2", "city", "state", "zip",
                "mortgage", "comment", "head"
            };
...
        if (rs.next()) {
            for (int i = 0; i < FIELD_NAMES.length; i++) {
                fieldsMap.get(FIELD_NAMES[i])
                         .setText(rs.getString(FIELD_NAMES[i]));
            }
        }

Open in new window

0
 

Author Comment

by:sargent240
ID: 39670892
Thanks CEHJ! One question, What do I send to the method when I call it for Map<String and JTextComponent>fieldsMap.

Thanks, Cheers.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39671338
Well you need to create a Map of course with all your fields keyed by column name
0
 

Author Comment

by:sargent240
ID: 39672796
I have been looking around at how to set up the map and am not grasping what I need to do.  Could you show me what the code should be as you did above?  THANKS!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
ID: 39672824
fieldsMap = new HashMap<String, JTextComponent>();
fieldsMap.put("number", tfNumber);
fieldsMap.put("name", tfName);
...

Open in new window

0
 

Author Comment

by:sargent240
ID: 39673622
I used the code from your last post and got a:

cannot find symbol var variable

on fieldsMap, tfNumber, and tfName.

I am struggling with the syntax, continuing to search through info on the web without much success.  Besides the code I could use a little elementary discussion as to the parts involved.  That would help a lot as to future use of the idea.  Again Thank you very much.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39673898
I used the code from your last post and got a:

cannot find symbol var variable
Please post, verbatim, the error you got. Of course, you would have to define your Map variable as an instance variable
0
 

Author Closing Comment

by:sargent240
ID: 39689086
I got it figured out.  Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39689546
:)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now