禁用JComboBox箭头按钮

Kwo*_*oin 3 java swing jcombobox

我尝试制作一个没有箭头按钮的可编辑JComboBox.(它将显示其下拉列表,具体取决于用户在其编辑器中输入的内容)

到目前为止,箭头按钮不可见,但仍然可以点击!它仍然会在点击时显示列表.

public class MyComboBox<E> extends JComboBox<E> {

    public MyComboBox(E[] list) {
        super(list);
        this.setEditable(true);
        setUI(new BasicComboBoxUI() {
            @Override
            protected JButton createArrowButton() {
                return new JButton() {
                    @Override
                    public int getWidth() {
                        return 0;
                    }
                };
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法禁用它?

Kwo*_*oin 5

我终于成功了!诀窍是更改UI后ComboBox没有相同的组件:

setUI方法调用之前列出组件时:

class javax.swing.plaf.metal.MetalComboBoxButton

class javax.swing.CellRendererPane

class javax.swing.plaf.metal.MetalComboBoxEditor $ 1

setUI方法调用之后列出组件时:

class kcomponent.MyComboBox $ 1 $ 1

class javax.swing.plaf.basic.BasicComboBoxEditor $ BorderlessTextField

class javax.swing.CellRendererPane

然后我来删除这些组件的MouseListeners,它处理第一个组件的第二个MouseListener:MyComboBox $ 1 $ 1.但光标仍然不同(鼠标指针而不是carret定位器),然后我完全删除它,它最终工作得很好!

这是我更正的代码:

public class MyComboBox<E> extends JComboBox<E> {

    public MyComboBox(E[] list) {
        super(list);
        this.setEditable(true);
        this.setUI(new BasicComboBoxUI() {
            @Override
            protected JButton createArrowButton() {
                return new JButton() {
                    @Override
                    public int getWidth() {
                        return 0;
                    }
                };
            }
        });
        this.remove(this.getComponent(0));
    }
}
Run Code Online (Sandbox Code Playgroud)