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.
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:
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:
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.