-
Enhancement
-
Resolution: Won't Fix
-
P5
-
5.0
-
x86
-
windows_xp
A DESCRIPTION OF THE REQUEST :
It should be possible to perform any of the following operations on blocks of cells within a JTable:
- ctrl-c to copy (this is the only one of these that is currently supported)
- ctrl-x to cut
- ctrl-v to paste
- delete or backspace to clear all selected cells (currently it clears only one cell)
JUSTIFICATION :
These are basic editing operations which users expect any table to support. JTable should provide them automatically, instead of requiring every programmer to reimplement them.
A justification I have sometimes heard for why JTable does not support pasting is that this cannot be implemented in a way that works for every table. That is equally true of copying, but this does not prevent BasicTableUI from providing an implementation that works for the majority of tables.
Here is an implementation of pasting I have used in the past which seems to work well:
- Get the clipboard contents as a String (DataFlavor.stringFlavor).
- Split it on tabs and line breaks to get a matrix of individual cell values. (This is consistent with the format used for copying, so you can copy and paste blocks of cells within a table.)
- For each cell, check the value returned by getColumnClass(). If a String is allowed in that column, set that as the cell value.
- If not, see whether the column class defines a static valueOf(String) method. If so, invoke it and set the return value as the cell value.
This covers all the standard "primitive" classes (String, Integer, Boolean, etc.), which is enough for about 90% of all tables.
It should be possible to perform any of the following operations on blocks of cells within a JTable:
- ctrl-c to copy (this is the only one of these that is currently supported)
- ctrl-x to cut
- ctrl-v to paste
- delete or backspace to clear all selected cells (currently it clears only one cell)
JUSTIFICATION :
These are basic editing operations which users expect any table to support. JTable should provide them automatically, instead of requiring every programmer to reimplement them.
A justification I have sometimes heard for why JTable does not support pasting is that this cannot be implemented in a way that works for every table. That is equally true of copying, but this does not prevent BasicTableUI from providing an implementation that works for the majority of tables.
Here is an implementation of pasting I have used in the past which seems to work well:
- Get the clipboard contents as a String (DataFlavor.stringFlavor).
- Split it on tabs and line breaks to get a matrix of individual cell values. (This is consistent with the format used for copying, so you can copy and paste blocks of cells within a table.)
- For each cell, check the value returned by getColumnClass(). If a String is allowed in that column, set that as the cell value.
- If not, see whether the column class defines a static valueOf(String) method. If so, invoke it and set the return value as the cell value.
This covers all the standard "primitive" classes (String, Integer, Boolean, etc.), which is enough for about 90% of all tables.