JDK-4803311 : Repaint problem when removing last item from a JList
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2,1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2003-01-15
  • Updated: 2003-03-12
  • Resolved: 2003-03-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Related Reports
Duplicate :  
Relates :  
Description

Name: jk109818			Date: 01/14/2003


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
If you have selected the last item in a JList (in
SINGLE_SELECTION_MODE) and then remove one of the previous
items, then when you select one of the other remaining
items in the list, the last item is not repainted. It looks
as if it is still selected even though it is not. When a
repaint of that cell is forced (through minmizing or
resizing for example) then it repaints correctly.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run sample code
2. Select last item
3. Press delete button
4. Select one of the other remaining items
5. Resize or select last item to force repaint

EXPECTED VERSUS ACTUAL BEHAVIOR :
I would expect the last item to get repainted as not being
selected at step 4. It should not have to wait for a
repaint to be forced.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ListTest2 extends JFrame {
    private JScrollPane jScrollPane1 = new JScrollPane();
    private JList jList1 = new JList();
    private JButton btnDelete = new JButton();

    public ListTest2() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel
                (UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        ListTest2 listTest2 = new ListTest2();
    }
    private void jbInit() throws Exception {
        btnDelete.setText("Delete");
        btnDelete.addActionListener(
          new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnDelete_actionPerformed(e);
            }
        });
        DefaultListModel listMod = new DefaultListModel();
        listMod.addElement("String1");
        listMod.addElement("String2");
        listMod.addElement("String3");
        listMod.addElement("String4");
        listMod.addElement("String5");
        listMod.addElement("String6");
        jList1.setModel(listMod);
        jList1.setSelectionMode(
            ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.getViewport().add(jList1, null);
        getContentPane().add(
            jScrollPane1,
            BorderLayout.CENTER);
        getContentPane().add(btnDelete, BorderLayout.NORTH);
        validate();
        setVisible(true);
    }

    void btnDelete_actionPerformed(ActionEvent e) {
        DefaultListModel listMod =
           (DefaultListModel)jList1.getModel();
        listMod.remove(0);
    }
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Create a ListSelectionListener that does a repaint in it's
valueChanged() method every time it gets a
ListSelectionEvent.
(Review ID: 180020) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b03
24-08-2004

EVALUATION Name: apR10133 Date: 02/07/2003 The problem is that DefaultListSelectionModel doesn't update the lead and anchor selection indices when it inserts/removes index interval. At the testcase from description the lead/anchor selection index becomes greater than list length after the first element is being removed and it causes undesirable effect of multiple selection. The insertIndexInterval() and removeIndexInterval() should update the lead and anchor selection indices when these indices greater than the removed/inserted indices. ###@###.### ======================================================================
24-08-2004

SUGGESTED FIX Name: apR10133 Date: 02/07/2003 ------- DefaultListSelectionModel.java ------- *** /tmp/sccs.Toayoc Fri Feb 7 17:43:41 2003 --- DefaultListSelectionModel.java Fri Feb 7 17:43:18 2003 *************** *** 521,526 **** --- 521,539 ---- for(int i = insMinIndex; i <= insMaxIndex; i++) { setState(i, setInsertedValues); } + + int leadIndex = this.leadIndex; + if (leadIndex >= index) { + leadIndex = this.leadIndex + length; + } + int anchorIndex = this.anchorIndex; + if (anchorIndex >= index) { + anchorIndex = this.anchorIndex + length; + } + if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) { + updateLeadAnchorIndices(leadIndex, anchorIndex); + } + fireValueChanged(); } *************** *** 543,548 **** --- 556,574 ---- for(int i = rmMinIndex; i <= maxIndex; i++) { setState(i, value.get(i + gapLength)); } + + int leadIndex = this.leadIndex; + if (leadIndex > rmMaxIndex) { + leadIndex = this.leadIndex - gapLength; + } + int anchorIndex = this.anchorIndex; + if (anchorIndex > rmMaxIndex) { + anchorIndex = this.anchorIndex - gapLength; + } + if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) { + updateLeadAnchorIndices(leadIndex, anchorIndex); + } + fireValueChanged(); } ###@###.### ======================================================================
24-08-2004