Can we disable JComboBox arrow button in Java?

Java 8Object Oriented ProgrammingProgramming

Yes, we can do that using removeArrow() method.

The following is an example to disable JComboBox arrow button:

Example

import java.awt.Component;
import java.awt.Container;
import javax.swing.*;
public class SwingDemo {
   public static void main(String[] args) {
      String[] strValues = {"One", "Two"};
      JComboBox<String> comboBox = new JComboBox<String>(strValues);
      removeArrow(comboBox);
      JOptionPane.showMessageDialog(null, comboBox);
   }
   private static void removeArrow(Container container) {
      Component[] c = container.getComponents();
      for (Component res : c) {
         if (res instanceof AbstractButton) {
            container.remove(res);
         }
      }
   }
}

Output

raja
Published on 02-May-2019 11:03:00
Advertisements