作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我右键单击了 JTable,并以一种糟糕的方式将一些代码插入到“post listeners code”中。
我没有看到要添加的选项
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
到 JTable 的“设计” View 中的“事件”。我确信有一种方法可以从设计 View 中添加 valueChanged(ListSelectionEvent evt),但如何添加?
也许是 bug ?
Row selection change events are produced by ListSelectionModel of JTable, not by JTable itself - so the event cannot be presented in Component Inspector (as event of JTable). Handling this event must be done manually, e.g. like:
jTable1.getSelectionModel().addListSelectionListener(
new javax.swing.event.ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
customRowSelectionEventHandler(evt);
}
}
);
虽然也许有一种方法可以在“蓝色”、“托管”代码之外获取 JTable 的 ListSelectionModel?
最佳答案
您可以在源代码的可编辑部分创建您自己的ListSelectionListener
。您可以将监听器的实例添加到表的 Post-init Code
属性中的类变量 jTable1
的选择模型:
jTable1.getSelectionModel().addListSelectionListener(new MyListener());
监听器本身可能如下所示:
private static class MyListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
System.out.println(e.getFirstIndex());
}
}
关于java - Netbeans:如何从 "design"GUI 生成器向 JTable 添加 valueChanged 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467258/
我是一名优秀的程序员,十分优秀!