我正在编写一个Java Swing应用程序,其中包含一个包含多个列和行的JTable。我希望能够在一个列中选择几个单元格,复制它们并将它们粘贴到另一个选定的单元格组中。
我在网上找到了使用ExcelAdapter类(here)的提示。我根据这个例子创建了自己的类,我觉得有点简单,因为我不需要检查是否只选择了相邻的单元格,或者所选单元格是否在多个列中(这在我的应用程序中已禁用)。 / p>
它确实在某种程度上起作用,但并不完全符合我的意愿。我可以选择单元格并将它们复制到剪贴板。我也可以粘贴它们,但由于某种原因,选择粘贴区域中的最后一个单元格被跳过(即不粘贴)。如果所选复制的单元格的数量与所选的粘贴区域单元格的数量相同,则粘贴的单元格的数量被选择为粘贴区域单元格 - 1.如果复制了更多的单元格(例如,4个单元格),则选择要粘贴的单元格更少(例如2),跳过选择用于粘贴的最后一个单元格,并从下面的一个单元格继续粘贴。
以下是我的ExcelAdapter类版本的代码:
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.AbstractAction;
import javax.swing.JTable;
import javax.swing.KeyStroke;
public class CopyPasteMachine {
private JTable table;
private Clipboard clipboard;
private StringSelection transferableString;
private String stringValue;
KeyStroke copy;
KeyStroke paste;
public CopyPasteMachine (JTable t) {
table = t;
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if(System.getProperty("os.name").toLowerCase().startsWith("mac")) {
copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false);
paste = KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false);
} else {
copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK, false);
paste = KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK, false);
}
table.getInputMap().put(copy, "copy");
table.getActionMap().put("copy", new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 7400399952613035707L;
@Override
public void actionPerformed(ActionEvent e) {
StringBuffer stringBuffer = new StringBuffer();
int rowCount = table.getSelectedRowCount();
int [] rows = table.getSelectedRows();
int columnNo = table.getSelectedColumn();
for (int i = 0; i < rowCount; i++) {
stringBuffer.append(table.getValueAt(rows[i], columnNo));
stringBuffer.append("\n");
}
transferableString = new StringSelection(stringBuffer.toString());
clipboard.setContents(transferableString, transferableString);
}
});
table.getInputMap().put(paste, "paste");
table.getActionMap().put("paste", new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 3678654129316517021L;
@Override
public void actionPerformed(ActionEvent e) {
int startRow = (table.getSelectedRows())[0];
int columnNo = table.getSelectedColumn();
try {
String tempStringValue = (String)(clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor));
StringTokenizer tokenizer = new StringTokenizer(tempStringValue,"\n");
for (int i = 0; tokenizer.hasMoreTokens(); i++) {
stringValue = tokenizer.nextToken();
if (startRow + i < table.getRowCount()) {
table.setValueAt(stringValue, startRow + i, columnNo);
}
}
} catch (UnsupportedFlavorException e1) {
System.out.println("Something went wrong");
System.err.println(e1.getMessage());
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("Something went wrong");
System.err.println(e1.getMessage());
e1.printStackTrace();
}
}
});
}
public JTable getTable() {
return table;
}
public void setTable(JTable table) {
this.table = table;
}
}