gpt4 book ai didi

java - Netbeans:如何从 "design"GUI 生成器向 JTable 添加 valueChanged 监听器?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:31:27 25 4
gpt4 key购买 nike

我右键单击了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com