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

BasicListUI.ListDataHandler improperly updates list selection on insertion

XMLWordPrintable

    • beta
    • generic
    • generic



      Name: mc57594 Date: 12/16/99


      java full version "JDK-1.2.2-001"

      BasicListUI.ListDataHander improperly updates list selection on insertion

      The following code demonstrates the problem. Clicking on the "Insert" button
      will insert a new item at the top of the list. All items on the list will
      shift down, but the selection index will remain the same.

      The mistake is in the intervalAdded method of BasicListUI.ListDataHandler.
      The ListSelectionModel is updated with the code:

      sm.insertIndexInterval(minIndex, maxIndex - minIndex, true);

      The second argument of this method is supposed to be the length
      of the interval inserted. In the case when a single item is
      inserted (minIndex == maxIndex) the length should be 1, but this
      code passes a zero for the length argument.

      The corrected code is:

      sm.insertIndexInterval(minIndex, maxIndex - minIndex + 1, true);

      -----------------------------------------------------

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class ListInsertionBug extends JFrame {
          JPanel panel = new JPanel();
          JScrollPane scrollPane = new JScrollPane();
          JButton insertButton = new JButton();
          DefaultListModel model = new DefaultListModel();
          JList list = new JList(model);
          
          public ListInsertionBug() {
      model.addElement("List Item 1");
      model.addElement("List Item 2");
      model.addElement("List Item 3");
      model.addElement("List Item 4");
      list.setSelectedIndex(2);

      insertButton.setText("Insert");
      insertButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
      insertButton_actionPerformed(e);
      }
      });
      scrollPane.getViewport().add(list, null);
      this.getContentPane().add(scrollPane, BorderLayout.CENTER);
      this.getContentPane().add(insertButton, BorderLayout.SOUTH);
      this.pack();
          }
          
          void insertButton_actionPerformed(ActionEvent e) {
      model.insertElementAt("Inserted List Item", 0);
          }
          
          public static void main(String[] args) {
      ListInsertionBug frame = new ListInsertionBug();
      frame.setVisible(true);
          }
          
      }
      (Review ID: 98899)
      ======================================================================

            sswingtrsunw Swingtraq Swingtraq (Inactive)
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: