0 Replies Latest reply: Jan 16, 2013 4:26 AM by 895146 RSS

    Problem customizing JComboBox: "infinite loops event" when changing LAF

    895146
      Dear all
      I customize my JComboBox as follow.
      The program ran ok with default LAF, but whenever i changed the LAF to System LAF, there was an infinite loop after the button was clicked. I saw that the actionPerformed method was called infinitely.
      Please help me solving this problem. I use jdk 1.6.0_33
      Thanks in advance.

      package sig.dw.ui;

      import java.awt.Component;
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.ComboBoxEditor;
      import javax.swing.DefaultComboBoxModel;
      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      //import javax.swing.event.EventListenerList;

      /**
      *
      * @author congnh
      */
      public class ButtonableComboBox extends JComboBox{

      private ButtonableComboBoxEditor comboBoxEditor;
      // private EventListenerList eventListenerList;

      public ButtonableComboBox(){
      super();
      comboBoxEditor = new ButtonableComboBoxEditor();
      // eventListenerList = new EventListenerList();
      setEditable(true);
      setEditor(comboBoxEditor);
      }

      public ButtonableComboBox(Object[] items){
      this();
      setModel(new DefaultComboBoxModel(items));
      }

      public void addButtonListener(ActionListener listener){
      comboBoxEditor.addActionListener(listener);
      // eventListenerList.add(ActionListener.class,listener);
      }

      public void removeButtonListener(ActionListener listener){
      comboBoxEditor.removeActionListener(listener);
      // eventListenerList.remove(ActionListener.class, listener);
      }

      class ButtonableComboBoxEditor implements ComboBoxEditor{

      private JButton button;

      public ButtonableComboBoxEditor(){
      button = new JButton();
      }

      @Override
      public Component getEditorComponent() {
      return button;
      }

      @Override
      public void setItem(Object anObject) {
      if(anObject!=null){
      button.setText(anObject.toString());
      }
      }

      @Override
      public Object getItem() {
      return button.getText();
      }

      @Override
      public void selectAll() {
      throw new UnsupportedOperationException("Not supported yet.");
      }

      @Override
      public void addActionListener(ActionListener l) {
      System.out.println("add new listener");
      button.addActionListener(l);
      }

      @Override
      public void removeActionListener(ActionListener l) {
      button.removeActionListener(l);
      }

      }

      public static void main(String args[]){
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
      @Override
      public void run(){
      String[] comboBoxItems = {"Browse","Explorer","Firefox","IE","Chrome","Opera"};
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout());
      ButtonableComboBox bcb = new ButtonableComboBox(comboBoxItems);
      bcb.addButtonListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
      System.out.println(e.getActionCommand());
      }
      });
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      panel.add(bcb);
      frame.add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      }
      });
      }

      }

      Edited by: monday0rsunday on Jan 16, 2013 2:26 AM