Working of FocusGained method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PreethiGowri
    New Member
    • Oct 2012
    • 126

    Working of FocusGained method

    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.
    Code:
    jTextField1.addFocusListener(this);
    
        public void focusGained(FocusEvent e) {
            try {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String filename = "C:/Users/Office User/Desktop/testfordb/test.mdb";
    String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
                    database+= filename.trim() + ";DriverID=22}";
    Connection con = DriverManager.getConnection( database ,"",""); 
    Statement st = con.createStatement();
                    // search
    String query1 = "select * from "+jTextField4.getText()+" where `U NAME` like '"+name.getText()+"%' ";
    ResultSet rs1 = st.executeQuery(query1); 
                                jTable1.setModel(DbUtils.resultSetToTableModel(rs1));
                     
            }
    catch (ClassNotFoundException | SQLException e) {
     e.printStackTrace();
     System.out.println("Error: " + e);
        }
        }
    
        public void focusLost(FocusEvent e) {
           // Save the text 
        }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • PreethiGowri
      New Member
      • Oct 2012
      • 126

      #3
      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?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

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

        Comment

        Working...