By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,237 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,237 IT Pros & Developers. It's quick & easy.

Working of FocusGained method

100+
P: 126
Can somebody tell me how focusgained method works?
I have a application in which as i enter the text in a textbox it should populate the jTable, My code takes 5 to 10 mins to populate the data, i use java netbeans and microsoft access database.
Expand|Select|Wrap|Line Numbers
  1. jTextField1.addFocusListener(this);
  2.  
  3.     public void focusGained(FocusEvent e) {
  4.         try {
  5.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  6. String filename = "C:/Users/Office User/Desktop/testfordb/test.mdb";
  7. String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
  8.                 database+= filename.trim() + ";DriverID=22}";
  9. Connection con = DriverManager.getConnection( database ,"",""); 
  10. Statement st = con.createStatement();
  11.                 // search
  12. String query1 = "select * from "+jTextField4.getText()+" where `U NAME` like '"+name.getText()+"%' ";
  13. ResultSet rs1 = st.executeQuery(query1); 
  14.                             jTable1.setModel(DbUtils.resultSetToTableModel(rs1));
  15.  
  16.         }
  17. catch (ClassNotFoundException | SQLException e) {
  18.  e.printStackTrace();
  19.  System.out.println("Error: " + e);
  20.     }
  21.     }
  22.  
  23.     public void focusLost(FocusEvent e) {
  24.        // Save the text 
  25.     }
Apr 13 '13 #1

✓ answered by r035198x

You need a KeyListener to listen for key events: http://docs.oracle.com/javase/tutori...ylistener.html

Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,421
Focus happens when you first enter a control when you weren't in it before. If you want something to happen each time a key is pressed, you'll need to use that event.
Apr 14 '13 #2

100+
P: 126
If you want something to happen each time a key is pressed, you'll need to use that event.

I did not understand this, Can i get an example please?
Apr 15 '13 #3

10K+
P: 13,264
You need a KeyListener to listen for key events: http://docs.oracle.com/javase/tutori...ylistener.html
Apr 15 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.