禁用JComboBox箭头按钮

时间:2014-03-23 21:09:47

标签: 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;
                    }
                };
            }
        });
    }
}

有没有办法禁用它?

3 个答案:

答案 0 :(得分: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));
    }
}

答案 1 :(得分:2)

如果需要,您可以删除它:

import java.awt.Component;
import java.awt.Container;

import javax.swing.AbstractButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class PlayWithComboArrow {
   public static void main(String[] args) {
      String[] items = {"Fe", "Fi", "Fo", "Fum"};
      JComboBox<String> combo = new JComboBox<String>(items);
      combo.setEditable(true);

      removeButton(combo);

      JOptionPane.showMessageDialog(null, combo);
   }

   private static void removeButton(Container container) {
      Component[] components = container.getComponents();
      for (Component component : components) {
         if (component instanceof AbstractButton) {
            container.remove(component);
         }
      }
   }
}

答案 2 :(得分:2)

也许你可以使用:

  • UIManager.put("ComboBox.squareButton", Boolean.FALSE);
  • JButton#setBorder(BorderFactory.createEmptyBorder());
  • JButton#setVisible(false);

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;

public final class DisableArrowButtonTest {
  public JComponent makeUI() {
    String[] items = {
      "JComboBox 111111111:", "JComboBox 222:", "JComboBox 33:"
    };
    JComboBox<String> comboBox0 = new JComboBox<>(items);
    JComboBox<String> comboBox1 = new MyComboBox1<>(items);
    JComboBox<String> comboBox2 = new MyComboBox2<>(items);
    comboBox0.setEditable(true);
    comboBox1.setEditable(true);
    comboBox2.setEditable(true);

    JPanel p = new JPanel();
    p.add(comboBox0);
    p.add(Box.createRigidArea(new Dimension(300, 40)));
    p.add(comboBox1);
    p.add(Box.createRigidArea(new Dimension(300, 40)));
    p.add(comboBox2);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new DisableArrowButtonTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    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));
  }
}

class MyComboBox2<E> extends JComboBox<E> {
  public MyComboBox2(E[] list) {
    super(list);
  }
  @Override public void updateUI() {
    super.updateUI();
    UIManager.put("ComboBox.squareButton", Boolean.FALSE);
    setUI(new BasicComboBoxUI() {
      @Override protected JButton createArrowButton() {
        JButton b = new JButton();
        b.setBorder(BorderFactory.createEmptyBorder());
        b.setVisible(false);
        return b;
      }
    });
    setBorder(BorderFactory.createLineBorder(Color.GRAY));
  }
}