有人可以告诉我为什么这不起作用吗?System.out.println(table.getSelectedRow());
由于某种原因,这条线是红色的!我希望每次单击一行时都显示选择了哪一行。我在这里错过了什么吗?
table = new JTable(data, columnNames) {
public Class getColumnClass(int column) {
for (int row = 0; row < getRowCount(); row++) {
Object obj = getValueAt(row, column);
if (obj != null) {
return obj.getClass();
}
}
return Object.class;
}
};
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.SOUTH);
table.addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e) {
System.out.println(table.getSelectedRow());
}
});