为什么选择行改变之后这个时间触发了两次呢?有没有什么方法只触发一次?AthleteList.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
StudentIDField.setText(AthleteList.getValueAt(AthleteList.getSelectedRow(), 0).toString());
AthleteNameField.setText(AthleteList.getValueAt(AthleteList.getSelectedRow(), 1).toString());
}
});
为什么println语句会打印两次呢?

解决方案 »

  1.   

    AthleteList.getSelectionModel().addListClickedListener{}
    试试用这个事件!
      

  2.   

    之所以会被触发两次,是因为选择事件包括两种情况,一种是取消选择,一种是选择,楼主点击一项的时候,一般是取消了一项的选择,此时会触发一次,然后选择了一项,此时又触发一次,好了,知道原理就知道怎么改了。import javax.swing.*;
    import javax.swing.event.*;public class Test {
    private static int index;

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(400, 300);
    final JList AthleteList = new JList(new Object[] { "选项1", "选项2" });
    index = AthleteList.getSelectedIndex();
    AthleteList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
    if (index == AthleteList.getSelectedIndex()) {
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }
    index = AthleteList.getSelectedIndex();
    }
    });
    frame.add(AthleteList);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }如果楼主是设置的可以选择多行的模式,情况就更复杂了,但思路还是一样。