2014-11-25 4 views
0

JComboBox 드롭 다운에 DocumentListener 클래스를 추가하려고하는데 상자 오른쪽에 오류 메시지가 나타납니다.JCombobox에 DocumentListener 추가

클래스를 만들지 만 JComboBox에서 클래스를 추가/생성하는 중 오류가 발생합니다.

오류 메시지 : 스레드 예외는 "주요"상위를 : 해결되지 않은 컴파일 문제 : 유형의 컨테이너에 메소드 추가 (구성 요소) test.TestGUI에서 인수 (CountyDocumentListener)에 적용 할 수 없습니다. test.TestGUI.main에서 (TestGUI.java:52) (TestGUI.java:20는)

목표는하고의 DocumentListener를 사용하여 모든 드롭 다운에 자동 완성을 추가하는 것입니다. 분명히 배열은 더 커질 것이므로 자동 완성을 시도하려고합니다.

DocumentListener에서이 작업을 수행 할 수 있습니까? 아니면 다른 방법이 있습니까? 그렇다면 모든 드롭 다운에 대해 별도의 doculistener 클래스가 필요합니까, 아니면 어떻게 모든 것을 구성해야합니까? (저는 SwingX와 같은 것을 사용하고 싶지 않습니다. 직접 만들고 싶습니다.)

public class TestGUI extends JFrame implements ActionListener { 

    private JPanel content; 


    String[] county = { " ", "Orange", "Placer", "Napa", "LA", "Kings" }; 
    String[] muni = { " ", "Anaheim", "Agoura Hills", "Auburn", "Avalon", "Calistoga" }; 
    String[] place = { " ", "Berkeley", "Calistoga", "El Toro", "Glendale", "Corcoran" }; 

    public static void main(String[] args) { 
     TestGUI frame = new TestGUI(); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    @SuppressWarnings("rawtypes") 
    public TestGUI() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     content = new JPanel(); 
     content.setLayout(new BorderLayout()); 
     setContentPane(content); 


     JPanel rightPanel = new JPanel(); 
     content.add(rightPanel, BorderLayout.EAST); 
     rightPanel.add(new TriGoButton()); 


     JPanel leftPanel = new JPanel(); 
     content.add(leftPanel, BorderLayout.WEST); 

     JPanel centerPanel = new JPanel(); 
     content.add(centerPanel, BorderLayout.CENTER); 
     centerPanel.setLayout(new GridLayout(3, 3, 0, 20)); 

     JLabel countyLbl = new JLabel("County"); 
     centerPanel.add(countyLbl); 

     JComboBox countyDropDown = new JComboBox(county); 
     centerPanel.add(countyDropDown); 
     countyDropDown.setEditable(true); 
     countyDropDown.add(new CountyDocumentListener()); // right here 

     JLabel muniLbl = new JLabel("Munipalicity"); 
     centerPanel.add(muniLbl); 

     JComboBox muniDropDown = new JComboBox(muni); 
     centerPanel.add(muniDropDown); 
     muniDropDown.setEditable(true); 

     JLabel placeLbl = new JLabel("City or place"); 
     placeLbl.setToolTipText("search"); 
     centerPanel.add(placeLbl); 

     JComboBox placeDropDown = new JComboBox(place); 
     centerPanel.add(placeDropDown); 
     placeDropDown.setEditable(true); 


     JPanel bottomPanel = new JPanel(); 
     content.add(bottomPanel, BorderLayout.SOUTH); 

     JPanel topPanel = new JPanel(); 
     content.add(topPanel, BorderLayout.NORTH); 

     JLabel headlineLbl = new JLabel("headline"); 
     topPanel.add(headlineLbl); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     //////  
    } 
} 

.

public class CountyDocumentListener implements DocumentListener { 

    //public public CountyDocumentListener() { 
     // TODO Auto-generated constructor stub 
    //} 

    @Override 
    public void changedUpdate(DocumentEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void insertUpdate(DocumentEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void removeUpdate(DocumentEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 
+2

countyDropDown.getEditor을 JComboBox AutoCompletion

를 참조하거나 사용하는 콤보 상자의 텍스트 필드에 문서 리스너를 추가, 그래서 달성을 돕기 위해 ((JTextField)countyDropDown.getEditor().getEditorComponent()).getDocument().addDocumentListener(new CountyDocumentListener())

를 사용한다 ..... addDocumentListener (XxxXxx) – mKorbel

+1

'목표는 documentlistener를 사용하여 모든 드롭 다운에 자동 완성을 추가하는 것입니다.'== 그렇게하지 말고 재발 명하지 마십시오. (추상화) 문서를 만들고, 모든 이벤트를 오버라이드하고,보기 (Caret, Selection)를 보호하기 위해 올바르게 작성해야합니다. 일반 편집 가능한 JCombobBox뿐만 아니라 많은 행운 – mKorbel

+0

Ok. 어떻게 든 내 머리 속에서 쉽게 들렸다. - userInput이 A> show Avalon, Auburn ...으로 표시되면 드롭 다운에 표시됩니다. 배열을 사전 순으로 정렬하고 입력란의 첫 번째 문자열을 추천하십시오. – cashmeer

답변

1

당신은 당신의 목표는 Swingx 자동 완성을 Swingx Autocompletion

+0

좋아, 그냥 Swingx를 사용해야 할 수도 있습니다. – cashmeer

관련 문제