|
Haha, I used to be similar to you, but I set up the editor and found a lot of information. The following is how I set the CellEditor of the specified cell, I hope it will be helpful to you.
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; //Hashtable
protected TableCellEditor editor, defaultEditor; //Editor
JTable table; //This variable does not seem to be used
public EachRowEditor(JTable table) {
this.table = table;
editors = new Hashtable();
defaultEditor = new DefaultCellEditor(new JTextField()); //Use JTextField to construct the editor
}
public void setEditorAt(int row, TableCellEditor editor) {//Key
editors.put(new Integer(row),editor); //Specify the editor to join the hash table
}
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) {//I don’t understand this
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;
}
}
}
Set up with the following methods:
EachRowEditor rowEditor = new EachRowEditor(JTable); //The parameter of JTable seems useless, null should work too
rowEditor.setEditorAt(row number, new DefaultCellEditor(jComboBox1)); //Use jComboBox1 to construct the editor and specify the row
jTable1.getColumnModel().getColumn(column number).setCellEditor(rowEditor); //Set the editor |
|