链接的 ComboBox JTable 单元格编辑器

Linked ComboBox JTable Cell Editors

我有一个包含两个组合框列的 table。第一列中的组合框始终具有相同的值。这很简单:

jTableEditTransaction.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(accountClassComboBox));

下一点比较棘手:我希望第二列中的值取决于第一列中选​​择的值,因此每一行都有不同的值。我可以将编辑器分配给特定的单元格而不是整个列吗?

您不需要为单元格使用单独的编辑器对象,而是真正需要为列使用一个编辑器。需要更改的是单元格保存并由编辑器显示的数据。此数据可以是特定于单元格的,并在您的编辑器中进行编码。您可能希望扩展 DefaultCellEditor class 以允许您执行此操作。

例如,可能是这样的:

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class LinkedCombBoxEditors extends JPanel {
   public static final String[] DAY_TYPE = {"Weekday", "Weekend"};
   private static final String[] COL_NAMES = {"Day Type", "Day"};
   private Map<String, DefaultComboBoxModel<String>> keyMap = new HashMap<>();
   private JComboBox<String> innerEditor = new JComboBox<>();

   public LinkedCombBoxEditors() {
      DefaultComboBoxModel<String> cModel = new DefaultComboBoxModel<>();
      cModel.addElement("Monday");
      cModel.addElement("Tuesday");
      cModel.addElement("Wednesday");
      cModel.addElement("Thursday");
      cModel.addElement("Friday");
      keyMap.put(DAY_TYPE[0], cModel);

      cModel = new DefaultComboBoxModel<>();
      cModel.addElement("Saturday");
      cModel.addElement("Sunday");
      keyMap.put(DAY_TYPE[1], cModel);

      DefaultTableModel model = new DefaultTableModel(COL_NAMES, 4);
      JComboBox<String> comboBox0 = new JComboBox<>(DAY_TYPE);
      JTable table = new JTable(model);
      table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(comboBox0));
      table.getColumnModel().getColumn(1).setCellEditor(new ComboEditor1(innerEditor));

      add(new JScrollPane(table));
   }

   private class ComboEditor1 extends DefaultCellEditor {

      private ComboBoxModel<String> emptyModel = new DefaultComboBoxModel<>();

      public ComboEditor1(JComboBox<String> innerEditor) {
         super(innerEditor);
      }

      @Override
      public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
         Component editorComp = super.getTableCellEditorComponent(table, value, isSelected, row, column);
         JComboBox<String> comboBox = (JComboBox<String>)editorComp; 
         Object item = table.getValueAt(row, 0);
         if (item != null) {
            DefaultComboBoxModel<String> comboModel = keyMap.get(item);
            comboBox.setModel(comboModel);
         } else {
            comboBox.setModel(emptyModel);
         }
         // DefaultComboBoxModel<String> model = new 
         return editorComp;
      }
   }

   private static void createAndShowGui() {
      LinkedCombBoxEditors mainPanel = new LinkedCombBoxEditors();

      JFrame frame = new JFrame("LinkedCombBoxEditors");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}