-1

I have a program which searches for price and Description by scanning the barcode. I need the program(txt_barcode TextField) to hold the user results(searched results) for only 3 seconds and automatically clear up for the next user request. For example, if i scan a barcode, the results should show for only 3 seconds and automativally clear up for the next scan or request.the request seasion should only be for 3 seconds Can you help me to insert this code for me?. I am using Netbeans and here is my code.

private void txt_barcodeKeyReleased(java.awt.event.KeyEvent evt) {                                        
   try{
       String sql="select * from item_mast where barcode=?" ;
       pst=conn.prepareStatement(sql);
       pst.setString(1,txt_barcode.getText());

       rs=pst.executeQuery();
       if(rs.next()){
           String add1 = rs.getString("descr");
           txt_description.setText(add1);
           String add2 = rs.getString("retail1");
           txt_price.setText(add2);


       }         


   }catch(Exception ex){
       JOptionPane.showMessageDialog(null, ex);
   }

}
Emma
  • 23
  • 1
  • 5

2 Answers2

0

You can achieve timertask functionality in your code by extending timertask class with this ref.

Community
  • 1
  • 1
The N..
  • 70
  • 1
  • 5
0

This is how it might be done in your case:

Timer timer = new Timer();
timer.schedule( new ClearField( txt_description ), 3000 );

class ClearField extends TimerTask {
    private JTextField field;
    public ClearField( JTextField field ){
        this.field = field;
    }
    public void run(){
         field.setText( "" );
    }
}

Adapt the argument list as required.

There may be a problem if the next scan happens and should be displayed before the 3 seconds are over. This may require a more complex procedure to avoid a collision on the field. Your Q does not indicate such a possibility.

If you really want/need to block (!) the application for three seconds, then of course a Thread.sleep would be an option, although this is usually sneered at (for good reason). In this case, make sure to use some sort of count-down - e.g. indicate the count-down on the GUI so that the users sees that the app is "on hold".

laune
  • 31,114
  • 3
  • 29
  • 42