链接的ComboBox JTable单元格编辑器

时间:2015-02-01 19:06:29

标签: java swing jtable tablecelleditor

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

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

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

1 个答案:

答案 0 :(得分:4)

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

例如,这样的事情可能是:

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();
         }
      });
   }
}