2015-10-03 35 views
0

我在Combobox中嘗試了一些帶動態列表的代碼,但輸入後不會在組合框中的選定項上添加整個單詞。我們如何在Java Swing中添加Google搜索框?

如何讓它更像Google搜索框那樣動態?

+0

你想要的「自動完成」。有幾個實現,Swing實驗室,SwingX庫想到 – MadProgrammer

+2

請參考[此鏈接] [1]。這是u有相同疑問 [1]:http://stackoverflow.com/questions/26550559/how-to-create-a-search-bar-similar-to-google-search-style -in-java-gui –

回答

0

您可以使用此代碼使用組合框數據庫連接動態地更新.....

//code: 


String pattern=TabletCombobox.getSelectedItem().toString(); 
    System.out.println(pattern); 
    //TabletCombobox.setSelectedItem(pattern); 

    //AutoCompleteDecorator decorator; 
    //AutoCompleteDecorator.decorate(TabletCombobox); 
    try 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con=DriverManager.getConnection(url,username,password); 
     Statement st=con.createStatement(); 
     String Query="select * from tablet"; 
     ResultSet rs=st.executeQuery(Query); 
     int i=1; 
     while(rs.next()) 
     { 
      TabletCombobox.add(this, rs.getString(1),i++); 
     } 
    } 
    catch(Exception e) 
    { 
     System.err.println(e); 
    } 
+0

yaa它的權利,但是,它不會搜索我們鍵入可編輯的組合框中的內容,那麼該怎麼辦? –

相關問題