Solved

Seeing textfields

Posted on 2013-11-20
10
171 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Plain Text Editor for iPad 6 75
Should localization be done inside spring controller 5 25
T-SQL:  Sigh---Boy, this is fun.... 12 46
check java version using powershell 13 84
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This is about my first experience with programming Arduino.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

774 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