2014-01-18 2 views
0

Google 검색 상자를 만들었 기 때문에 입력을 누르기 전까지 JList에 항목을 추가하지 않으면됩니다. 나는 "입력"키를 "읽지"않는 콤보 상자라고 생각합니다."enter"키를 누른 후 JList에 추가

public void count(){ 

    try{ 

     String sql2 = "select count(*) from workers_info"; 

     stmt = conn.prepareStatement(sql2); 

     rs=stmt.executeQuery(); 
     while(rs.next()){ 
     String x = rs.getString("count(*)"); 

     z = Integer.parseInt(x); 

     } 

     auto = new String[z]; 

    } 


     catch(SQLException | NumberFormatException e){ 

     } 

} 


public void cB(){ 

    try{ 

      String sql = "Select concat(first_name, ' ',last_name) as full_name from workers_info"; 

      stmt = conn.prepareStatement(sql); 
      rs=stmt.executeQuery(); 

      while(rs.next()){ 

       String name = rs.getString("full_name"); 

       auto[i] = name; 

       i++; 
      } 

      AutoCompleteSupport a = AutoCompleteSupport.install(comboSearch, GlazedLists.eventListOf(auto)); 
      a.setStrict(false); 
      comboSearch.isEditable(); 


     } 

     catch(SQLException e){ 

     } 

    } 

    //this actually is my main concern..why it cant detect when i hit "enter" key? 

private void comboSearchKeyPressed(java.awt.event.KeyEvent evt) {         
    String s1 = (String)comboSearch.getSelectedItem(); 
    if(evt.getKeyCode()==KeyEvent.VK_ENTER){ 

     model.addElement(s1); 

     workerList.setModel(model); 

     comboSearch.setSelectedItem(null); 
     } 
} 

답변

0

그런 목적으로 KeyListener 대신 Key bindings을 사용하는 것이 좋습니다. 다음 간단한 예를 시도해보십시오.

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 

import javax.swing.AbstractAction; 
import javax.swing.DefaultListModel; 
import javax.swing.JComboBox; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JScrollPane; 
import javax.swing.KeyStroke; 

public class Example extends JFrame{ 

    private JComboBox<String> comboBox; 
    private JList<String> list; 
    private DefaultListModel<String> model; 

    Example(){ 
      comboBox = new JComboBox<>(new String[]{"111","222","333"}); 
      list = new JList<>(model = new DefaultListModel<>()); 
      add(comboBox,BorderLayout.SOUTH); 
      add(new JScrollPane(list)); 

      addKeyBindings((JComponent) getContentPane()); 
      addKeyBindings(comboBox); 

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      pack(); 
      setVisible(true); 
    } 

    private void addKeyBindings(JComponent c) { 
     c.getInputMap(JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ENTER"), "doSomething"); 
     c.getActionMap().put("doSomething", new AbstractAction() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       Object selectedItem = comboBox.getSelectedItem(); 
       if(selectedItem != null){ 
        model.addElement((String)selectedItem); 
       } 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     new Example(); 
    } 

} 
+0

감사합니다. bro : 급여 시스템에서 일하고 있습니다.이 사이트는 정말 나를 도와 줬습니다. – codeLover