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.