Note that there are some explanatory texts on larger screens.

plurals
  1. POClearing a JList with another List's Selection Listener does not work
    primarykey
    data
    text
    <p>I have three lists in three separate scroll panes. The code is supposed to fill the lists dynamically with vectors as I select items from the first one to the third.</p> <p>In theory, the code makes it so if I select an item from list 1, list 2 get filled. If select something in list 2, list 3 gets filled. </p> <p>The code should also make it possible to select another item from list 1, filling another set of items in list 2 and leaving list 3 blank. </p> <p>Here's the code:</p> <pre><code>list1 = new JList(vector1); list2 = new Jlist(vector2); list3 = new Jlist(vector3); s1 = new JScrollPane(list1); s2 = new JScrollPane(list2); s3 = new JScrollPane(list3); list1.addListSelectionListener(new OneListener()); list2.addListSelectionListener(new TwoListener()); //add panes to gui and start gui. Listeners are inner classes. public class OneListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { if(!e.getValueIsAdjusting()) { //getting a vector from another class called return_value vector2 = return_value; vector3.removeAllElements(); list2.setListData(vector2); list3.setListData(vector3); s2.repaint(); s3.repaint(); } } } public class TwoListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { if(!e.getValueIsAdjusting()) { //geting a vector from another class named return_value vector3 = return_value; list3.setListData(vector3); s3.repaint(); } } } </code></pre> <p>It all works fine until I try to select another item from list1, list2 and list 3 filled. </p> <p>OneListener is fired. </p> <p>List2 updates just fine but List 3 stays the same, not going blank/clearing the list. </p> <p>As soon as TwoListener is fired, list3 updates accordingly.</p> <p>Also, the console throws out the following</p> <blockquote> <p>Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1</p> </blockquote> <p>This is not a deal breaker, as the program still runs. I'd like to solve the problem so the implementation survives being extended and worked upon.</p> <p>I tried implementing <a href="http://www.coderanch.com/t/338860/GUI/java/Clearing-JList" rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/4162078/java-swing-clear-jlist-without-triggering-listener">this</a>. They haven't worked.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload