ListSelectionListener on JList not working?

After trying to understand what is going on in your question I understood that you are failing to get selected item in the list, when the selection is changed.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm having problems with this listener, the gui in general constructs and works fine, also the jlist is there but when I select some items in the list I don't see the results and also not the printl() I wrote for test purpose, pls note this code is contained within the getJContentPane in order to add the event handler at init-time private JList myList=new JList(dlm);//a defaultlistmodel myList. AddListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { if (evt. GetValueIsAdjusting()){ System.out.

Println("Eventhandler called"); return; } System.out. Println("Eventhandler called"); doMyMethod(evt); } }); The doMyMethod(): private void doMyMethod(ListSelectionEvent e){ if(e. GetValueIsAdjusting()){ return; }else{ String item=(String)e.getSource(); accounter.

Add(item); } } It is a very simple method which takes as parameter an instance of ListSelectionEvent The main problem in my opinion is not the doMyMethod() which performs very basic actions but the fact that the eventHandler is not fired at all, it seems lik the gui does not "listen" to this list at all Any idea? Here the initialisation code: private JScrollPane getScrollBox() { if (scrollboxBox == null) { scrollboxBox = new JScrollPane(); scrollBox. SetBounds(new Rectangle(280, 56, 245, 204)); scrollBox.getViewport().

Add(myList,null); myList. AddListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { System.out. Println("addListSelectionListener"); if (evt.

GetValueIsAdjusting()){ System.out. Println("Eventhandler called"); return; } System.out. Println("Eventhandler called"); doMyMethod(evt); } }); } return scrollboxBox; } java swing jlist link|improve this question edited May 24 '11 at 5:19 asked May 24 '11 at 4:19JBoy17212 64% accept rate.

– Hovercraft Full Of Eels May 24 '11 at 4:22 did not notice the recursive method, changed now: synoniemenList. AddListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { if (evt. GetValueIsAdjusting()){ System.out.

Println("Eventhandler called"); return; } System.out. Println("Eventhandler called"); do(evt); } }); – JBoy May 24 '11 at 4:26 1 @JBoy, please edit your question with the new code. – jjnguy?

May 24 '11 at 4:27 2 Not sure I understand the logic of your initialization code. If the content pane is null you add the listener to the list otherwise your don't add it. A proper SSCCE (sscce.org) would be more helpful.

– camickr May 24 '11 at 4:48 2 @JBoy now it even won't compile. You once used scrollboxBox and one line later scrollBox (unless you have defined both fields but then the code does not make sense). I once again suggest that you provide a SSCCE.

– Howard May 24 '11 at 5:37.

After trying to understand what is going on in your question I understood that you are failing to get selected item in the list, when the selection is changed. To present you how this could be achieved I have build the below example. You can observe there when each part of the if else is called.

The if(e. GetValueIsAdjusting()) is execute always when you drag the moue over items. The else part is called when you release the mouse while previously clicked on an item.

There are some serious issues in the code that you have provided therefore I tried to remove all that is unnecessary, e.g. 1. The doMyMethod() method the if(e. GetValueIsAdjusting()) piece of code that is in it would never be called since you already are checking that condition before it is called.

Plus there is no need to code a method working on listener's events, this code should stay inside the listener which is private. Then if you want to call programmatically its code you would call the listener. ValueChanged(evt).

2. The evt.getSource() returns the object which is author of the event in this case the myList not the selected item which I think you were after, for this use the getSelectedValue() on the source or myList directly since this listener serves only this list. Import javax.swing.

JFrame; import javax.swing. JList; import javax.swing. JPanel; import javax.swing.

JScrollPane; import javax.swing. SwingUtilities; import javax.swing.event. ListSelectionEvent; import javax.swing.event.

ListSelectionListener; public class ListTest extends JPanel { private JScrollPane myScrollPane; private JList myList; private ListSelectionListener lSL = new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent evt) { if(evt. GetValueIsAdjusting()) { System.out. Println("Eventhandler called evt.

GetValueIsAdjusting() true"); return; } else { // String item = (String) evt.getSource();//! Exception casting JList to String //maybe what you need is getSelectedValue() System.out. Println("else called myList.

GetSelectedValue() ="+myList. GetSelectedValue()); } } }; private ListTest() { int noRows = 25; Object listData = new StringnoRows; for(int I =0; I MyList = new JList(listData); this. MyScrollPane = new JScrollPane(myList); myList.

AddListSelectionListener(lSL); add(myScrollPane); } public static void main(String args) { SwingUtilities. InvokeLater(new Runnable() { @Override public void run() { ListTest p = new ListTest(); JPanel contentPane = new JPanel(); contentPane. Add(p); JFrame f = new JFrame(); f.

SetContentPane(contentPane); f. SetSize(800, 600); f. SetDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE); f. SetVisible(true); } }); } } Hope that even if it doesn't solve your problem it at least will be helpful in building an SSCCE for us.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions