Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4177723

ListSelectionEvents not fired on model changes affecting JList selection

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.3.1_03
    • 1.1.7, 1.1.8, 1.2.0, 1.2.1_004, 1.2.2, 1.3.0, 1.3.1_01
    • client-libs
    • 03
    • generic, x86, sparc
    • generic, solaris_2.6, solaris_8, windows_95, windows_nt



        Name: el35337 Date: 09/30/98


        (Swing 1.1b2, JDK 1.1.5, 1.1.6, 1.1.7)
        Although removing an element from a JList's underlying
        ListModel causes that element's representation to
        disappear from the view, ListSelectionEvent listeners
        are not informed despite the fact that the selection
        state of the view has changed.

        The following code displays a frame containing a
        JList view on a small model; a button; and a text area.
        The text area is fed with string equivalents of
        ListSelectionEvents heard from the JList, and the button
        removes the item currently selected in the list, by manipulating
        the model directly. No ListSelectionEvent is
        detected when the button is used - I believe this to
        be a bug.

        import com.sun.java.swing.*;
        import com.sun.java.swing.border.*;
        import com.sun.java.swing.event.*;
        import java.awt.*;
        import java.awt.event.*;


        public class ListDeselectionDemonstrator {

        static final JTextArea eventDisplay = new JTextArea(10, 40);
        static final String LINE_SEPARATOR = System.getProperty("line.separator");

        public static void main(String[] args) {

        JFrame frame = new JFrame("List deselection by model change: event-firing bug");
        Container cont = frame.getContentPane();
        cont.setLayout(new BorderLayout());

        final DefaultListModel model = new DefaultListModel();
        model.addElement("Item zero"); // let default renderer do its job
        model.addElement("Item one");
        model.addElement("Item two");

        final JList list = new JList(model);
        list.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

        list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent lse) {
        echoText(lse.toString());
        }
        });

        eventDisplay.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

        JButton removeButton = new JButton("Remove element");
        removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        int selectedIndex = list.getSelectedIndex();
        if (selectedIndex == - 1) {
        echoText("Nothing selected.");
        return;
        }

        // For this example, we're validly assuming a model/view index match.
        model.removeElementAt(selectedIndex); // should fire event
        }
        });

        cont.add(list, BorderLayout.WEST);
        cont.add(new JScrollPane(eventDisplay), BorderLayout.CENTER);
        cont.add(removeButton, BorderLayout.SOUTH);

        frame.setSize(500,500);
        frame.setVisible(true);
        }

        static void echoText(String message) {
        eventDisplay.append(message + LINE_SEPARATOR);

        // Scroll to show appended text (thanks to Eric Tavares):
        try { eventDisplay.setCaretPosition(eventDisplay.getDocument().getLength()); }
        catch (IllegalArgumentException e) {}
        }
        }
        (Review ID: 39713)
        ======================================================================

              sswingtrsunw Swingtraq Swingtraq (Inactive)
              elarsen Erik Larsen (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: