Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4364937

Look and feel causes key naviagtion in JComboBox to crash

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • client-libs



      Name: skT45625 Date: 08/22/2000


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


      The following source code creates a button to change look and feel, and a menu
      item in the menu bar to show a dialog containing a combo box.

      Steps to reproduce the problem
      ==============================
      (1) Run the following program
      (2) Click on the "Change" button
      (3) Show the "My Dialog" with the JMenuItem
      (4) Close the dialog
      (5) Click on the "Change" button once again
      (6) Show the "My Dialog" with the JMenuItem
      (7) When the dialog is open, press the down arrow key on the keyboard. The
      program will give a ClassCastException

      java.lang.ClassCastException: javax.swing.plaf.metal.MetalComboBoxUI
          at
      com.sun.java.swing.plaf.windows.WindowsComboBoxUI$DownAction.ActionPerformed(Win
      dowComboBoxUI.java:154)

      Note: The bug can be reproduced.


      Source Code
      ===========
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class TestComboBox extends JFrame implements ActionListener
      {
      private static final String METAL_CLASS_NAME
      = "javax.swing.plaf.metal.MetalLookAndFeel";
          private static final String WINDOWS_CLASS_NAME
           = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

      private JMenuItem m_show;
      private JButton m_button;
      private boolean m_changed;

      public TestComboBox()
      {
      JPanel panel = (JPanel) getContentPane();
      panel.setLayout(new BorderLayout());
      panel.add("South", m_button = new JButton("Change Look And
      Feel"));
      m_button.addActionListener(this);

      JMenuBar bar = new JMenuBar();
      JMenu menu = new JMenu("File");
      m_show = new JMenuItem("show dialog");
      m_show.addActionListener(this);
      menu.add(m_show);
      bar.add(menu);

      getRootPane().setJMenuBar(bar);
      setSize(new Dimension(100, 100));
      }

      public void actionPerformed(ActionEvent e)
      {
      Object source = e.getSource();
      if (source == m_show)
      {
      MyDialog dialog = new MyDialog(this);
      dialog.setVisible(true);
      } else if (source == m_button)
      {
      try
      {
      if (m_changed)
      {

      UIManager.setLookAndFeel(METAL_CLASS_NAME);
      } else
      {

      UIManager.setLookAndFeel(WINDOWS_CLASS_NAME);
      }
      } catch (Exception ex)
      {
      ex.printStackTrace();
      }

      SwingUtilities.updateComponentTreeUI(TestComboBox.this);

      m_changed = !m_changed;
      }
      }

      public static void main(String[] args)
      {
      TestComboBox tr = new TestComboBox();
      tr.setVisible(true);
      }

      // Inner class
      private class MyDialog extends JDialog
      {
      public MyDialog(JFrame frame)
      {
      super(frame, "My Dialog", true);
      JPanel panel = (JPanel) getContentPane();

      Object [] choices = {"0", "1", "2", "3", "4", "5"};
      panel.add(new JComboBox(choices));
      pack();
      setSize(getPreferredSize());
      }
      }
      }

      Stack Trace
      ===========
      java.lang.ClassCastException: javax.swing.plaf.metal.MetalComboBoxUI
          at
      com.sun.java.swing.plaf.windows.WindowsComboBoxUI$DownAction.ActionPerformed(Win
      dowComboBoxUI.java:154)
          at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1702)
          at javax.swing.JComponent.processKeyBinding(JComponent.java:2156)
          at javax.swing.JComponent.processBindings(JComponent.java:2193)
          at ...
      (Review ID: 108769)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            skondamasunw Suresh Kondamareddy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: