Netbeans:如何从“设计"窗口中向JTable添加valueChanged侦听器? GUI生成器? [英] Netbeans: How do I add a valueChanged listener to a JTable from the "design" GUI builder?

查看:70
本文介绍了Netbeans:如何从“设计"窗口中向JTable添加valueChanged侦听器? GUI生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我右键单击JTable,然后在糟糕的代码中将一些代码插入后侦听器代码"中.

I right clicked the JTable and inserted some code into "post listeners code" in an awful kludge.

我看不到添加选项

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
  public void valueChanged(ListSelectionEvent evt) {

JTable的设计"视图中的

到事件".我敢肯定有一种方法可以从设计视图中添加一个valueChanged(ListSelectionEvent evt),但是如何呢?

to "events" in the "design" view for the JTable. I'm sure there's a way to add a valueChanged(ListSelectionEvent evt) from design view, but how?

也许是错误?

行选择更改事件由以下项的ListSelectionModel产生 JTable,而不是JTable本身-因此无法在 组件检查器(作为JTable的事件).处理此事件必须 手动完成,例如像:

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?

Although maybe there's a way to get the ListSelectionModel for a JTable outside of the "blue", "managed," code?

推荐答案

您可以在源代码的可编辑部分中创建自己的ListSelectionListener.您可以在表的Post-init Code属性中将侦听器的实例添加到类变量jTable1的选择模型中:

You can create your own ListSelectionListener in the editable part of the source. You can add an instance of the listener to the selection model of the class variable jTable1 in your table's Post-init Code property:

jTable1.getSelectionModel().addListSelectionListener(new MyListener());

侦听器本身可能看起来像这样:

The listener itself might look like this:

private static class MyListener implements ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent e) {
        System.out.println(e.getFirstIndex());
    }
}

这篇关于Netbeans:如何从“设计"窗口中向JTable添加valueChanged侦听器? GUI生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆