读书人

JTable表格事件求解,该如何处理

发布时间: 2012-09-16 17:33:16 作者: rapoo

JTable表格事件求解
表格中有10行记录
我想现在向点击表格中的某行记录 然后触发相应事件
请教下应该触发哪个监听事件?
说明: 是点击JTable中的一行记录触发事件 求解!

[解决办法]
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {

}
});
[解决办法]
其实Google下就大把了:

Java code
SelectionListener listener = new SelectionListener(jtable);jtable.getSelectionModel().addListSelectionListener(listener);public class SelectionListener implements ListSelectionListener {        JTable table;        SelectionListener(JTable table) {            this.table = table;        }        public void valueChanged(ListSelectionEvent e) {            if (e.getSource() == table.getColumnModel().getSelectionModel()                   && table.getColumnSelectionAllowed() ){                int firstRow = e.getFirstIndex();                int lastRow = e.getLastIndex();                // 事件处理...            }        }    } 

读书人网 >J2SE开发

热点推荐