Skip to content

CopyAndPaste

Dirk Fauth edited this page Jan 5, 2026 · 1 revision

Copy & Paste

NatTable supports copying of selected data. To make this work of course the SelectionLayer needs to be added to the layer stack, as it is needed to determine which values should be copied.

There is no additional configuration necessary in order to enable copying. You simply need to ensure that the CopyDataCommandHandler is registered in your layer stack. If this is true, you just need to execute the according CopyDataToClipboardCommand which will copy the String representation of the selected data to the clipboard.

Commands

ILayerCommand ILayerCommandHandler Description
CopyDataToClipboardCommand CopyDataCommandHandler Executing this command will copy the String representation of the selected data to the clipboard.
CopyDataToClipboardCommand InternalCopyDataCommandHandler Executing this command will copy the String representation of the selected data to the clipboard and to the InternalCellClipboard so it can be pasted within NatTable.
CopyDataToClipboardCommand RowSpanningCopyDataCommandHandler Executing this command will copy the String representation of the selected data to the clipboard and to the InternalCellClipboard so it can be pasted within NatTable. It will treat cells with row spanning as a single cell and will not create gaps for rows with no cell to copy.
PasteDataCommand InternalPasteDataCommandHandler Executing this command will paste the copied data from the InternalCellClipboard to the NatTable.
PasteDataCommand RowSpanningPasteDataCommandHandler Executing this command will paste the copied data from the InternalCellClipboard to the NatTable. It will treat cells with row spanning as a single cell.

The CopyDataCommandHandler is registered by default to the SelectionLayer. The DefaultSelectionConfiguration also aggregates the DefaultSelectionBindings which adds the CTRL + C key binding to the CopyDataAction.

The created instance is configured to only copy the selected cells. In case of a grid layer you might also want to copy the values of the column and row header, so on dropping the copied data, the context is available. You can do this easily by creating and registering a different configured instance of CopyDataCommandHandler. For this use the constructor that takes the column header DataLayer and the row header DataLayer as additional parameter. This way on copying the corresponding header values will be copied also.

If you register this CopyDataCommandHandler instance to the GridLayer for example, it will handle the CopyDataToClipboardCommand so the CopyDataCommandHandler registered to the SelectionLayer will never get informed about the command.

// create a CopyDataCommandHandler that also copies the headers of the selected cells
CopyDataCommandHandler copyHandler = 
    new CopyDataCommandHandler(
        selectionLayer, 
        columnHeaderDataLayer, 
        rowHeaderDataLayer);
// configure to use IDisplayConverters for copy
copyHandler.setCopyFormattedText(true);
// register the handler to the GridLayer to avoid handling of the command in the SelectionLayer
gridLayer.registerCommandHandler(copyHandler);

You are also able to configure the CopyDataCommandHandler for how to determine the String representation of the data value to copy. By default it will simply call toString() on the object. But a more convenient way is to use the configured IDisplayConverter in the ConfigRegistry, so the copied data really looks like the data in the application. You can configure this by calling setCopyFormattedText(true) on the CopyDataCommandHandler instance.

Fill drag handle

Another way to copy data in a NatTable is to configure a fill drag handle similar to well known spreadsheet applications. This can be enabled by simply adding the FillHandleConfiguration to a NatTable instance.

natTable.addConfiguration( 
    new FillHandleConfiguration(selectionLayer));

Note:
Ensure that the NatTable instance is editable in order to make the fill drag handle work. It is not intended to update cells that are not editable.

The FillHandleConfiguration basically adds the following new elements:

  • FillHandleLayerPainter
    Specialized SelectionLayerPainter that renders a selection handle on the bottom right of a consecutive selection.
  • FillHandleMarkupListener
    Listener that will trigger a markup of the cell to which the fill drag handle should be bound.
  • FillHandleEventMatcher
    MouseEventMatcher that returns true in case the mouse moves over the fill drag handle rendered by the FillHandleOverlayPainter.
  • FillHandleCursorAction
    Action that will change the cursor to a small cross to indicate fill drag behavior can be used.
  • FillHandleDragMode
    IDragMode that gets activated once the fill drag handle is dragged.
  • FillHandlePasteCommand & FillHandlePasteCommandHandler
    ILayerCommandHandler that handles the ILayerCommand which is triggered on releasing the fill drag handle. For formula support the FormulaFillHandlePasteCommandHandler sub-class is provided.

Commands

ILayerCommand ILayerCommandHandler Description
FillHandlePasteCommand FillHandlePasteCommandHandler Command that is triggered on releasing the fill drag handle. The FillHandleDragMode first collects and prepares the data and puts it into the InternalCellClipboard so it can be pasted afterwards.
FillHandlePasteCommand FormulaFillHandlePasteCommandHandler Command that is triggered on releasing the fill drag handle. The FillHandleDragMode first collects and prepares the data and puts it into the InternalCellClipboard so it can be pasted afterwards. The specialized sub-class adds formula support.

Configuration Attributes

The fill drag handle can be configured via ConfigRegistry. The corresponding configuration attributes are defined in the class FillHandleConfigAttributes.

ConfigAttribute Description
FillHandleConfigAttributes#FILL_HANDLE_REGION_BORDER_STYLE The line style that should be used to render the border around cells that are contained in the fill area.
FillHandleConfigAttributes#FILL_HANDLE_BORDER_STYLE The border style of the fill drag handle itself.
FillHandleConfigAttributes#FILL_HANDLE_COLOR The color of the fill drag handle.
FillHandleConfigAttributes#INCREMENT_DATE_FIELD The date field that should be incremented when inserting a series of date values via fill drag handle.
FillHandleConfigAttributes#ALLOWED_FILL_DIRECTION The direction(s) that are allowed for the fill drag handle.

Related Examples

  • Tutorial Examples - Additional Functions - CopyExample
    This example shows how to register a different copy handling for a NatTable grid that adds the header information to the copied body values and uses the configured IDisplayConverter to create the String representation of the copied values.
  • Tutorial Examples - Data - FormulaDataExample
    This example is configured to support Copy & Paste of cells inside the NatTable. It also supports the fill drag handle.

Clone this wiki locally