Java Swing How to - Disable JComboBox arrow button








Question

We would like to know how to disable JComboBox arrow button.

Answer

import java.awt.Component;
import java.awt.Container;
//from   w ww.  j  a  v  a  2 s . com
import javax.swing.AbstractButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class Main {
   public static void main(String[] args) {
      String[] items = {"A", "B", "C", "D"};
      JComboBox<String> combo = new JComboBox<String>(items);
      combo.setEditable(true);

      removeButton(combo);

      JOptionPane.showMessageDialog(null, combo);
   }

   private static void removeButton(Container container) {
      Component[] components = container.getComponents();
      for (Component component : components) {
         if (component instanceof AbstractButton) {
            container.remove(component);
         }
      }
   }
}