如何调整Java JTable以便将粘贴复制到多个单元格

时间:2018-04-06 14:33:12

标签: java swing jtable

我有一个相当普通的JTable,(主要的变化是我有一个行标题列)大多数单元格只持有一个字符串。在表格单元格上使用 Ctrl + C 使其可用于剪贴板,问题是我无法使用 Ctrl + V < / kbd>将值粘贴到另一个单元格或多个单元格中。

我必须单击单元格进入编辑模式,然后我才能粘贴该值,但这很麻烦我只想选择一个字段(或多个字段)然后粘贴 Ctrl + V ,我该怎么做?

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

表格型号

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;
    }

}

0 个答案:

没有答案