|
发表于 2020-10-15 15:00:01
|
显示全部楼层
呵呵,我以前和你差不多,不过我是设置editor,当时找了很多资料,以下是我设置指定单元格的CellEditor的方法,希望对你有帮助。
EachRowEditor.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class EachRowEditor implements TableCellEditor {
protected Hashtable editors; //哈希表
protected TableCellEditor editor, defaultEditor; //编辑器
JTable table; //这个变量似乎没用到
public EachRowEditor(JTable table) {
this.table = table;
editors = new Hashtable();
defaultEditor = new DefaultCellEditor(new JTextField()); //用JTextField构造编辑器
}
public void setEditorAt(int row, TableCellEditor editor) { //关键
editors.put(new Integer(row),editor); //指定编辑器加入哈希表
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
return editor.getTableCellEditorComponent(table,
value, isSelected, row, column);
}
public Object getCellEditorValue() {
return editor.getCellEditorValue();
}
public boolean stopCellEditing() {
return editor.stopCellEditing();
}
public void cancelCellEditing() {
editor.cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
selectEditor((MouseEvent)anEvent);
return editor.isCellEditable(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
editor.addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
editor.removeCellEditorListener(l);
}
public boolean shouldSelectCell(EventObject anEvent) {
selectEditor((MouseEvent)anEvent);
return editor.shouldSelectCell(anEvent);
}
protected void selectEditor(MouseEvent e) { //这个没看明白
int row;
if (e == null) {
row = table.getSelectionModel().getAnchorSelectionIndex();
} else {
row = table.rowAtPoint(e.getPoint());
}
editor = (TableCellEditor)editors.get(new Integer(row));
if (editor == null) {
editor = defaultEditor;
}
}
}
设置用以下方法:
EachRowEditor rowEditor = new EachRowEditor(JTable); //JTable那个参数似乎没用,null应该也可以
rowEditor.setEditorAt(行号, new DefaultCellEditor(jComboBox1)); //用jComboBox1构造编辑器,并指定行
jTable1.getColumnModel().getColumn(列号).setCellEditor(rowEditor); //设置编辑器 |
|