2011-10-24 12 views

答えて

1

基本的に、コンボに適切なリスナーをインストールして、明示的にポップアップを開く必要があります。 「適切な」の最初の候補はAncestorListenerで、ancestorAddedメソッドのポップアップを表示します。

これは全体的な話ではないようです。表のsurrenderFocusプロパティがfalseの場合に機能します。 trueの場合、編集不可能なコンボだけが動作します。いくつかの掘り出した後、ancestorListenerによってポップアップが開かれた後、非動作部分の理由が内部focustransfer(コンボからテキストフィールドまで)であることが判明しました。その場合、エディタのeditingComponentがフォーカスを永久に得た後にポップアップを開く第2のリスナーが必要です。

複数のリスナーは、通常、互いの足に踏み込んでいるため、両方を永続的にインストールするのではなく、getEditorCompを呼び出すたびに行うのが最良です。以下は、それを行う方法の実際の例です。ちょうど注意してください:正式にはテストされていません!

public static class DefaultCellEditorX extends DefaultCellEditor { 
    private AncestorListener ancestorListener; 
    private PropertyChangeListener focusPropertyListener; 

    public DefaultCellEditorX(JComboBox comboBox) { 
     super(comboBox); 
    } 

    /** 
    * Overridden to install an appriate listener which opens the 
    * popup when actually starting an edit. 
    * 
    * @inherited <p> 
    */ 
    @Override 
    public Component getTableCellEditorComponent(JTable table, 
      Object value, boolean isSelected, int row, int column) { 
     super.getTableCellEditorComponent(table, value, isSelected, row, column); 
     installListener(table); 
     return getComponent(); 
    } 

    /** 
    * Shows popup. 
    */ 
    protected void showPopup() { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       getComponent().setPopupVisible(true); 
      } 
     }); 
    } 


    /** 
    * Dynamically install self-uninstalling listener, depending on JComboBox 
    * and JTable state. 
    * @param table 
    */ 
    private void installListener(JTable table) { 
     if (getComponent().isEditable() && table.getSurrendersFocusOnKeystroke()) { 
      installKeyboardFocusListener(); 
     } else { 
      installAncestorListener(); 
     } 
    } 

    private void installAncestorListener() { 
     if (ancestorListener == null) { 
      ancestorListener = new AncestorListener() { 

       @Override 
       public void ancestorAdded(AncestorEvent event) { 
        getComponent().removeAncestorListener(ancestorListener); 
        showPopup(); 
       } 

       @Override 
       public void ancestorRemoved(AncestorEvent event) { 
       } 

       @Override 
       public void ancestorMoved(AncestorEvent event) { 
       } 

      }; 
     } 
     getComponent().addAncestorListener(ancestorListener); 
    } 

    private void installKeyboardFocusListener() { 
     if (focusPropertyListener == null) { 
      focusPropertyListener = new PropertyChangeListener() { 

       @Override 
       public void propertyChange(PropertyChangeEvent evt) { 
        LOG.info("property: " + evt.getPropertyName()); 
        if (focusManager().getPermanentFocusOwner() != 
         getComponent().getEditor().getEditorComponent()) return; 
        focusManager() 
         .removePropertyChangeListener("permanentFocusOwner", focusPropertyListener); 
        showPopup(); 
       } 

      }; 
     } 
     focusManager().addPropertyChangeListener("permanentFocusOwner", focusPropertyListener); 
    } 

    /** 
    * Convience for less typing. 
    * @return 
    */ 
    protected KeyboardFocusManager focusManager() { 
     return KeyboardFocusManager.getCurrentKeyboardFocusManager(); 
    } 

    /** 
    * Convenience for type cast. 
    * @inherited <p> 
    */ 
    @Override 
    public JComboBox getComponent() { 
     return (JComboBox) super.getComponent(); 
    } 

} 
+0

私はコンボボックスセルエディタ上SwingX AutoCompleteDecoratorを使用しようとしています、と私は常にいくつかの点でフォーカスを失うので、私は... – DejanLekic

+0

@DejanLekic問題を抱えている - ええ、デコレータがmuddyingさを水をさらに。あなたが達成する必要があるものを正確に説明している小規模なプログラムを用意してください。 SwingXのオートコンプリートマジックの現在のマスターです:-) – kleopatra

+0

フィルタリングされたコンボボックス(http://snippets.dzone.com/posts)の実装をしようとしています。/show/7633)がセルエディタになります。 JTableのキーを押すと、そのキーをAutoCompleteComboに「転送」し、そのキーに一致する項目のリストを直ちにドロップする必要があります。 – DejanLekic

0
JTable table = new JTable(data, columns); 
    table.putClientProperty("terminateEditOnFocusLost", true); 
    JScrollPane scrollPane = new JScrollPane(table); 
    final JXComboBox editorComboBox = new JXComboBox(array); 
    editorComboBox.addAncestorListener(new AncestorListener() { 
     public void ancestorAdded(AncestorEvent event) { 
      //make sure combobox handles key events 
      editorComboBox.requestFocusInWindow(); 
     } 
     public void ancestorMoved(AncestorEvent event) {} 
     public void ancestorRemoved(AncestorEvent event) {} 
    }); 
    AutoCompleteDecorator.decorate(editorComboBox); 
    TableColumn column = table.getColumnModel().getColumn(0); 
    column.setCellEditor(new ComboBoxCellEditor(editorComboBox)); 
関連する問題