When I try to paste into a JTable
cell using table.setValueAt()
, the cell I am pasting in remains blank, but setValueAt()
seems to be working. Also, when I try to cut or copy from one cell, the Paste option on my JPopupMenu
remains disabled when I want to paste into another cell. I'm not sure why. My code is below.
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;
}
}
Here's my code for setValueAt()
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);
}
}
GridModel
? The model'ssetValueAt
method is the critical element in this problem. You should consider creating a runnable example which demonstrates your problem – SaccharifyGridModel
) so we can see what it's doing and howsetValueAt
works... – Saccharify