-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.4.2
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
When an existing combo box element name is typed in rather than selected, the class for the selected item is String, rather than the underlying item type of the object in the model.
This occurs even if the combo box itself is deleted and a new one with the same model instantiated, which implies that somehow the DefaultComboBoxModel is keeping out-of-date information.
I suppose that this isn't exactly a bug, given that the documentation doesn't seem to say anything about the expected behaviour, but it's certainly counter-intuitive.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the program. This has a DefaultComboBoxModel which is populated wth Ref objects (these are slightly more complicated than necessary because of the original program I took them from).
Each time you click the button, it deletes any existing combo box, and creates a new one. It outputs the selected index and if this is >=0, outputs the class of the
item at the selected index, and the class of the selected item.
For the results below:
Select "Item0" and click
Type "item1" and click
Select "Item1" and click
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Selected index: -1
Selected item:null
Selected index: 0
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item0
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class java.lang.TestCombo.MainProg$Ref
Selected item:item1
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item1
ACTUAL -
Selected index: -1
Selected item:null
Selected index: 0
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item0
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class java.lang.String // SHOULD BE REF IMHO
Selected item:item1
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item1
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/*
* MainProg.java
*
* Created on 26 May 2006, 12:17
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ptoye.TestCombo;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
*
* @author PToye
*/
public class MainProg extends JFrame {
/**
* Creates a new instance of MainProg
*/
public MainProg() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Testcombo");
rl=new RefList();
rl.add(new Ref(1, "Item0"));
rl.add(new Ref(2, "Item1"));
btnButton=new JButton("New combo");
AL al=new AL();
btnButton.addActionListener(al);
add(btnButton,BorderLayout.NORTH);
pack();
}
private class AL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (cboBox!= null) {
cboBox.setVisible(false);
remove(cboBox);
cboBox=null;
}
cboBox=new JComboBox(rl);
cboBox.setEditable(true);
add(cboBox, BorderLayout.CENTER);
pack();
int selIndex=cboBox.getSelectedIndex();
System.out.println("Selected index: "+selIndex);
if (selIndex>=0) {
System.out.println("Selected index class: "+rl.getElementAt(selIndex).getClass());
System.out.println("Selected item class: "+rl.getSelectedItem().getClass());
}
System.out.println("Selected item:"+rl.getSelectedItem());
}
}
private class Ref implements Comparable<Ref> {
/**
* Creates a new instance of Ref
*/
public Ref(int Id, String Name) {
super();
id=Id;
name=Name;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int compareTo(Ref o) {
return name.compareToIgnoreCase(o.getName());
}
public boolean equals(Object o) {
if (o==null) {
return false;
}
String s= o.toString();
return name.equalsIgnoreCase(s);
}
public String toString() {
return name;
}
public int hashCode() {
return name.hashCode();
}
private int id;
private String name;
}
private class RefList extends DefaultComboBoxModel {
/**
* Creates a new instance of RefList
*/
public RefList() {
super();
}
/**
* Adds a new item to the list in alphabetical order
* If it is already there, does nothing
* Result is the index of the added (or found) item
*/
public int add(Ref ref) {
int i;
int ListSize=getSize();
for (i = 0; i < ListSize; i++) {
int j= ref.compareTo((Ref)getElementAt(i));
if (j==0) {
return i;
} else if (j<0) {
break;
}
}
super.insertElementAt(ref,i);
return i;
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainProg().setVisible(true);
}
});
}
private JComboBox cboBox;
private JButton btnButton;
private RefList rl;
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Be very careful when looking at the selected item. Use the item at the selected index instead.
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
When an existing combo box element name is typed in rather than selected, the class for the selected item is String, rather than the underlying item type of the object in the model.
This occurs even if the combo box itself is deleted and a new one with the same model instantiated, which implies that somehow the DefaultComboBoxModel is keeping out-of-date information.
I suppose that this isn't exactly a bug, given that the documentation doesn't seem to say anything about the expected behaviour, but it's certainly counter-intuitive.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the program. This has a DefaultComboBoxModel which is populated wth Ref objects (these are slightly more complicated than necessary because of the original program I took them from).
Each time you click the button, it deletes any existing combo box, and creates a new one. It outputs the selected index and if this is >=0, outputs the class of the
item at the selected index, and the class of the selected item.
For the results below:
Select "Item0" and click
Type "item1" and click
Select "Item1" and click
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Selected index: -1
Selected item:null
Selected index: 0
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item0
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class java.lang.TestCombo.MainProg$Ref
Selected item:item1
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item1
ACTUAL -
Selected index: -1
Selected item:null
Selected index: 0
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item0
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class java.lang.String // SHOULD BE REF IMHO
Selected item:item1
Selected index: 1
Selected index class: class com.ptoye.TestCombo.MainProg$Ref
Selected item class: class com.ptoye.TestCombo.MainProg$Ref
Selected item:Item1
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/*
* MainProg.java
*
* Created on 26 May 2006, 12:17
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ptoye.TestCombo;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
*
* @author PToye
*/
public class MainProg extends JFrame {
/**
* Creates a new instance of MainProg
*/
public MainProg() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Testcombo");
rl=new RefList();
rl.add(new Ref(1, "Item0"));
rl.add(new Ref(2, "Item1"));
btnButton=new JButton("New combo");
AL al=new AL();
btnButton.addActionListener(al);
add(btnButton,BorderLayout.NORTH);
pack();
}
private class AL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (cboBox!= null) {
cboBox.setVisible(false);
remove(cboBox);
cboBox=null;
}
cboBox=new JComboBox(rl);
cboBox.setEditable(true);
add(cboBox, BorderLayout.CENTER);
pack();
int selIndex=cboBox.getSelectedIndex();
System.out.println("Selected index: "+selIndex);
if (selIndex>=0) {
System.out.println("Selected index class: "+rl.getElementAt(selIndex).getClass());
System.out.println("Selected item class: "+rl.getSelectedItem().getClass());
}
System.out.println("Selected item:"+rl.getSelectedItem());
}
}
private class Ref implements Comparable<Ref> {
/**
* Creates a new instance of Ref
*/
public Ref(int Id, String Name) {
super();
id=Id;
name=Name;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int compareTo(Ref o) {
return name.compareToIgnoreCase(o.getName());
}
public boolean equals(Object o) {
if (o==null) {
return false;
}
String s= o.toString();
return name.equalsIgnoreCase(s);
}
public String toString() {
return name;
}
public int hashCode() {
return name.hashCode();
}
private int id;
private String name;
}
private class RefList extends DefaultComboBoxModel {
/**
* Creates a new instance of RefList
*/
public RefList() {
super();
}
/**
* Adds a new item to the list in alphabetical order
* If it is already there, does nothing
* Result is the index of the added (or found) item
*/
public int add(Ref ref) {
int i;
int ListSize=getSize();
for (i = 0; i < ListSize; i++) {
int j= ref.compareTo((Ref)getElementAt(i));
if (j==0) {
return i;
} else if (j<0) {
break;
}
}
super.insertElementAt(ref,i);
return i;
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainProg().setVisible(true);
}
});
}
private JComboBox cboBox;
private JButton btnButton;
private RefList rl;
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Be very careful when looking at the selected item. Use the item at the selected index instead.
- relates to
-
JDK-6455449 Allow user to select non-String items in editable JComboBox, typing their toString() representation
-
- Open
-