United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4513773 requestFocus on an editable JComboBox sends focus to the combobox button
JDK-4513773 : requestFocus on an editable JComboBox sends focus to the combobox button

Details
Type:
Bug
Submit Date:
2001-10-11
Status:
Resolved
Updated Date:
2003-04-12
Project Name:
JDK
Resolved Date:
2002-08-12
Component:
client-libs
OS:
windows_nt,generic,windows_2000
Sub-Component:
javax.swing
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.3.0,1.4.0
Fixed Versions:
1.4.2 (mantis)

Related Reports
Duplicate:
Duplicate:
Duplicate:

Sub Tasks

Description

Name: yyT116575			Date: 10/11/2001


java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)



Calling requestFocus() on an editable JComboBox sends the focus to the combobox
button instead of sending focus to the editor component.

Test: Run the attached program.
Result: The combobox button has the focus.


import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class TestBug6 extends JFrame {

  JComboBox comboBox;

  public TestBug6(String name) {
    super(name);

    JPanel panel = new JPanel(new FlowLayout());

    JTextField textField = new JTextField(10);
    panel.add(textField);

    comboBox = new JComboBox(new String[]{"One", "Two", "Three"});
    comboBox.setEditable(true);
    panel.add(comboBox);

    getContentPane().add(panel);

  }

  public void setComboBoxFocus() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        comboBox.requestFocus();
      }
    });
  }

  public static void main(String[] args) {
    TestBug6 test = new TestBug6("TestBug");
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    test.pack();
    test.setLocation(100, 70);
    test.setVisible(true);

    test.setComboBoxFocus();
  }
}
(Review ID: 133582) 
======================================================================

                                    

Comments
EVALUATION

Yup this is a bug all right. It's still in 1.4 and 1.4.1 beta. A quick solution is to add a block to the BasicComboBoxUI.FocusHandler which will request the focus on the editor in the focusGained method call. See suggested fix.


###@###.### 2002-05-24
                                     
2002-05-24
SUGGESTED FIX


------- BasicComboBoxUI.java -------
467a468,471
> 	    if (comboBox.isEditable() && editor != null) {
> 		editor.requestFocus();
> 	    }
> 

###@###.### 2002-05-24
                                     
2002-05-24
WORK AROUND



Name: yyT116575			Date: 10/11/2001


Request focus to the combobox editor component:

comboBox.getEditor().getEditorComponent().requestFocus();
======================================================================

Create a static adapter (stateless so it can be reused):

   // XXX workaround for 4513773
    public static class FocusHandler extends FocusAdapter {
	public void focusGained(FocusEvent evt) {
	    JComboBox cb = (JComboBox)evt.getSource();
	    if (cb.isEditable()) {
		Component editor = cb.getEditor().getEditorComponent();
		if (editor != null) {
		    editor.requestFocus();
		}
	    }
	}
    }

Add the FocusListener to your editable combo boxes:

   private static FocusListener focusHandler = new FocusHandler();

   combo.setEditable(true);
   combo.addFocusListener(focusHandler);



###@###.### 2002-05-30
                                     
2002-05-30
PUBLIC COMMENTS

The situation in which the editable combo box as a table cell editor is mostly fixed. Pressing F2 will put the focus on the editable portion of the combo box. 

However, this exposes another bug in which an enter pressed on the value to accept the edited contents will move the focus outside the table rather than to the edited cell. This is because of an incorrect focus cycle when composite components are used as a table cell editor. This bug existed before this fix and will be filed under a separate bug.
###@###.### 2002-06-21
                                     
2002-06-21
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
mantis
mantis-b02

FIXED IN:
mantis
mantis-b02

INTEGRATED IN:
mantis
mantis-b02


                                     
2004-06-14



Hardware and Software, Engineered to Work Together