Link to home
Start Free TrialLog in
Avatar of reedsr
reedsr

asked on

Java JTextField Behavior and getText() return value

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




Avatar of zzynx
zzynx
Flag of Belgium image

>> 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.
Avatar of reedsr
reedsr

ASKER

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
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?
Avatar of reedsr

ASKER

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
Avatar of reedsr

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of reedsr

ASKER

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
>> I have actually just found my problem
Great.
>> anyways, thanks for your help
Thanks for accepting
Avatar of reedsr

ASKER

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