package com.example.client.utils; import com.example.client.dto.JComboBoxItem; import org.apache.commons.lang3.math.NumberUtils; import javax.swing.*; import java.awt.*; import java.util.Map; // 自定义编辑器 public class CellComboBoxEditor extends DefaultCellEditor { private final Map itemMap; public CellComboBoxEditor(JComboBox comboBox, Map itemMap) { super(comboBox); this.itemMap = itemMap; } @Override public Object getCellEditorValue() { JComboBoxItem selectedItem = (JComboBoxItem) super.getCellEditorValue(); if (selectedItem != null) { return selectedItem.getId(); // 返回 id } return null; } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if (NumberUtils.isDigits(value.toString())) { String valueStr = value.toString(); long id = Long.parseLong(valueStr); JComboBoxItem item = itemMap.get(id); if (item != null) { ((JComboBox) getComponent()).setSelectedItem(item); // 设置选中项 } } return super.getTableCellEditorComponent(table, value, isSelected, row, column); } }