Java Swing How to - Remove Arrow button from JComboBox








Question

We would like to know how to remove Arrow button from JComboBox.

Answer

// w  ww.j  a  v a2s.co  m
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicComboBoxUI;

public final class Main {
  public static void main(String[] args) {
    String[] items = {"A", "B", "C"};
    JComboBox<String> comboBox1 = new MyComboBox1<>(items);
    JPanel p = new JPanel();
    p.add(comboBox1);
    
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(p);
    f.setSize(320, 240);
    f.setVisible(true);
  }
}

class MyComboBox1<E> extends JComboBox<E> {
  public MyComboBox1(E[] list) {
    super(list);
  }
  @Override public void updateUI() {
    super.updateUI();
    setUI(new BasicComboBoxUI() {
      @Override protected JButton createArrowButton() {
        return new JButton() {
          @Override public int getWidth() {
            return 0;
          }
        };
      }
    });
    setBorder(BorderFactory.createLineBorder(Color.GRAY));
  }
}