Search code examples
javaswingjtabletablecelleditordefaulttablemodel

Need to keep some columns editable and non-editable and allow cell editing on only double click on the cell


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 -


Solution

  • 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();
                }
            });
        }
    }