JDK-4188128 : JComboBox drops its cursor after L&F change
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.6,1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_95
  • CPU: generic,x86
  • Submitted: 1998-11-08
  • Updated: 1999-01-28
  • Resolved: 1999-01-28
Related Reports
Duplicate :  
Description

Name: krT82822			Date: 11/08/98


This JComboBox bug occurs with both JDK v1.2b4.1 *and* JDK v1.1.6
w/Swing v1.1b2.

I'm using WinNT v4.0, SP3.

I've included a minimal sample program to illustrate the buggy behavior.

The problem has to do with setting the cursor for a JComboBox's "edit
field" when setEditable(true) has been called on the JComboBox.  The
problem is that when the LaF changes, the cursor reverts back to the
standard arrow.

Not that if I replace the JComboBox with a JTextField, this problem does
not occur.


Follow these steps:

1.  Run the attached program.
2.  Move the cursor over the edit field of the JComboBox.  Note the
shape of the cursor.
3.  Change the LaF to any of the three available LaFs.
4.  Move the cursor over the edit field of the JComboBox.  Note the
shape of the cursor -- it is an arrow!


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

class Tester9
{
   private static final JComboBox comboBox = new JComboBox();

   public static void main(String[] args)
   {
      final JFrame    f = new JFrame();

      // WindowListener for closing progam.
      f.addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent evt)
         {
            evt.getWindow().dispose();
            System.exit(0);
         }
      });

      f.getContentPane().add(comboBox, BorderLayout.NORTH);
      
      comboBox.setEditable(true);
      
      Component editorComponent = comboBox.getEditor().getEditorComponent();
      editorComponent.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
      
      //--------------------
      // The remaining code simply allows for a change in the LaF....
      //--------------------
      JPanel LaFPanel = new JPanel();

      final JButton windowsLaF = new JButton("Windows"),
                    motifLaF   = new JButton("Motif"),
                    metalLaF   = new JButton("Metal");

      LaFPanel.add(windowsLaF);
      LaFPanel.add(motifLaF);
      LaFPanel.add(metalLaF);

      ActionListener LaFListener = new ActionListener()
      {
         public void actionPerformed(ActionEvent evt)
         {
            Object src = evt.getSource();

            try
            {
               if(src == windowsLaF)
                  UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
               else if(src == motifLaF)
      	    	   UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
               else
      	    	   UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");

               SwingUtilities.updateComponentTreeUI(f);
            }
            catch(Exception e)
            {
               System.err.println("*** ERROR IN CHANGING LAF: " + e);
            }
         }
      };

      windowsLaF.addActionListener(LaFListener);
      motifLaF.addActionListener(LaFListener);
      metalLaF.addActionListener(LaFListener);

      f.getContentPane().add(LaFPanel, BorderLayout.SOUTH);
      f.setBounds(50, 50, 300, 300);
      f.setVisible(true);
   }
}
(Review ID: 38139)
======================================================================

Comments
WORK AROUND Name: krT82822 Date: 11/08/98 Explicit re-setting of text field's cursor after L&F change restores the expected behavior. ======================================================================
11-06-2004

EVALUATION It is up to each look and feel to provide the subcomponents for JComboBox (like the edit field, for example). When you switch L&Fs, the edit field that had your cursor on it goes away and a new editor is made for the new L&F. This problem would be fixed if we had a fix for the compound component problem, so I'm marking this as a duplicate of bug 4144505. tom.santos@eng 1999-01-28
28-01-1999