2014-07-08 62 views
0

我開始開發自己的自動完成搖擺組件,並且我想在用戶按下上或下鍵時將焦點設置到列表中,並且同時讓光標開啓文本域,讓他輸入文本或數字....將焦點設置爲JList並將光標放在textField上swing java autocomplete

到打字時或將焦點設置的JList下來我已經使用

list.requestFocus();

有什麼辦法有重點JList和光標的JTextField

here

這裏查看圖像我的代碼:

package examples.autocomplete; 

import java.awt.EventQueue; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.util.List; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 
import javax.swing.ListSelectionModel; 
import javax.swing.border.EmptyBorder; 

import com.gestioncaisse.dao.ClientDAO; 
import com.gestioncaisse.dao.DAO; 
import com.gestioncaisse.dao.MyConnection; 
import com.gestioncaisse.pojos.Client; 
import com.gestioncaisse.utils.utils; 

public class testcombo extends JFrame { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JPanel contentPane; 
    private JTextField textField; 
    final DAO<Client> clientDao; 
    List<Client> list_clients; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        testcombo frame = new testcombo(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    boolean first_time = true; 

    /** 
    * Create the frame. 
    */ 
    public testcombo() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 

     clientDao = new ClientDAO(MyConnection.getInstance()); 

     list_clients = clientDao.findAll(); 

     textField = new JTextField(); 
     textField.setBounds(5, 11, 113, 20); 

     textField.setColumns(10); 

     final JButton btnNewButton = new JButton("..."); 
     btnNewButton.setBounds(116, 10, 45, 23); 
     contentPane.setLayout(null); 

     JScrollPane scrollPane = new JScrollPane(); 
     scrollPane.setBounds(5, 31, 156, 144); 

     final JList list = new JList(
       utils.fromListToObjectTable2Clients(list_clients)); 
     scrollPane.setViewportView(list); 
     list.setVisibleRowCount(5); 

     list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     contentPane.add(scrollPane); 
     contentPane.add(textField); 
     contentPane.add(btnNewButton); 

     textField.addKeyListener(new KeyListener() { 
      @Override 
      public void keyTyped(KeyEvent arg0) { 
      } 
      @Override 
      public void keyReleased(KeyEvent arg0) { 
       int a = list.getSelectedIndex(); 
       if (arg0.getKeyCode() == KeyEvent.VK_UP) { 
        list.requestFocus(); 
       } else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) { 
        list.requestFocus(); 
       } else if (!first_time) { 
       } else { 
        first_time = false; 
       } 
      } 
      @Override 
      public void keyPressed(KeyEvent arg0) { 

      } 
     }); 

    } 
} 

//if (a > 0) 
//list.setSelectedIndex(a - 1); 
//int first_vis = list.getFirstVisibleIndex(); 
//list.setListData(utils.fromListToObjectTable2Clients(clientDao.findByString(textField.getText()))); 
//list.setSelectedIndex(0); 
+0

郵政編碼,您嘗試使用。 – alex2410

+0

請查看編輯 – grotibo

回答

2

離開注重JTextField但增加向上/向下鍵將KeyBindings。在行動只是改變選擇在JListpublic void setSelectedIndex(int index)法)

UPDATE

的aslternative方法是對JList的焦點,並添加KeyListener的轉換輸入字符到JTextField的大小。顯示尖使用

jTextFieldInstance.getCaret().setVisible(true); 
jTextFieldInstance.getCaret().setSelectionVisible(true); 
+0

我嘗試過setSelectedIndex,但它不太方便,速度也不那麼快,在這種情況下,如果它們不是我也不知道的,我也要做出明顯的選定項目 – grotibo

+0

@ user3747935 JList has built_in navigation(KeyBindings ),例如兩次按任何字符,肯定只能用於第一次。字符串中的字符(startWith) – mKorbel

+0

那麼,你可能會說,這是不可能有我想要的? – grotibo

2

在這裏,我用來從數據庫

keywordssearcher.setEditable(true); 
    final JTextComponent sfield = (JTextComponent) keywordssearcher.getEditor().getEditorComponent(); 
    sfield.setVisible(true); 
    sfield.addKeyListener(new KeyAdapter() { 

     @Override 
     public void keyReleased(KeyEvent ke) { 
      SwingUtilities.invokeLater(new Runnable() { 

       public void run() { 
        comboFilter(sfield.getText(), con); 
       } 
      }); 
     } 

     @Override 
     public void keyPressed(KeyEvent e) { 
      if (!(sfield.getText().equals("")) || (sfield.getText() == null)) { 
       if (e.getKeyCode() == KeyEvent.VK_ENTER) { 
        addKeyWord(sfield.getText().toString()); 
       } 
      } 
     } 
    }); 

    public void comboFilter(String enteredText, Connection ncon) { 
    log.info("ncon autosuggest--->" + ncon); 
    ArrayList<String> filterArray = new ArrayList<String>(); 
    String str1 = ""; 
    try { 
     Statement stmt = dc.ConnectDB().createStatement(); 
     String str = "SELECT k_word FROM T_KeyWords WHERE k_word LIKE '" + enteredText + "%'"; 
     ResultSet rs = stmt.executeQuery(str); 
     while (rs.next()) { 
      str1 = rs.getString("k_word"); 
      filterArray.add(str1); 
      con.close(); 
     } 
    } catch (Exception ex) { 
     log.error("Error in getting keywords from database" + ex.toString()); 
     JOptionPane.showMessageDialog(null, "Error in getting keywords from database" + ex.toString()); 
    } 

    if (filterArray.size() > 0) { 
     keywordssearcher.setModel(new DefaultComboBoxModel(filterArray.toArray())); 
     keywordssearcher.setSelectedItem(enteredText); 
     keywordssearcher.showPopup(); 
    } else { 
     keywordssearcher.hidePopup(); 
    } 
} 

檢索匹配的數據在這裏我keywordssearcher(JComboBox中)是可編輯和輸入值可直接添加到數據庫中。

將此作爲參考並修改代碼

+0

我想創建我自己的組件,因爲我有我自己的規範,我沒有檢索數據的問題....我唯一的問題是這個 – grotibo