简体   繁体   English

链接的ComboBox JTable单元格编辑器

[英]Linked ComboBox JTable Cell Editors

I have a table with two comboBox columns. 我有一个带有两个comboBox列的表。 The combo box in the first column always has the same values. 第一列中的组合框始终具有相同的值。 This is pretty simple: 这很简单:

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

The next bit is trickier: I want the values in the second column to depend on which value is selected in the first column, so each row will have different values. 下一点比较棘手:我希望第二列中的值取决于第一列中选​​择的值,因此每一行将具有不同的值。 Can I assign an editor to a specific cell rather than to an entire column? 我可以将编辑器分配给特定的单元格而不是整个列吗?

You don't want a separate editor object for cells, but instead really want one single editor for the column. 您不希望为单元格使用单独的编辑器对象,而实际上希望为列使用一个单独的编辑器。 What needs to change is the data held by the cell and displayed by the editor. 需要更改的是单元格保存并由编辑器显示的数据。 This data can be cell specific and coded for in your editor. 此数据可以是特定于单元格的,并可以在编辑器中进行编码。 You'll likely want to extend the DefaultCellEditor class to allow you to do this. 您可能需要扩展DefaultCellEditor类以允许您执行此操作。

For example, something like this perhaps: 例如,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM