Solved

Seeing textfields

Posted on 2013-11-20
10
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Object Oriented Programming, C#, referencing, scoping. 13 86
youtube blocking politics 4 80
Crystal Reports Licensing Questions 4 61
JDBC performance 8 29
A short article about a problem I had getting the GPS LocationListener working.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

749 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