在Swing中更改JTable列中的Dropdown内容

时间:2014-11-19 05:31:31

标签: java swing jtable jcombobox tablecelleditor

我有一个JTable,其中第一列包含每个单元格中具有相同项目的组合框。如果我在单元格组合框中选择一个项目,我需要从该列中的所有其他组合框中删除所选项目并添加之前的选择项目到所有其他组合框。我应该怎么做?请帮我举个例子。

public class Save extends JFrame {
  String[] items1 = new String[] { "Cash", "Bank1", "Bank2" ,"Bank3"};
  TableCellEditor editors;
  DefaultTableModel dtmFunds;
  private JComboBox comboBox1;
  private JTable jtblFunds;

  private void loadTable(){
   comboBox1=new JComboBox(items1);
     comboBox1.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {

                    int x=comboBox1.getSelectedIndex();
                    comboItem= e.getItem().toString();
                }
            }
        }); 
     editors=new DefaultCellEditor(comboBox1);

    dtmFunds = new DefaultTableModel(new Object[][] {{"", " ","delete"}}, new Object[] {"Fund Store", "Amount","Action"});  
    jtblFunds=new JTable(dtmFunds){
        public TableCellEditor getCellEditor(int row,int column){

            int modelColumn=convertColumnIndexToModel(column);
            if(modelColumn==0 && row<jtblFunds.getRowCount()-1)
                return editors;
            else
                return super.getCellEditor(row,column);
        }
    };
    jtblFunds.setModel(dtmFunds);
    jtblFunds.getModel().addTableModelListener(new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent e) {                   
            int row=e.getFirstRow();
            int column=e.getColumn();
            if((column==0)&&(row<jtblFunds.getRowCount()-1)){
               System.out.println("Dropdown changed  "+row);
           }

        }
    });

  }
  }

这些是我用来将组合框添加到名为“Fund Store”的JTable列的代码。

2 个答案:

答案 0 :(得分:2)

真的,把你的精力集中在CellEditor本身,这就是它的工作。没有必要从JTable延伸或使用它。

import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

public class TestCellEditor {

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

    public TestCellEditor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                List<String> values = new ArrayList<>(5);
                values.add("Bananas");
                values.add("Apples");
                values.add("Oranages");
                values.add("Grapes");
                values.add("Pears");

                ComboBoxTableCellEditor editor = new ComboBoxTableCellEditor(values);
                DefaultTableModel model = new  DefaultTableModel(new Object[]{"Fruit"}, 5);
                JTable table = new JTable(model);
                table.getColumnModel().getColumn(0).setCellEditor(editor);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ComboBoxTableCellEditor extends AbstractCellEditor implements TableCellEditor {

        private JComboBox editor;
        private List<String> masterValues;

        public ComboBoxTableCellEditor(List<String> masterValues) {
            this.editor = new JComboBox();
            this.masterValues = masterValues;
        }

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

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

            DefaultComboBoxModel model = new DefaultComboBoxModel(masterValues.toArray(new String[masterValues.size()]));
            for (int index = 0; index < table.getRowCount(); index++) {
                if (index != row) {
                    String cellValue = (String) table.getValueAt(index, 0);
                    model.removeElement(cellValue);
                }
            }

            editor.setModel(model);
            editor.setSelectedItem(value);

            return editor;

        }
    }

}

我更喜欢有两个值池,一个是主列表,另一个是选定值,每次调用它时编写编辑器会更容易,更快,但这是基本的想法...

答案 1 :(得分:1)

您实际需要的只是更新用于编辑器的组合框的模型。

comboBox1.setModel(new DefaultComboBoxModel(theArrayWithRemovedValue));

您有基本的组合框项目(源代码)和结果数组,其中应删除所选项目。

只需重新创建删除单元格value的数组。

顺便说一句。最好定义单元编辑器标准方式,而不是覆盖JTable的getCellEditor()方法。使用例如public void setDefaultEditor(Class<?> columnClass, TableCellEditor editor)。您可以为列0定义自定义类。