views:

35

answers:

1

Hello everyone!

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.

A: 

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.

camickr
Ok ok, first of all the action is supposed to do something totally irrelevant to the jList. It's supposed to give a yFiles EdgeRealiser a color. The thing is you won't be able to compile and run a SSCCE if you don't have yFiles and I do believe it will not really help. Question is, is there any good reason why this action as it is implemented there, is not working?
devilwontcry
If you add a System.out.println to the method and you see the display then the code WILL be exeucted. Its just a matter of if you implemented the code correctly. You haven't posted the implementation of the Action so we can't tell if it should work or not. You haven't posted the code where you add the listener to the component, so again we don't know if you are doing that correctly. As I told you in your last posting we don't have time to play 20 questions guessing what you might have forgotten to code.
camickr
As I answered above, I have made all the necessary "s.out" tests and the Listener is responding. I cannot post a SSCCE here because my program needs a package yFiles that is not free, so most of the people here won't be able to compile still but I will edit my post and add some more snippets like the action and the jList initialization.
devilwontcry
You still don't understand the concept of a SSCCE. I don't care about your yFiles class. The idea is to create a short, simple program that demonstrates the problem you are having. Therefore all you need to do is create a simple Action that displays some text and you will prove that you implemented the method properly and added all the listeners properly. Then its up to you to figure out why your real Action doesn't work. Based on the updated code you posted its possible you have the variable "jList" defined twice, once as a class variable and once as a local variable.
camickr
I have added a SSCCE. (Still trying to figure out what another person would need in order to help me with my solution. I am not slacking or anything camickr)
devilwontcry
You are right! That works! Though I admit I have not yet understood why that works instead of the code I used. Can you please explain it to me for future usages?
devilwontcry
I gave you the reason. Creating an Object does not invoke any methods contained in the Object. Unless you manually invoke the actionPerformed() method, nothing will happen. Now when you add an Action to a menuItem or a button, then the ActionListener added to the component will invoke the actionPerformed() method for you when you click on the component.
camickr
Hmm I hadn't noticed that ever but it makes perfect sense when I think about it. I guess it's because I mostly used actions on buttons etc. Thank you very much for the help once more mate!
devilwontcry