我在Mac上遇到DnD和JTable问题。 如果您启动以下程序并在表格中单击(快速),有时选择某些内容,有时会在应用程序崩溃一段时间后执行DnD,或者至少DnD不会 有可能了。我在2台Mac上测试过它。
Java版本:1.6.0_35 Mac OS X:10.6.8
有人能证实吗? 任何解决方法?
package tablednd;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class TableDnD {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Object[][] data = {
{"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews", "Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)}
};
String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
final JTable table = new JTable(data, columnNames);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.setDragEnabled(true);
frame.add(table);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
答案 0 :(得分:3)
当将拖动的行放在桌子上的任何其他位置时,我(有时)会将下面显示的错误返回到Mac OS X 10.5.8。目标选择矩形保留在屏幕上,不再进行拖动操作。我不确定为什么,但我认为单元格不被认为是一行的合适目的地。
2012-10-14 14:14:23.912 java[44061:10b] *** -[NSWindowViewAWT draggingEnded:]: unrecognized selector sent to instance 0x1001e7140 2012-10-14 14:14:23.913 java[44061:10b] *** -[NSWindowViewAWT draggingEnded:]: unrecognized selector sent to instance 0x1001e7140
删除另一个应用程序上拖动的行按预期工作。
另外,auto-boxing可以简化初始化代码:
Object[][] data = {
{"Mary", "Campione", "Snowboarding", 5, false},
{"Alison", "Huml", "Rowing", 3, true},
{"Kathy", "Walrath", "Chasing toddlers", 2, false},
{"Mark", "Andrews", "Speed reading", 20, true},
{"Angela", "Lih", "Teaching high school", 4, false}
};
附录:此图像显示正在进行的拖动;触发异常后,拖动框架时灰色矩形保持不动。
作为一种解决方法,有disable the grey rectangle altogether的解决方案。