JTable copiar e colar usando a área de transferência e AbstractAction
Quando tento colar em umJTable
célula usandotable.setValueAt()
, a célula em que estou colando permanece em branco, massetValueAt()
parece estar funcionando. Além disso, quando eu tentocortar oucópia de de uma célula, a opção Colar no meuJPopupMenu
permanece desativado quando quero colar em outra célula. Não sei por que. Meu código está abaixo.
class CopyAction extends AbstractAction {
private JTable table;
public CopyAction(JTable table)
{
this.table = table;
}
@Override
public void actionPerformed(ActionEvent e)
{
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(new CellTransferable(table.getValueAt(row, col)), null);
}
}
class CutAction extends AbstractAction {
private JTable table;
public CutAction(JTable table)
{
this.table = table;
}
@Override
public void actionPerformed(ActionEvent e)
{
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(new CellTransferable(table.getValueAt(row, col)), null);
}
}
class PasteAction extends AbstractAction {
private JTable table;
private GridModel gridModel;
public PasteAction(JTable tbl, GridModel gm)
{
gridModel = gm;
table = tbl;
final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.addFlavorListener(new FlavorListener() {
@Override
public void flavorsChanged(FlavorEvent e)
{
setEnabled(cb.isDataFlavorAvailable(CellTransferable.CELL_DATA_FLAVOR));
}
});
setEnabled(cb.isDataFlavorAvailable(CellTransferable.CELL_DATA_FLAVOR));
}
@Override
public void actionPerformed(ActionEvent e)
{
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
if (cb.isDataFlavorAvailable(CellTransferable.CELL_DATA_FLAVOR))
{
try
{
Object value = cb.getData(CellTransferable.CELL_DATA_FLAVOR);
System.out.println(value);
table.setValueAt(value, row, col);
}
catch (UnsupportedFlavorException | IOException ex)
{
ex.printStackTrace();
}
}
}
}
class CellTransferable implements Transferable {
public static final DataFlavor CELL_DATA_FLAVOR = new DataFlavor(Object.class, "application/x-cell-value");
private Object cellValue;
public CellTransferable(Object cellValue) {
this.cellValue = cellValue;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{CELL_DATA_FLAVOR};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return CELL_DATA_FLAVOR.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return cellValue;
}
}
Aqui está o meu código parasetValueAt()
class MyModel extends AbstractTableModel
{
private String[] names = {"1", "2", "3", "4", "5" };
private String[][] values = new String[5][5];
//...other methods
public void setValueAt(Object value, int row, int col)
{
if (value instanceof Double || value instanceof Integer)
values[row][col] = value.toString();
else
{
values[row][col] = (String) value;
}
fireTableCellUpdated(row, col);
}
}