2011-09-29 14 views
3

私はテーブルを持っています。そのテーブルの更新データベースの変更。 1つの列は、その表のJComboBoxによって編集されます。その列の任意のセルをクリックすると、tableChangedイベントが発生します。ただし、JComboBoxの項目を選択してから起動する必要があります。どのように選択後にtableChangedを発生させることができますか?カスタムTableCellEditorとしてのJComboBox

public class JIDCellEditor extends AbstractCellEditor implements TableCellEditor { 

    JComboBox jComboBox; 

    @Override 
    public Object getCellEditorValue() { 
     return jComboBox.getSelectedItem(); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { 
     Vector vector = new Vector(); 
     vector.add(0); 
     for (int i = 0; i < table.getRowCount(); i++) { 
      if (!vector.contains(table.getValueAt(i, 0)) && table.getValueAt(i, 3).toString().equals("Female")) { 
       vector.add(table.getValueAt(i, 0)); 
      } 
     } 
     vector.remove(table.getValueAt(row, 0)); 
     jComboBox = new JComboBox(vector); 
     jComboBox.setSelectedItem(value); 
     return jComboBox; 
    } 
} 
+0

コンボボックスをエディタに使用します。チュートリアルとこのフォーラムへの投稿の両方でこれを行う方法のいくつかの例があります。私はそれを試してみることをお勧めします。失敗した場合は、あなたの最善の試みを示す[SSCCE](http://SSCCE.org)を投稿してください。 –

+0

私はエディター用にコンボボックスを使用します。 – MOD

+1

また、あなたのクエストでの入力が決して終わったかのように見えます。 –

答えて

4

これで機能します。 getTableCellEditorComponentメソッドが呼び出されるたびにJComboBoxを再初期化する必要があります。また、このJComboBoxのitemstatechangeでは、itemが選択されたときに編集が完了したことをstopCellEditing()メソッドがリスナーに通知する必要があります。これにより、TableModelListenerのfireTableChangedイベントが作成されます。 (Fixed)ただし、選択せずに別のJComboBoxをクリックした後にJComboBoxをクリックすると、そのイベントが発生します。 (固定)

編集:次のコードが最後のバージョンです。このTableModelListenerによって、項目が選択されたときにのみ通知されます。上記の問題は修正されています。これは、デフォルトのstopCellEditing()メソッドが常にtrueを返すためでした。これにより、予期しない方法でセルの編集が停止します。必要に応じてオーバーライドし、fireEditingStopped(); TableModelListenerに通知するために使用する必要があります。

public class JIDCellEditor extends AbstractCellEditor implements TableCellEditor { 

    private JComboBox jComboBox = new JComboBox(); 
    boolean cellEditingStopped = false; 

    @Override 
    public Object getCellEditorValue() { 
     return jComboBox.getSelectedItem(); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { 
     Vector vector = new Vector(); 
     ArrayList<Integer> arrayList = new ArrayList<Integer>(); 
     arrayList.add(Integer.parseInt(value.toString())); 
     vector.add(0); 
     for (int i = 0; i < table.getRowCount(); i++) { 
      if (!vector.contains(table.getValueAt(i, 0)) && table.getValueAt(i, 3).toString().equals("Sheep")) { 
       vector.add(table.getValueAt(i, 0)); 
      } 
     } 
     vector.remove(table.getValueAt(row, 0)); 

     for (int i = 0; i < vector.size(); i++) { 
     } 
     jComboBox = new JComboBox(vector); 
     jComboBox.setSelectedItem(value); 

     jComboBox.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if (e.getStateChange() == ItemEvent.SELECTED) { 
        fireEditingStopped(); 
       } 
      } 
     }); 
     jComboBox.addPopupMenuListener(new PopupMenuListener() { 

      @Override 
      public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 
       cellEditingStopped = false; 
      } 

      @Override 
      public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 
       cellEditingStopped = true; 
       fireEditingCanceled(); 
      } 

      @Override 
      public void popupMenuCanceled(PopupMenuEvent e) { 

      } 
     }); 
     return jComboBox; 
    } 

    @Override 
    public boolean stopCellEditing() { 
     return cellEditingStopped; 
    } 
} 
6

私は非常にComboBoxCellEditor成分を有するSwingXを使用することをお勧めします。本質的にSunのインキュベーターで、Swingコンポーネントには必要な機能があります。私はまだプロジェクトがまだ活発に開発されているのかどうかはわかりませんが、成熟しており、私は多くのプロジェクトでそれを使っています。

注:何らかの理由で、あなたがまたは外部ライブラリを使用したくないことはできませんが、場合

が、ここでは、そのままコメント(カスタムSwingX機能を削除するように変更部分と)自分のコードですライブラリはGPLのコードです。

/* 
* $Id: ComboBoxCellEditor.java 3738 2010-07-27 13:56:28Z bierhance $ 
* 
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved. 
* 
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as 
* published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. 
* 
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 
* 
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software 
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*/ 

import java.awt.event.ActionEvent; 
import java.awt.event.MouseEvent; 
import java.util.EventObject; 

import javax.swing.DefaultCellEditor; 
import javax.swing.JComboBox; 

/** 
* <p> 
* This is a cell editor that can be used when a combo box (that has been set up for automatic completion) is to be used in a JTable. The 
* {@link javax.swing.DefaultCellEditor DefaultCellEditor} won't work in this case, because each time an item gets selected it stops cell 
* editing and hides the combo box. 
* </p> 
* <p> 
* Usage example: 
* </p> 
* <p> 
* 
    * <pre> 
* <code> 
* JTable table = ...; 
* JComboBox comboBox = ...; 
* ... 
* TableColumn column = table.getColumnModel().getColumn(0); 
* column.setCellEditor(new ComboBoxCellEditor(comboBox)); 
* </code> 
* </pre> 
* 
* </p> 
*/ 
public class ComboBoxCellEditor extends DefaultCellEditor { 

    /** 
    * Creates a new ComboBoxCellEditor. 
    * 
    * @param comboBox the comboBox that should be used as the cell editor. 
    */ 
    public ComboBoxCellEditor(final JComboBox comboBox) { 
     super(comboBox); 

     comboBox.removeActionListener(this.delegate); 

     this.delegate = new EditorDelegate() { 
      @Override 
      public void setValue(final Object value) { 
       comboBox.setSelectedItem(value); 
      } 

      @Override 
      public Object getCellEditorValue() { 
       return comboBox.getSelectedItem(); 
      } 

      @Override 
      public boolean shouldSelectCell(final EventObject anEvent) { 
       if (anEvent instanceof MouseEvent) { 
        final MouseEvent e = (MouseEvent) anEvent; 
        return e.getID() != MouseEvent.MOUSE_DRAGGED; 
       } 
       return true; 
      } 

      @Override 
      public boolean stopCellEditing() { 
       if (comboBox.isEditable()) { 
        // Commit edited value. 
        comboBox.actionPerformed(new ActionEvent(ComboBoxCellEditor.this, 0, "")); 
       } 
       return super.stopCellEditing(); 
      } 

      @Override 
      public void actionPerformed(final ActionEvent e) { 
       ComboBoxCellEditor.this.stopCellEditing(); 
      } 
     }; 
     comboBox.addActionListener(this.delegate); 
    } 
} 
+0

これは、選択されたComboBoxアイテムの後にtableChangedイベントを発生させ、JComboBoxを異なるアイテムのセルで使用できるようにしますか? – MOD

+0

ええ、別のセルの同じコンボボックスを使うことができますが、コンボボックスで利用できる値は同じになります。ドロップダウンの値を異なるようにするには、startCellEditing()を上書きしてそこから値を入力します。 –

+0

私はどのようにしてCelEditing()を開始できますか? – MOD

関連する問題