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

Execute addItem for JComboBox, then throws Exception.

XMLWordPrintable

    • swing1.1
    • generic
    • generic
    • Verified

      Run the attached program.
        - Select Look & Feel.
        - Execute addItem by "ADD" button.
        - and repeat addItem.

      Then, the following exception is occurred.
        Exception occurred during event dispatching:
        java.lang.ArrayIndexOutOfBoundsException: xx
        at com.sun.java.swing.plaf.basic.BasicListUI.convertRowToY(Compiled Code)
      :
      :

      When Look & Feel is not specified, this exception is not occurred.
      But you select Look & Feel once, this exception is sure to be occurred.

      Program : ComboTest.java
      ------------------------------------------------------------------------------
      import com.sun.java.swing.*;
      import java.awt.*;
      import java.util.*;
      import java.awt.event.*;

      public class ComboTest extends JFrame implements WindowListener, ActionListener, ItemListener {
      JComboBox combo;

      public ComboTest() {
      super("Combo Test");

      combo = new JComboBox();
      combo.addActionListener(this);
      combo.addItemListener(this);
      combo.setEditable(true);
      combo.addItem("Item 1");
      combo.addItem("Item 2");
      combo.addItem("Item 3");
      this.getContentPane().setLayout(new FlowLayout());
      this.getContentPane().add(combo);
      JButton button = new JButton("ADD");
      button.addActionListener(this);
      this.getContentPane().add(button);

      UIManagerMenu uim = new UIManagerMenu();
      uim.setMenuBar(this);

      setBounds(0,0,300,150);
      addWindowListener(this);
      setVisible(true);
      }

      public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().compareTo("ADD") == 0) {
      combo.addItem(combo.getEditor().getItem());
      }
      }

      public void itemStateChanged(ItemEvent e) {

      }

      public void windowOpened(WindowEvent e) {}
      public void windowClosing(WindowEvent e) {
      System.exit(0);
      }
      public void windowClosed(WindowEvent e) {}
      public void windowIconified(WindowEvent e) {}
      public void windowDeiconified(WindowEvent e) {}
      public void windowActivated(WindowEvent e) {}
      public void windowDeactivated(WindowEvent e) {}

      public static void main(String args[]) {
      ComboTest ct = new ComboTest();
      }
      }
      ------------------------------------------------------------------------------

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

      public class UIManagerMenu implements ActionListener{
      private Component parent = null;

      public UIManagerMenu(){
      }

      public void setMenuBar(JFrame frame){
      this.parent = frame;
      JMenuBar menubar = new JMenuBar();
      frame.setJMenuBar(menubar);
      this.setMenu(menubar, frame);
      }

      public void setMenu(JMenuBar menubar, JFrame frame){
      JMenu menu = new JMenu();
      menu = new JMenu("LookAndFeel");
              menu.setMnemonic('L');
      menubar.add(menu);

      this.parent = frame;

      UIManager.LookAndFeelInfo[] uiinfo = UIManager.getInstalledLookAndFeels();
      String laf = UIManager.getLookAndFeel().getID();

      JRadioButtonMenuItem rb;
      ButtonGroup rg = new ButtonGroup();
      for(int i = 0; i < uiinfo.length; i++){
      rb = new JRadioButtonMenuItem( uiinfo[i].getName() );
      menu.add(rb);
      rg.add(rb);
      rb.setActionCommand( uiinfo[i].getClassName() );
      rb.addActionListener(this);

      if (uiinfo[i].getName().compareTo(laf) == 0)
      rb.setSelected(true);
      }
      }

      public void actionPerformed(ActionEvent event) {
      String action = event.getActionCommand();
      try{
      UIManager.setLookAndFeel(action/*ClassName*/);
      SwingUtilities.updateComponentTreeUI(this.parent);
      }catch(Exception e){
      System.out.println("action setLookAndFeel err: " + e);
      e.printStackTrace();
      }
      }
      }
      -------------------------------------------------------------------------------

            tsantossunw Tom Santos (Inactive)
            eishidasunw Etsuko Ishida (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: