How to disable certain items in a JComboBox

后端 未结 2 699
无人共我
无人共我 2021-01-25 07:12

I have a ComboBox in which I have 8 Items out of which I want to display all but on a certain condition, the user should only be able to select the first two of the

2条回答
  •  终归单人心
    2021-01-25 07:24

    To solve @Samsotha 's code "problem" where disabled items are still selectable, I found and adapted a code which works fine for this purpose. Here is a working example on how you can call it and use it in similar way to a JComboBox:

    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.ListCellRenderer;
    import javax.swing.UIManager;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    /**
     * Creates a util class to generate a JComboBox with enabled/disabled items.
     * Strongly adapted from original post of Joris Van den Bogaert at http://esus.com/disabling-some-of-the-elements-in-a-jcombobox/
     */
    
    @SuppressWarnings({ "unchecked" })
    public class MyComboBox extends JComboBox {
    
    private static final long serialVersionUID = 6975854742812751380L;
    
    /**************************************************
     * FOR TESTING:
     */
    
    public static void main(String[] args) throws Exception {
    
         // Way 1: load an array
         ConditionalItem[] arr = new ConditionalItem[] {
             new ConditionalItem("Item 0", false),
             new ConditionalItem("Item 1", false),
             new ConditionalItem("Item 2"),
             new ConditionalItem("Item 3", false),
             new ConditionalItem("Item 4", true)
         };
         MyComboBox combo = new MyComboBox(arr);
    
    //      // Way 2: load oned by one (allows run-time modification)
    //      MyComboBox combo = new MyComboBox();
    //      combo.addItem("sss", false);
    //      combo.addItem("ffffd", true);
    //      combo.addItem("eeee");
    
        // Way 3: initial load and oned by one on run-time
        combo.addItem("Item 5");
        combo.addItem("Item 6", false);
        combo.addItem("Item 7", true);
    
        JPanel panel = new JPanel(new FlowLayout());
        panel.add(new JLabel("Test:"));
        panel.add(combo);
    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    
        Thread.sleep(2*1000);
        combo.setItem("Item 2", false);
    
        Thread.sleep(2*1000);
        combo.setItem("Item 8", false);
    }
    
    /**************************************************
     * CONSTRUCTORS:
     */
    
    ActionListener listener;
    
    public MyComboBox() {
        this.setRenderer(new ConditionalComboBoxRenderer());
    }
    
    public MyComboBox(ConditionalItem[] arr) {
        for(ConditionalItem ci : arr) {
            this.addItem(ci);
        }
        this.setRenderer(new ConditionalComboBoxRenderer());
        listener = new ConditionalComboBoxListener(this);
        this.addActionListener(listener);
    }
    
    public void addItem(String str) {
        addItem(new ConditionalItem(str, true));
    }
    
    public void addItem(String str, boolean bool) {
        addItem(new ConditionalItem(str, bool));
    }
    
    public void addItem(Component ci) {
        this.add(ci);
        this.setRenderer(new ConditionalComboBoxRenderer());
        this.addActionListener(new ConditionalComboBoxListener(this));
    }
    
    /** if combobox contains "str", sets its state to "bool"; 
     *  if it's not yet an item, ignores it; 
     *  the method also re-sets the selected item to the first one 
     *  shown in the list as "true", and disables the listeners in this 
     *  process to avoid firing an action when reorganizing the list. 
     */
    public void setItem(String str, boolean bool) {
        int n = this.getItemCount();
        for (int i=0; i
        

    提交回复
    热议问题