In my implementation of JTable
. I have to keep some columns editable and some columns UN-editable so I have override isCellEditable
-
@Override
public boolean isCellEditable(int row, int col) {
if (col == uneditableColumn) {
return false;
}
return bEdit;
}
Now my requirement is to allow edit the cell only on Double click i.e. if user double clicks on cell then only it comes into editable mode. For this - I will have to make your own CellEditor
and override.
public boolean isCellEditable( EventObject e )
Can someone suggest if can be possible using -
public boolean isCellEditable(int row, int col)
Please help -
is to allow edit the cell only on Double click i.e. if user double clicks
on cell then only it comes into editable mode.
you have look at DefaultCellEditor#clickCountToStart
for CellEditor or methods override isCellEditable (AbstractTableModel)
@Override
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
}
return true;
}
for DefaultTableModel could it be
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.*;
public class EditorRendererClickCountToStart {
public EditorRendererClickCountToStart() {
TableModel model = new DefaultTableModel(new Object[][]{
{"A", "Item 0"}, {"B", "Item 1"}, {"C", "Item 2"},
{"D", "Item 3"}, {"E", "Item 4"}}, new String[]{"TextField", "Combo"});
JTable table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultCellEditor editor = new DefaultCellEditor(new JComboBox(new Object[]{
"Item 0", "Item 1", "Item 2", "Item 3", "Item 4"}));
editor.setClickCountToStart(2);
table.getColumnModel().getColumn(1).setCellEditor(editor);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
System.out.println(info.getName());
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (UnsupportedLookAndFeelException e) {// handle exception
} catch (ClassNotFoundException e) {// handle exception
} catch (InstantiationException e) {// handle exception
} catch (IllegalAccessException e) {// handle exception
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new EditorRendererClickCountToStart();
}
});
}
}
Well, I'd not override isCellEditable(...)
in JTable
but rather provide a custom table model.
From the JavaDoc on JTable#isCellEditable(...)
:
The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.
Additionally the cell might appear to be editable to the table but the editor itself might not provide input fields until it has been double clicked. If you want to make only one cell editable at a time you might store that information in the model as well and let the editors check that.