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
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
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?
Or can you post your current code?
ASKER
private void look_up_request_idActionPe rformed(ja va.awt.eve nt.ActionE vent evt) {//GEN-FIRST:event_look_up _request_i dActionPer formed
// 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.sourcef orge.jtds. jdbc.Drive r").newIns tance();
} catch( Exception ex) {System.out.println("Class .forname attempt:" + ex);}
try{
Connection con = DriverManager.getConnectio n(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.g etString(1 ));
look_up_requestor.setText( rs.getStri ng(2));
look_up_desc.setText(rs.ge tString(3) );
look_up_phy_loc.setText(rs .getString (4));
look_up_asset_name.setText (rs.getStr ing(5));
look_up_ts.setText( rs.getString(6).substring( 0,10) );
look_up_state.setText(rs.g etString(7 ));
System.out.println(look_up _wr_id.get Text());
}
} catch( Exception ex){ }
look_up_request_id.request Focus();
}//GEN-LAST:event_look_up_ request_id ActionPerf ormed
// 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:
System.out.println("wr_num
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.sourcef
} catch( Exception ex) {System.out.println("Class
try{
Connection con = DriverManager.getConnectio
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.g
look_up_requestor.setText(
look_up_desc.setText(rs.ge
look_up_phy_loc.setText(rs
look_up_asset_name.setText
look_up_ts.setText( rs.getString(6).substring(
look_up_state.setText(rs.g
System.out.println(look_up
}
} catch( Exception ex){ }
look_up_request_id.request
}//GEN-LAST:event_look_up_
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I agree but i don't do
>> System.out.println(look_up _wr_id.get Text());
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
>> System.out.println(look_up
it is System.out.println("wr_num
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
Great.
>> anyways, thanks for your help
Thanks for accepting
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 :°)
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.