↓ Skip to main content

How to turn off automatic editing of JTable

·155 words·1 min
Sergii Zolotarov
Author
Sergii Zolotarov
Twenty years of Java, from Kyiv to Leeuwarden. Currently at Booking.com

I’ve got really tired of one JTable feature: you press any button and it starts editing current cell. For instance, your application is setup to save on “Control+S”, but when the table is focused the table intercepts your shortcut and starts editing a cell instead. To fix this go to JTable.processKeyBinding and find the hardcoded flag there JTable.autoStartsEdit (how I love these fckng hidden Swing flags). Now let’s put it to work.


       table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);

All your shortcuts are in your hands :)

After you have done this the table will only start editing if you press F2. If you wish to change that just do the following:


        Action action = table.getActionMap().get("startEditing");
        if (action != null)
        {
            table.getInputMap().put(KeyStroke.getKeyStroke("mykey"),
                    "startEditing");
        }

Where the hack did I get that startEditing? Look into the BasicTableUI, there all the actions for tables are dscribed. Unfortunatelly in a hidden - private manner.


First published on the old anydoby.com blog on 2009-06-04.