下面列出了javax.swing.JTable#getCellEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void actionPerformed(ActionEvent ae) {
JTable jt = (JTable) ae.getSource();
if (jt != null) {
if (jt.isEditing()) {
TableCellEditor tce = jt.getCellEditor();
if (PropUtils.isLoggable(BaseTable.class)) {
PropUtils.log(BaseTable.class, "Cancelling edit due to keyboard event"); //NOI18N
}
if (tce != null) {
jt.getCellEditor().cancelCellEditing();
}
} else {
//If we're in a dialog, try to close it
trySendEscToDialog(jt);
}
}
}
public void actionPerformed(ActionEvent e) {
JTable table = (JTable)e.getSource();
if (!table.hasFocus()) {
CellEditor cellEditor = table.getCellEditor();
if (cellEditor != null && !cellEditor.stopCellEditing()) { return; }
table.requestFocus();
return;
}
ListSelectionModel rsm = table.getSelectionModel();
int anchorRow = rsm.getAnchorSelectionIndex();
table.editCellAt(anchorRow, PropertySheetTableModel.VALUE_COLUMN);
Component editorComp = table.getEditorComponent();
if (editorComp != null) {
editorComp.requestFocus();
}
}
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if(!SwingUtilities.isLeftMouseButton(e)) {
return;
}
JTable t = getTable();
Point p = e.getPoint();
int row = t.rowAtPoint(p);
int column = t.columnAtPoint(p);
int rowCount = t.getRowCount();
int columnCount = t.getColumnCount();
if(column < 0 || row < 0 || column >= columnCount || row >= rowCount ) {
return;
}
TableCellEditor tce = t.getCellEditor();
if((tce==null) || (tce.shouldSelectCell(e))) {
t.requestFocus();
updateTableSelectionModel(row, column, e.isControlDown(), e.isShiftDown(), false);
t.repaint();
}
}
void update() {
JTable table = provider.getTable();
TableCellEditor cellEditor = table.getCellEditor();
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
populateMap();
}
public final Map<String,String> getData() {
Map<String,String> retVal = new HashMap<String,String>(getDataComponents().size());
for (Component c : getDataComponents()) {
// fill in the blanks...
String compName = c.getName();
if (compName != null) {
// construct the key
String key = compName;
if (c instanceof JComboBox) {
final JComboBox jcb = (JComboBox) c;
retVal.put(key, (String) jcb.getSelectedItem());
} else if (c instanceof JTextComponent) {
final JTextComponent jtc = (JTextComponent) c;
retVal.put(key, jtc.getText());
} else if (c instanceof AbstractButton) {
AbstractButton ab = (AbstractButton) c;
retVal.put(key, Boolean.toString(ab.isSelected()));
} else if (c instanceof JTable) {
JTable table = (JTable) c;
TableCellEditor tce = table.getCellEditor();
if (null != tce) {
tce.stopCellEditing();
}
Object tm = table.getModel();
if (tm instanceof DataTableModel) {
DataTableModel model = (DataTableModel) tm;
retVal.putAll(model.getData());
}
}
}
}
return retVal;
}
public final Map<String,String> getData() {
Map<String,String> retVal = new HashMap<String,String>(getDataComponents().size());
for (Component c : getDataComponents()) {
// fill in the blanks...
String compName = c.getName();
if (compName != null) {
// construct the key
String key = compName;
if (c instanceof JComboBox) {
final JComboBox jcb = (JComboBox) c;
retVal.put(key, (String) jcb.getSelectedItem());
} else if (c instanceof JTextComponent) {
final JTextComponent jtc = (JTextComponent) c;
retVal.put(key, jtc.getText());
} else if (c instanceof AbstractButton) {
AbstractButton ab = (AbstractButton) c;
retVal.put(key, Boolean.toString(ab.isSelected()));
} else if (c instanceof JTable) {
JTable table = (JTable) c;
TableCellEditor tce = table.getCellEditor();
if (null != tce) {
tce.stopCellEditing();
}
Object tm = table.getModel();
if (tm instanceof DataTableModel) {
DataTableModel model = (DataTableModel) tm;
retVal.putAll(model.getData());
}
}
}
}
return retVal;
}
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
if(!SwingUtilities.isLeftMouseButton(e)) {
return;
}
JTable t = getTable();
Point p = e.getPoint();
int row = t.rowAtPoint(p);
int column = t.columnAtPoint(p);
int rowCount = t.getRowCount();
int columnCount = t.getColumnCount();
if(column < 0 || row < 0 || column >= columnCount || row >= rowCount ) {
return;
}
TableCellEditor tce = t.getCellEditor();
if(tce==null) {
t.requestFocus();
updateTableSelectionModel(row, column, e.isControlDown(), !e.isShiftDown(), true);
t.repaint();
}
}
@Override
public void mouseClicked(MouseEvent e) {
JTableHeader h = (JTableHeader) e.getSource();
JTable table = h.getTable();
int selectedRow = table.getSelectedRow();
TableModel model = table.getModel();
//remember selection to keep after sorting
Object selectedAction=null;
int objectColumn=-1;
if(selectedRow>-1) {
for(int i=0; i<table.getColumnCount(); i++) {
//first find colum with appropriate object
if(model.getValueAt(selectedRow, i) instanceof ActionHolder) {
//remember this object
selectedAction=model.getValueAt(selectedRow, i);
objectColumn=i;
//stop edition as we click somewhere ouside of editor
TableCellEditor editor=table.getCellEditor();
if(editor!=null) {
editor.stopCellEditing();
}
break;
}
}
}
TableColumnModel columnModel = h.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = columnModel.getColumn(viewColumn).getModelIndex();
if (column != -1) {
int status = getSortingStatus(column);
if (!e.isControlDown()) {
cancelSorting();
}
// Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
// {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
status = status + (e.isShiftDown() ? -1 : 1);
status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
setSortingStatus(column, status);
//reselect the same object
if(selectedAction!=null)setSelectedRow(table, selectedAction, objectColumn);
}
}
public void testEditors() {
setUpModel();
JTable t = ot.treeTable.getTable();
ot.revalidate();
javax.swing.JFrame f = new javax.swing.JFrame();
f.setLayout(new BorderLayout());
f.add(ot, BorderLayout.CENTER);
f.setSize(600, 500);
f.setVisible(true);
//while (f.isVisible()) {
try {
Thread.sleep(333);
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
}
//}
System.out.println("table rows = "+t.getRowCount());
TableCellEditor tce = t.getCellEditor(0, 0);
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 0)));
//assertTrue(t+"is not editable.", t.isCellEditable(0, 0));
Component c = tce.getTableCellEditorComponent(t, null, true, 0, 0);
//System.err.println("c = "+c);
assertTrue("Editor component = "+c, c instanceof EditorComponent);
assertEquals("Editor of 0:DN", ((EditorComponent) c).getText());
tce = t.getCellEditor(0, 1);
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 1)));
assertTrue(t+"is not editable.", t.isCellEditable(0, 1));
c = tce.getTableCellEditorComponent(t, null, true, 0, 1);
assertTrue("Editor component = "+c, c instanceof EditorComponent);
assertEquals("Editor of 0:col1", ((EditorComponent) c).getText());
tce = t.getCellEditor(0, 2);
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 2)));
assertTrue(t+"is not editable.", t.isCellEditable(0, 2));
c = tce.getTableCellEditorComponent(t, null, true, 0, 2);
assertTrue("Editor component = "+c, c instanceof EditorComponent);
assertEquals("Editor of 0:col2", ((EditorComponent) c).getText());
tce = t.getCellEditor(1, 0);
assertFalse(tce+"is editable.", tce.isCellEditable(getMouseClickAt(t, 1, 0)));
assertFalse(t+"is editable.", t.isCellEditable(1, 0));
c = tce.getTableCellEditorComponent(t, null, true, 1, 0);
assertFalse("Editor component = "+c, c instanceof EditorComponent);
tce = t.getCellEditor(1, 2);
assertFalse(tce+"is editable.", tce.isCellEditable(getMouseClickAt(t, 1, 2)));
assertFalse(t+"is editable.", t.isCellEditable(1, 2));
c = tce.getTableCellEditorComponent(t, null, true, 1, 2);
assertFalse("Editor component = "+c, c instanceof EditorComponent);
tce = t.getCellEditor(3, 1);
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 3, 1)));
assertTrue(t+"is not editable.", t.isCellEditable(3, 1));
c = tce.getTableCellEditorComponent(t, null, true, 3, 1);
assertTrue("Editor component = "+c, c instanceof EditorComponent);
assertEquals("Editor of 3:col1", ((EditorComponent) c).getText());
tce = t.getCellEditor(6, 0);
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 6, 0)));
assertTrue(t+"is not editable.", t.isCellEditable(6, 0));
c = tce.getTableCellEditorComponent(t, null, true, 6, 0);
assertTrue("Editor component = "+c, c instanceof EditorComponent);
assertEquals("Editor of 6:DN", ((EditorComponent) c).getText());
tce = t.getCellEditor(9, 2);
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 9, 2)));
assertTrue(t+"is not editable.", t.isCellEditable(9, 2));
c = tce.getTableCellEditorComponent(t, null, true, 9, 2);
assertTrue("Editor component = "+c, c instanceof EditorComponent);
assertEquals("Editor of 9:col2", ((EditorComponent) c).getText());
}
/**
* stops the tmodel cell editing
*
* @param table the target tmodel
*/
public static void cancelEditing(JTable table) {
if (table.getCellEditor() != null) {
table.getCellEditor().cancelCellEditing();
}
}
public static void stopEditing(JTable table) {
if (null != table.getCellEditor()) {
table.getCellEditor().stopCellEditing();
}
}