不太想用键盘事件,不知道有没有别的方法可以实现!键盘的话可以用keyPress来监听,但是总觉得别扭!

解决方案 »

  1.   

    试试,应该好用。
    public class Test {

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(400,300);
    frame.setLayout(new BorderLayout());
    frame.add(new MyTable(5,4));
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class MyTable extends JTable{

    private static final long serialVersionUID = -2880222031613028633L; public MyTable(int i, int j) {
    super(5,4);
    SelectionListener listener  = new SelectionListener(this);
    this.getColumnModel().getSelectionModel().addListSelectionListener(listener);
    this.getSelectionModel().addListSelectionListener(listener);
    }
    }class SelectionListener implements ListSelectionListener{

    private JTable table;

    public SelectionListener(JTable table){
    this.table = table;
    } @Override
    public void valueChanged(ListSelectionEvent e) {
    if(!e.getValueIsAdjusting()){
    System.out.println(table.getSelectedRow()+","+table.getSelectedColumn());
    }
    }
    }