Java - jList:自定义 ListSelectionListener 问题(包含 SSCCE)

发布于 2024-09-07 01:22:08 字数 3290 浏览 7 评论 0原文

我试图在自定义 ListSelectionListener 上设置一些操作,尽管当我实际选择 jList 的组件时一切都编译得很好,但它不起作用。

这是一个代码片段:

public class ListSelectionHandler implements ListSelectionListener
{
    ListCustomObject o;

    @Override
    public void valueChanged(ListSelectionEvent e)
    {
            o = (ListCustomObject) app.MainWindow.jList1.getModel()
                    .getElementAt(e.getFirstIndex());

            new app.actions.Actions().createSetEdgeColorTo(o.getColor());
    }

}

我正在调用的操作正在运行,并且编译时没有错误。但实际上什么也没发生。

我知道我没有在这段代码中包含太多细节,我只是想问我在这个事件中是否犯了逻辑错误。

提前致谢!

编辑:添加了操作和 JList 初始化:

    public Action createSetEdgeColorTo(Color color)
    {
        return new SetEdgeColorTo(color);
    }

    class SetEdgeColorTo extends AbstractAction
    {

        Color color;

        SetEdgeColorTo(Color color)
        {

            super("Set new Edge Color");
            this.color = color;
        }

        @Override
        public void actionPerformed(ActionEvent evt)
        {
            app.graph.GraphEdit.view.getGraph2D().getDefaultEdgeRealizer()
                                                 .setLineColor(color);
            app.graph.GraphEdit.view.getGraph2D().updateViews();
        }
    }

以及

JList jList1 = new javax.swing.JList();
ListSelectionModel listSelectionModel = jList1.getSelectionModel();
listSelectionModel.addListSelectionListener(new app.jlist
                                             .ListSelectionHandler());

编辑 3:重新设计了 SSCCE

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.Action;
import javax.swing.JFrame;

public class SSCCE
{

    static JList jList1;

    public static void main(String[] args)
    {

        JFrame frame = new JFrame();
        jList1 = new JList();
        ListSelectionModel listSelectionModel = jList1.getSelectionModel();
        listSelectionModel.addListSelectionListener(
                new ListSelectionHandler());
        DefaultListModel listModel = new DefaultListModel();
        jList1.setModel(listModel);
        listModel.addElement("String");
        listModel.addElement("String two");

        frame = new JFrame();
        frame.setDefaultCloseOperation(1);
        frame.add(jList1);
        frame.pack();
        frame.setVisible(true);

    }
}

class ListSelectionHandler implements ListSelectionListener
{
    @Override
    public void valueChanged(ListSelectionEvent e)
    {

        System.out.println("" + e.getFirstIndex());
        new Actions().createTestAction();
    }
}

class Actions
{

    public Action createTestAction()
    {
        return new TestAction();
    }

    class TestAction extends AbstractAction
    {

        TestAction()
        {
            super("Test Action");
        }

        @Override
        public void actionPerformed(ActionEvent evt)
        {
            System.out.println("Test Action Fired!");
        }
    }
}

此 SSCCE 描述了再次未触发的示例 TestAction 的确切问题。

I am trying to set some actions on a custom ListSelectionListener and although everything compiles out fine when I actually select a component of the jList it's not working.

Here's a code snippet:

public class ListSelectionHandler implements ListSelectionListener
{
    ListCustomObject o;

    @Override
    public void valueChanged(ListSelectionEvent e)
    {
            o = (ListCustomObject) app.MainWindow.jList1.getModel()
                    .getElementAt(e.getFirstIndex());

            new app.actions.Actions().createSetEdgeColorTo(o.getColor());
    }

}

The action I am calling, is working and there's no error when compiling. But nothing actually happens.

I know I am not including much detail in this code, I just wanna ask if I am making a logical mistake in this event.

Thanks in advance!

EDIT: Added the Action and the JList initialization:

    public Action createSetEdgeColorTo(Color color)
    {
        return new SetEdgeColorTo(color);
    }

    class SetEdgeColorTo extends AbstractAction
    {

        Color color;

        SetEdgeColorTo(Color color)
        {

            super("Set new Edge Color");
            this.color = color;
        }

        @Override
        public void actionPerformed(ActionEvent evt)
        {
            app.graph.GraphEdit.view.getGraph2D().getDefaultEdgeRealizer()
                                                 .setLineColor(color);
            app.graph.GraphEdit.view.getGraph2D().updateViews();
        }
    }

and

JList jList1 = new javax.swing.JList();
ListSelectionModel listSelectionModel = jList1.getSelectionModel();
listSelectionModel.addListSelectionListener(new app.jlist
                                             .ListSelectionHandler());

EDIT 3: Reworked SSCCE:

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.Action;
import javax.swing.JFrame;

public class SSCCE
{

    static JList jList1;

    public static void main(String[] args)
    {

        JFrame frame = new JFrame();
        jList1 = new JList();
        ListSelectionModel listSelectionModel = jList1.getSelectionModel();
        listSelectionModel.addListSelectionListener(
                new ListSelectionHandler());
        DefaultListModel listModel = new DefaultListModel();
        jList1.setModel(listModel);
        listModel.addElement("String");
        listModel.addElement("String two");

        frame = new JFrame();
        frame.setDefaultCloseOperation(1);
        frame.add(jList1);
        frame.pack();
        frame.setVisible(true);

    }
}

class ListSelectionHandler implements ListSelectionListener
{
    @Override
    public void valueChanged(ListSelectionEvent e)
    {

        System.out.println("" + e.getFirstIndex());
        new Actions().createTestAction();
    }
}

class Actions
{

    public Action createTestAction()
    {
        return new TestAction();
    }

    class TestAction extends AbstractAction
    {

        TestAction()
        {
            super("Test Action");
        }

        @Override
        public void actionPerformed(ActionEvent evt)
        {
            System.out.println("Test Action Fired!");
        }
    }
}

This SSCCE describes the exact problem with a sample TestAction that again is not firing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-09-14 01:22:08

那么你究竟想做什么?

如果您尝试设置已选择的行的颜色,那么该代码应该在渲染器中完成。一般来说,每当您在渲染器中使用颜色时,您都需要进行检查:

if (! isSelected)
    //  do you custom rendering

这样,当您选择不同的行时,该行仍将显示默认突出显示。

当然,仍然应该发布 SSCCE,因为我们不知道您的自定义操作正在尝试执行什么操作,因此无法提出任何真正的建议。

编辑:

它不执行任何操作,因为您的所有代码所做的就是创建操作。如果您想调用该操作,那么代码应该类似于:

new Actions().createTestAction().actionPerformed(null);

实际上,无需创建实际的操作,您只需直接调用一些执行您想要的操作的方法即可。创建 Action 的原因是如果您想要将 Actdion 添加到 JButton 或 JMenuItem,以便用户可以单击该组件来调用 Action。

So what exactly are you trying to do?

If you are trying to set the color of a row that has been selected, then that code should be done in the renderer. In general whenever you play with colors in a renderer you need the check:

if (! isSelected)
    //  do you custom rendering

This way the row will still show the default highlighting as you select different rows.

And of course a SSCCE should still be posted because we have no idea what your custom Action is attempting to do and therefore can't make any real suggestions.

Edited:

It doesn't do anything because all your code is doing is creating the Action. If you want to invoke the Action then the code should be something like:

new Actions().createTestAction().actionPerformed(null);

In reality there is no need to create an actual Action, you just need to invoke some method directly that does what you want. The reason you create an Action would be if you wanted to add the Actdion to a JButton or JMenuItem so the user could click on the component to invoke the Action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文