我正在使用TableCellEditor对于我的列(ButtonColumns),如下所示。 当我输入向下键时,与Jtable相关联的键事件未被触发。请提前告诉我这个障碍,洛杉矶的感谢。以下SSCCE如下
class ButtonEditor_Utility extends DefaultCellEditor {
protected JButton button;
public ButtonEditor_Utility() {
button.setActionCommand(tableName);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
return button;
}
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
Class Test extent JFframe{
public void AddButtonColumn(){
tblDetailInfo.getColumn(1).setCellEditor(
new ButtonEditor_Utility(new JCheckBox(), this, 1, selectedRow, this,null, "TestDB"));}
//在事件下方没有响应Down键 //其密钥代码为40
private void tblDetailInfoKeyPressed(java.awt.event.KeyEvent evt){
// TODO add your handling code here:
if (evt.getKeyCode() == 40) {
int rowId = tblDetailInfo.getRowCount() - 1;
setSelectedRow(rowId);
tblDetailInfo.setCellSelectionEnabled(true);
tblDetailInfo.changeSelection(rowId, 0, false, false);
tblDetailInfo.requestFocus();
tblDetailInfo.scrollRectToVisible(new Rectangle(tblDetailInfo.getCellRect(rowId, 0, true)));
AddDetailRow();
}
}
private void formWindowOpened(java.awt.event.WindowEvent evt){ AddButtonColumn(); }
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Test test = new Test();
test.setVisible(true);
}
});
} }
}
答案 0 :(得分:1)
不要试图强制JLabel
和KeyEvent
作为表格单元格编辑器使用,而是使用实际的TableCellEditor
,例如@ camickr的ButtonColumn
。此TableTest
说明了在JTable
中使用ButtonColumn
的一种方法。优点是您可以获得所有熟悉的key bindings导航(箭头键)和激活(空格键)。