?
Solved

Java JTextField Behavior and getText() return value

Posted on 2005-05-17
10
Medium Priority
?
4,330 Views
Last Modified: 2008-01-16
My problem seems to be in the Jtextfield behavior.  It is used as an input field for searching, basically what happens is as long as the user does not click the field with the mouse, keying in the values and pressing enter works, getText will work once enter is pressed, however if this same field is clicked on with the mouse and then typed in, the numbers show up in the field, the event gets fired,  but getText() returns an empty field to my handler.  Any suggestions would be appreciated to fix this nuiscance




0
Comment
Question by:reedsr
  • 5
  • 5
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 14018933
>> the numbers show up in the field, the event gets fired,  but getText() returns an empty field
Are you sure you're looking at the right JTextField?

You could try:

Document doc = yourTextField.getDocument();
String txt = doc.getText(0, doc.getLength());

But that should be the same.
0
 
LVL 4

Author Comment

by:reedsr
ID: 14019142
That didn't work, it yielded the same result.  

Like i said the app works however many n times you type the string then press enter but once you click in the box it quits getting anything form the getText Function
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14019184
Are you able to reproduce this behaviour in a small application that you can post the code of?
Or can you post your current code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Author Comment

by:reedsr
ID: 14019229
private void look_up_request_idActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_look_up_request_idActionPerformed
        // TODO add your handling code here:
       
        G_wr_id = look_up_request_id.getText();
        String wr_num = look_up_request_id.getText();
        look_up_request_id.setText("");

       
        System.out.println("Event: " + evt);
       
        System.out.println("wr_num recieved from search box: " + G_wr_id);
       
        if (wr_num.length() < 5 || wr_num.length() > 7 )
            return;
        else if (wr_num.length() == 5 )
            wr_num = "R0" + wr_num;
        else if (wr_num.length() == 6 )
            wr_num = "R" + wr_num;
       
       
       
        //build sql statement
       
        ResultSet rs = null;
        ResultSetMetaData rsmd = null;
        initComponents();
       
           try{
            Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        } catch( Exception ex) {System.out.println("Class.forname attempt:" + ex);}
       
        try{
           
            Connection con = DriverManager.getConnection(url);
            Statement stmt = con.createStatement();
           
            rs = stmt.executeQuery( " /* removed for posting */ " );
            rsmd = rs.getMetaData();

             while( rs.next()) {
               
                //set varibles
                //look into repainting issue
                       
                        look_up_wr_id.setText(rs.getString(1));
                       
                        look_up_requestor.setText(rs.getString(2));
                       
                        look_up_desc.setText(rs.getString(3));
                       
                        look_up_phy_loc.setText(rs.getString(4));
                       
                        look_up_asset_name.setText(rs.getString(5));
                       
                        look_up_ts.setText( rs.getString(6).substring(0,10) );
                       
                        look_up_state.setText(rs.getString(7));
                       
                        System.out.println(look_up_wr_id.getText());
             }
            } catch( Exception ex){ }    
        look_up_request_id.requestFocus();
       
    }//GEN-LAST:event_look_up_request_idActionPerformed
0
 
LVL 4

Author Comment

by:reedsr
ID: 14019249
the globabl isn't used right now, i just put it in to test some things, what i am seeing is the correct returns  like event data and the wr_id value when i hit enter but once i click the box i get nothing form the getText printout
0
 
LVL 37

Accepted Solution

by:
zzynx earned 1000 total points
ID: 14019329
Well, it's for sure that once you do this

>>  look_up_request_id.setText("");
>>
>> ...
>> 
>> System.out.println(look_up_wr_id.getText());

 This will for sure return ""

Q1. When is look_up_request_idActionPerformed triggered?
Q2. Why do you
             look_up_request_id.setText("");
0
 
LVL 4

Author Comment

by:reedsr
ID: 14019375
I agree but i don't do
>> System.out.println(look_up_wr_id.getText());
it is  System.out.println("wr_num recieved from search box: " + G_wr_id);
 and G_wr_id was saved form before, kind of silly i know, i have been fiddling with things and my codde has become a little sloppy

1. when the enter key is pressed and focus is on the box on the form
2. I want to clear the results they typed in so they can search again with haveing to change anything

I have actually just found my problem and I am not sure how it got in there.  the call to init components was screwing the things up because it is called in another poriton of the code so i am guessing it was re-initializing things and for some reason the mouse click was not working with it like the enter was anyways, thanks for your help
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14019484
>> I have actually just found my problem
Great.
>> anyways, thanks for your help
Thanks for accepting
0
 
LVL 4

Author Comment

by:reedsr
ID: 14019503
i always dislike when I try to help someone and then they withdraw the question and points because the solution wasn't exactly because of why they posted in the first place. enjoy
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14019592
You're too kind :°)
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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 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