JDK-4492457 : setLabelFor() does not work with editable JComboBoxes
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-08-15
  • Updated: 2002-05-30
  • Resolved: 2002-05-30
Related Reports
Duplicate :  
Description

Name: yyT116575			Date: 08/15/2001


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

Editable JComboBoxes do not receive focus via ALT-mnemonic, 
where the mnemonic is set via JLabel.setDisplayedMnemonic() and 
JLabel.setLabelFor(JComboBox) is called.  When ALT-mnemonic 
is pressed in this situation, focus is lost completely.  If you hit
TAB after pressing ALT-mnemonic, the JComboBox receives focus.

The following is a simple program that demonstrates the bug:

// Testing the setLabelFor behavior of the JLabel/JComboBox
import java.awt.*;
import javax.swing.*;

public class ComboBoxTest {

  public static void main(String[] args) {

    String syslooknfeel = UIManager.getSystemLookAndFeelClassName();
    try {
      UIManager.setLookAndFeel(syslooknfeel);
    } catch (Exception ex) {
      System.out.println("Could not find System Look and Feel.");
      ex.printStackTrace();
    }
	
    display();
  }

  public static void display() {

    JFrame myframe = new JFrame("ComboBox test");
    createUI(myframe.getContentPane());

    myframe.pack();
    myframe.setVisible(true);
  }
    
  public static void createUI(Container container) {

    GridBagLayout lay = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
	
    JPanel panel = new JPanel( lay );

    JLabel label = new JLabel("Label");
    label.setDisplayedMnemonic('L');
    JComboBox combobox = new JComboBox();
    combobox.addItem("first");
    combobox.addItem("second");
    label.setLabelFor(combobox);

    JLabel label2 = new JLabel("Label: Part Deux");
    label2.setDisplayedMnemonic('D');
    JComboBox combobox2 = new JComboBox();
    combobox2.addItem("third");
    combobox2.addItem("fourth");
    combobox2.setEditable(true);
    label2.setLabelFor(combobox2);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0.9;
    gbc.weighty = 0;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets( 10,4, 10,4 );

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    lay.setConstraints(label, gbc);
    panel.add(label);

    gbc.gridx = 2;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    lay.setConstraints(combobox, gbc);
    panel.add(combobox);

    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    lay.setConstraints(label2, gbc);
    panel.add(label2);

    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    lay.setConstraints(combobox2, gbc);
    panel.add(combobox2);

    container.add(panel);
  }
}
(Review ID: 130032) 
======================================================================

Comments
EVALUATION Once again, this seems to be a problem with requestFocus on an editable JComboBox and is covered by 4513773. When I implement the fix for that bug then the problem goes away. See that bug report for a work around. ###@###.### 2002-05-30
30-05-2002