JDK-4110721 : JComboBox-FOCUS_GAINED event not fired if combobox is editable.
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.6,1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6,windows_nt
  • CPU: x86,sparc
  • Submitted: 1998-02-09
  • Updated: 1998-10-06
  • Resolved: 1998-10-06
Related Reports
Duplicate :  
Description
In JComboBox, the FOCUS_GAINED event is not fired if the combobopx is editable. This happens if the focus is set by tabbing into the component with the keyboard and not when the mouse is used to set focus to the combobox. This happens on both Solaris and Win NT


Steps to reproduce the same,
1. Run the application for which the sample code is given below.
2. Set focus to the first dummy button
3. Press <TAB> - focus shifts to the next dummy button.
4. Press <TAB> - focus shifts to the Combobox - Observe in the output that the FOCUS_GAINED event which is supposed to be fired in not fired.
5. Press <TAB> 3 times. - Ths focus is now set to the second combo box which has setEditable --> false.
6. Now the FOCUS_GAINED event is fired.


-- Sample Code --

import	java.io.*;
import	java.util.*;
import	java.awt.*;
import	java.awt.event.*;
import	com.sun.java.swing.*;



public class TestJCombo01 extends JFrame	implements FocusListener {
	/*************************************** Bug Description ***************************************/
	
	String[]	strBugDesc = {	"",
							"",
							"",
							"",
							"This TestCase tests the JComboBox Component",
							"**Problem** : FocusEvent not fired when JComboBox is editable.",
							"",
							"Steps to reproduce :",
							"1. Set focus to the first dummy button",
							"2. Press <TAB> - focus shifts to the next dummy button",
							"3. Press <TAB> - focus shifts to the Combobox - Observe in the output that the FOCUS_GAINED event which ",
							" is supposed to be fired in not fired.",
							"4. Press <TAB> 3 times. - Ths focus is now set to the second combo box which has setEditable --> false.",
							"5. Now the FOCUS_GAINED event is fired.",
							"",
							"Pass/Fail Criteria : ",
							"Pass -- FOCUS_GAINED event should be fired even when the JComboBox is editable..",
							"",
							"",
							"",
							""};
 	/*************************************** Bug Description ***************************************/

	Container		content;
	JPanel			panel;
	JComboBox	combo;
	JButton			label;
	String[]		data = {"one", "two", "free", "four", "five", "six", "seven", "eight", "nine", "ten"};
	
	
	TestJCombo01(String[]	args) {
		displayBugDesc();
		content = getContentPane();
		content.setLayout(new BorderLayout());
		addWindowListener(new WindowAdapter() {
									public void windowClosing(WindowEvent event){
										System.exit(0);
									}
								}
		);

		panel = new JPanel();
		panel.setLayout(new GridLayout(0, 2, 10, 10));

		panel.add(new JButton("Dummy Button"));
		panel.add(new JButton("Dummy Button"));
		panel.add(new JLabel("ComboBox with setEditable --> true"));
		combo = new JComboBox();
		combo.setEditable(true);
		combo.addFocusListener(this);

		for(int i =0; i < data.length; i ++) {
			combo.addItem(data[i]);
		}
		panel.add(combo);


		panel.add(new JButton("Dummy Button"));
		panel.add(new JButton("Dummy Button"));
		panel.add(new JLabel("ComboBox with setEditable --> false"));
		combo = new JComboBox();
		combo.setEditable(false);
		combo.addFocusListener(this);

		for(int i =0; i < data.length; i ++) {
			combo.addItem(data[i]);
		}
		panel.add(combo);


	
		content.add("Center", panel);
		pack();
		show();
	}

	public void displayBugDesc() {
		int n = strBugDesc.length;
		System.out.println("/******************************* Bug Description  and Comments *******************************/");
		System.out.println();
		for(int i = 0; i < n; i ++) {
			System.out.println(strBugDesc[i]);
		}
		System.out.println();
		System.out.println("/******************************* Bug Description  and Comments *******************************/");
	}



	public void focusGained(FocusEvent	e) {
		System.out.println(e.paramString());
	}

	public void focusLost(FocusEvent	e) {
		System.out.println(e.paramString());
	}



	// Main funtion	
	public static void main(String[]	args) {
		TestJCombo01	test = new TestJCombo01(args);
	}
}


-- Sample Code --

Comments
WORK AROUND The workaround is to traverse the children of the combobox and add focus listeners to each of them. It's horrible but it will work. In some cases it may be necessary to have a ContainerListener listen the addition/removal of child components and add/remove focus listeners accordingly.
11-06-2004

EVALUATION This is because combo box is a compound component and the editable text field gets the focus instead of the combo box. This can be fixed by adding code to the combo box editors in Basic, Organic, and Metal. tom.santos@eng 1998-02-09 Running attched description attached sample program by Beta 4 "K". Now both editable/noneditable do not fire focus event. Since there are no clear description on API documents, this causes confusion in developers. At least, please clarify what's the expected result -- in other words, specification --. koushi.takahashi@japan 1998-07-06 ====================================================================== Hania and I took a look at this, and decided the following: 1. This is merely one example of a class of focus problems involving composite component classes 2. There is a workaround for this (which I will add) 3. A solution to the class of problems needs to be designed rather than applying a patch to this particular case. We do think it is important to do this, but don't see it being done in time for for JDK1.2 fcs. georges.saab@Eng 1998-07-08
08-07-1998