当我尝试使用table.setValueAt()将内容粘贴到JTable单元格中时,我发现我要粘贴的单元格没有内容,但setValueAt()似乎起作用了。此外,当我尝试从一个单元格中剪切或复制内容时,在我想要粘贴到另一个单元格时,我的JPopupMenu上的Paste选项保持禁用状态。我不确定原因。以下是我的代码。
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;
}
}
以下是我的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
在哪里?模型的setValueAt
方法是这个问题中的关键要素。您应该考虑创建一个演示您问题的可运行示例。 - MadProgrammerGridModel
的代码,以便我们可以看到它在做什么以及setValueAt
如何工作... - MadProgrammer