I want to remove the editor from a JComboBox to make the arrow button span the entire width of the box.
I tried to use a custom ComboBoxUI to set the size and color of the arrow button. This partially works, but because i couldn't figure out how to remove the editor, the button is only half the width of the entire box.
The main class:
import javax.swing.*;
import java.awt.*;
public class Box {
public Box() {
JFrame frame = new JFrame("frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,250);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
String[] items = { "", "item1", "item2", "item3", "item4" };
JComboBox<String> box = new JComboBox<>(items);
box.setBounds(100,20,200,50);
UIManager.put("ComboBox.squareButton", Boolean.FALSE);
box.setUI(new ComboUI());
box.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Color.BLACK));
panel.add(box);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Box();
}
}
And the custom ComboBoxUI:
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import java.awt.*;
public class ComboUI extends BasicComboBoxUI {
@Override
protected JButton createArrowButton() {
final JButton button = new JButton("Items");
button.setBackground(Color.GRAY);
button.setForeground(Color.WHITE);
button.setBounds(0,0, 200,40);
return button;
}
}
This code results in this:
Arrow button half the size of combo box"
What i would like instead is this:
Arrow button same size as combo box:


I created the following GUI using a
JButtonand aPopupfrom thePopupFactory.There's a
JLabelat the top to show the selection and aJButtonto show the item options. Unfortunately, I couldn't print the GUI when the item list is displayed.When you left-click the
JButton, the list of items appear under theJButton. When you select an item, the list disappears and the selected item is displayed.This is a proof of concept. You would need to add code to hide the list when the Esc key is pressed to cancel the list, as one example.
Here's the complete runnable code.