1

I have a fairly vanilla JTable, (the main change is I have a row header column) most cells just hold a String. Using Ctrl+C on a table cell makes it available to the clipboard, the problem is that I cannot use Ctrl+V to paste the value into another cell or multiple cells.

I have to click on the cell to go into an edit mode, only then I can paste the value, but this is cumbersome I just want to be able to select a field (or multiple fields) and then paste with Ctrl+V, how do I do this?

Table

public class EditSongsTable extends JTable
{
    /**
     * Overridden to improve performance because always use the same renderer
     *
     * @param row
     * @param column
     * @return the row label renderer
     */
    @Override
    public TableCellRenderer getCellRenderer(int row, int column)
    {
        if(column==0)
        {
            return  TableRowLabelRenderer.getInstanceOf();
        }
        else
        {
            return super.getCellRenderer(row, column);
        }
    }
}

Table Model

public class EditSongsTableModel extends DefaultTableModel
{
    private List<SongFieldName> fields;
    private List<Song>         songs;

    public EditSongsTableModel(Set<SongFieldName> fields, List<Song> songs)
    {
        super(songs.size(), fields.size());
        this.fields= new ArrayList<>(fields);
        this.songs=songs;

        Vector<String> columnNames = new Vector<>();
        columnNames.add("#");
        for (SongFieldName next : fields)
        {
            columnNames.add(next.getName());
        }
        this.setColumnIdentifiers(columnNames);
    }

    public Object getValueAt(int row, int column)
    {


        if(column==0)
        {
            return String.valueOf(row + 1);
        }
        else
        {
            SongFieldName sfn = fields.get(column -1);
            Song song = songs.get(row);
            if (sfn.getDataType() == SongFieldDataType.BOOLEAN)
            {
                return Boolean.valueOf(song.getFieldValueOrEmptyString(fields.get(column - 1).getSongFieldKey()));
            }
            else
            {
                return song.getFieldValueOrEmptyString(fields.get(column - 1).getSongFieldKey());
            }
        }
    }

    @Override
    public void setValueAt(Object aValue, int row, int column)
    {
        SongFieldName sfn = fields.get(column - 1);
        Song song = songs.get(row);
        if(sfn.getDataType()== SongFieldDataType.BOOLEAN)
        {
            song.setField(fields.get(column - 1).getSongFieldKey(), ((Boolean)aValue).toString());
        }
        else
        {
            song.setField(fields.get(column - 1).getSongFieldKey(), (String) aValue);
        }
        fireTableCellUpdated(row, column);
    }


    public Class getColumnClass(int columnIndex)
    {
        if(columnIndex == 0)
        {
            return String.class;
        }
        else
        {
            SongFieldName sfn = fields.get(columnIndex - 1);
            if (sfn.getDataType() == SongFieldDataType.BOOLEAN)
            {
                return Boolean.class;
            }
            else
            {
                return super.getColumnClass((columnIndex));
            }
        }
    }

    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        return columnIndex > 0;
    }

}
Developer Guy
  • 2,318
  • 6
  • 19
  • 37
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

0 Answers0