-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
5.0
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode)
FULL OS VERSION :
Microsoft Windows XP [version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Due to a bug in BasicComboBoxUI.EditorFocusListener, when you self-implements a ComboBoxEditor where getItem() returns "null", it does not send a notification (when tabbing or losting focus).
The user is obliged to press enter when he has done typing. Otherwise modification doesn't take effects.
The problem i saw is -- BasicComboBoxUI.EditorFocusListener.focusLost() --
"
if (!e.isTemporary() && item != null &&
!item.equals( comboBox.getSelectedItem())) {
"
Item could be null and then need to notify if getSelectedItem is not null.
Proposed solution:
Object o = comboBox.getSelectedItem();
if (!e.isTemporaty() && item == null ? o != null : !item.equals(o))
So, if selectedItem returns null and item is null no notification is made.
But, if item is null and selectedItem is not null then notify.
Also, if item is not equal to selectedItem then notify.
This behavior only happens there. It work properly with "ENTER" pressed.
Sorry for mistaken english!! :)
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run attached source code.
enter anything you want in JComboBox (ie: "abc")
press "Click Me" button... Prints "Button Pressed: abc" (OK)
remove what you typed in (WITHOUT pressing enter).
press "Click Me" button... Still prints "Button Pressed: abc" (Should be null).
That's it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
getSelectedItem to return null;
ACTUAL -
getSelectedItem returns last selected item;
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/*
* Created on 2003-10-23
* Copyright 2003-10-23 Info-Micro PC (IMPC)
* All rights reserved.
*/
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* @author Jean-Francois
* Type your description
*/
public class Bug implements ComboBoxEditor{
public static void main(String[] args) {
JFrame frame = new JFrame("Tester App");
final JComboBox cbx = new JComboBox();
cbx.setEditable(true);
cbx.setEditor(new Bug());
JPanel pnl = new JPanel();
pnl.add(cbx);
JButton btn = new JButton("Click Me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed: " + cbx.getSelectedItem());
}
});
pnl.add(btn);
frame.getContentPane().add(pnl);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
protected JTextField editor = new JTextField();
public void addActionListener(ActionListener l) {
editor.addActionListener(l);
}
public Component getEditorComponent() {
return editor;
}
public Object getItem() {
if(editor.getText().length() == 0){
return null;
}
return editor.getText();
}
public void removeActionListener(ActionListener l) {
editor.removeActionListener(l);
}
public void selectAll() {
editor.selectAll();
}
public void setItem(Object anObject) {
editor.setText(anObject == null ? null : anObject.toString());
}
}
---------- END SOURCE ----------
###@###.### 2005-04-05 22:00:47 GMT
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode)
FULL OS VERSION :
Microsoft Windows XP [version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Due to a bug in BasicComboBoxUI.EditorFocusListener, when you self-implements a ComboBoxEditor where getItem() returns "null", it does not send a notification (when tabbing or losting focus).
The user is obliged to press enter when he has done typing. Otherwise modification doesn't take effects.
The problem i saw is -- BasicComboBoxUI.EditorFocusListener.focusLost() --
"
if (!e.isTemporary() && item != null &&
!item.equals( comboBox.getSelectedItem())) {
"
Item could be null and then need to notify if getSelectedItem is not null.
Proposed solution:
Object o = comboBox.getSelectedItem();
if (!e.isTemporaty() && item == null ? o != null : !item.equals(o))
So, if selectedItem returns null and item is null no notification is made.
But, if item is null and selectedItem is not null then notify.
Also, if item is not equal to selectedItem then notify.
This behavior only happens there. It work properly with "ENTER" pressed.
Sorry for mistaken english!! :)
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run attached source code.
enter anything you want in JComboBox (ie: "abc")
press "Click Me" button... Prints "Button Pressed: abc" (OK)
remove what you typed in (WITHOUT pressing enter).
press "Click Me" button... Still prints "Button Pressed: abc" (Should be null).
That's it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
getSelectedItem to return null;
ACTUAL -
getSelectedItem returns last selected item;
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
/*
* Created on 2003-10-23
* Copyright 2003-10-23 Info-Micro PC (IMPC)
* All rights reserved.
*/
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* @author Jean-Francois
* Type your description
*/
public class Bug implements ComboBoxEditor{
public static void main(String[] args) {
JFrame frame = new JFrame("Tester App");
final JComboBox cbx = new JComboBox();
cbx.setEditable(true);
cbx.setEditor(new Bug());
JPanel pnl = new JPanel();
pnl.add(cbx);
JButton btn = new JButton("Click Me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed: " + cbx.getSelectedItem());
}
});
pnl.add(btn);
frame.getContentPane().add(pnl);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
protected JTextField editor = new JTextField();
public void addActionListener(ActionListener l) {
editor.addActionListener(l);
}
public Component getEditorComponent() {
return editor;
}
public Object getItem() {
if(editor.getText().length() == 0){
return null;
}
return editor.getText();
}
public void removeActionListener(ActionListener l) {
editor.removeActionListener(l);
}
public void selectAll() {
editor.selectAll();
}
public void setItem(Object anObject) {
editor.setText(anObject == null ? null : anObject.toString());
}
}
---------- END SOURCE ----------
###@###.### 2005-04-05 22:00:47 GMT