Remove border from JComboBox

后端 未结 2 943
眼角桃花
眼角桃花 2020-12-07 04:23

Do you know any way to remove the border from a JComboBox in Java? I try the following code

public class ComboFrame extends JFrame {
    public ComboFrame()          


        
相关标签:
2条回答
  • 2020-12-07 05:20

    If you want to use the windows L&F, you can do cmd.setUI(new WindowsComboBoxUI()); If you, however, want to be able to use any L&F, you might be better off using the solution proposed by Jeach.

    0 讨论(0)
  • 2020-12-07 05:28

    I did a bit of research and found this bug

    I tried it for myself and it does seem to affect the border. You might want to try one or both of the following code blocks for yourself.

    for (int i = 0; i < combo.getComponentCount(); i++) 
    {
        if (combo.getComponent(i) instanceof JComponent) {
            ((JComponent) combo.getComponent(i)).setBorder(new EmptyBorder(0, 0,0,0));
        }
    
    
        if (combo.getComponent(i) instanceof AbstractButton) {
            ((AbstractButton) combo.getComponent(i)).setBorderPainted(false);
        }
    }
    

    It is important to note that at the bottom of the bug entry, you can read the following:

    The JButton maintains it's own border so JComponent paintBorder() and paintComponent() has no awareness of the JComboBox border.

    Good luck,

    Jeach!

    0 讨论(0)
提交回复
热议问题