我有这段代码:
listTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
String title = listTable.getValueAt(row, column).toString();
String duration = listTable.getValueAt(row, column+1).toString();
String genre = listTable.getValueAt(row, column+2).toString();
String actor = listTable.getValueAt(row, column+3).toString();
String director = listTable.getValueAt(row, column+4).toString();
//String rentable = listTable.getValueAt(row, column+5).toString();
//String synopsis = listTable.getValueAt(row, column+6).toString();
txtTitle.setText(title);
txtDuration.setText(duration);
txtGenre.setText(genre);
}
});
这使我能够:
- 选择行时,它将列中的值传递给JTextBoxes。
但是,当我默认不单击第一列时,选择会出现乱码。比如我不先点击“Title”栏,再点击“Duration”栏,就会把duration放在txtTitle里面,其他的也混在一起了。
如何添加一段代码,即选择行时,它会默认 - 选择第一列?
这是发生的情况的屏幕截图:
谢谢,布赖恩