2013-01-17 1 views
1

일반적으로 편집기에서 JComponent을 얻으시겠습니까?Swing : 에디터에서 JComponent를 얻는 방법

예 :

는 이제 JComboBox 편집을 보자. 따라서 콤보에는 편집기 (기본값 : JTextField)가 있습니다.

JComboBox b = new JComboBox(); 
b.setEditable(true); 

이제 '글로벌'키보드 수신기를 스윙 응용 프로그램에 추가하십시오.

Toolkit.getDefaultToolkit().addAWTEventListener(
     new AWTEventListener() { 
      public void eventDispatched(AWTEvent e) { 
       JComponent c = (JComponent) e.getSource(); 
       System.out.println(c); // <- printing the event source 
      } 
     }, 
     AWTEvent.KEY_EVENT_MASK); 

키 이벤트의 소스가 b의 편집기가 아닌 콤보 박스이다, 콤보 상자에 b 쇼를 텍스트를 입력의 출력은 자체 b :

javax.swing.plaf.metal.MetalComboBoxEditor$1[,0,0, ... 
javax.swing.plaf.metal.MetalComboBoxEditor$1[,0,0, ... 
javax.swing.plaf.metal.MetalComboBoxEditor$1[,0,0, ... 
... 

는 방식이 있나요 편집기 또는 키 이벤트에서 b에 대한 참조를 얻으시겠습니까? 아니요, '현재 편집 콤보 상자'에 대한 참조를 얻으려면 어떻게해야합니까?

피. 에스. : 제발 나에게 "왜 당신이 그것을 필요로 할 것입니까?"와 같은 질문을하지 마십시오. 감사.

+3

의 getParent() – Petr

+2

를 사용해보십시오 :) ? 귀하의 경우'getParent()'는 무엇을 반환합니까? –

+1

나는 물을 수 없습니다 : 왜 그게 필요하겠습니까? –

답변

2

호출 getEditorComponent(). 이 방법은 인터페이스 ComboBoxEditor에 정의되어

public interface ComboBoxEditor { 

    /** Return the component that should be added to the tree hierarchy for 
    * this editor 
    */ 
    public Component getEditorComponent(); 
    ................ 

을 너무 ComboBoxEditor 처음에 캐스팅 '? 왜 당신이 그것을 필요'우리가 물어해야하는 이유

Component component = (ComboBoxEditor)event.getSource()).getEditorComponent();