Reputation: 1016
I am using a TableCellEditor For my column (ButtonColumns) as follows. When i enter the down key the key event associated with the Jtable not fired . Please guide me regarding this obstacle, Los of Thanks in advance. The below SSCCE is as follows
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"));}
// Below Event is not responding on the Down Key //whose key code is 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);
}
});
} }
}
Upvotes: 1
Views: 525
Reputation: 205785
Instead of trying to force a JLabel
and a KeyEvent
into service as a table cell editor, use an actual TableCellEditor
such as @camickr's ButtonColumn
. This TableTest
illustrates one way to use ButtonColumn
in a JTable
. The advantage is that you get all the familiar key bindings for navigation (arrow keys) and activation (space key).
Upvotes: 1