簡體   English   中英

禁用JComboBox箭頭按鈕

[英]Disable JComboBox arrow button

我嘗試制作一個沒有箭頭按鈕的可編輯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;
                    }
                };
            }
        });
    }
}

有沒有辦法禁用它?

我終於成功了! 訣竅是更改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));
    }
}

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

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);
         }
      }
   }
}

也許你可以使用:

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

在此輸入圖像描述

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));
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM