在运行时禁用JComboBox项

时间:2013-02-13 11:30:41

标签: java swing jcombobox

1.当用户从JComboBox中选择项目时,我创建了一个JComboBox和Jtable,它们被添加到JTable中。
            2.我不想让用户选择他之前在JComboBox中选择过的项目             3.因此必须禁用所选的选项(不可选择)。我该怎么办? 4.下面的代码在JTable中添加后从JComboBox中删除了该项,但我有兴趣禁用它

        String getchoice=(String)selectedgames_combobox.getSelectedItem();

        DefaultTableModel gamesmodel = new DefaultTableModel(); 

        //adding selected choices from JComboBox in JTable 
        gamesmodel.addColumn("Selected Games");     
        gamesmodel.insertRow(gamesmodel.getRowCount(),new Object[]{ getchoice }) ;  

        //refreshing table
        games_table.setModel(gamesmodel);

        //removing the selected item from JComboBox
        selectedgames_combobox.removeItem(getchoice);

2 个答案:

答案 0 :(得分:5)

假设您有一个列表,其中包含一些应禁用的元素,您需要更改已禁用元素的外观,并且需要阻止用户选择这些项目。为了能够阻止用户选择已禁用的用户,您需要覆盖setSelectedIndex这样的JComboBox方法:

public void setSelectedIndex(int index) {
   if (!disabled_items.contains(index)) {
       super.setSelectedIndex(index);
   }
}

此外,您可以更改BasicComboBoxRenderer中的项目颜色,如下所示:

if (disabled_items.contains(index)) {
     setBackground(list.getBackground());
     setForeground(UIManager.getColor("Label.disabledForeground"));
}

For further details

答案 1 :(得分:0)

这是我的解决方案(可能有用)

/**
 * A ComboBox with Enabling/disabling items support
 */
public class AvailableItemComboBox extends JComboBox {
    private static final long serialVersionUID = 1L;

    // previosly chosen item
    protected int old = -1;

    /**
     * Default constructor
     */
    public AvailableItemComboBox() {
        super();
        setRenderer(new EnabledJComboBoxRenderer());    
        initializeComboBox();
    }

    /**
     * Constructor with model
     * 
     * @param model
     */
    public AvailableItemComboBox(ComboBoxModel model) {
        super(model);
        setRenderer(new EnabledJComboBoxRenderer());
        initializeComboBox();
        System.out.println("selected index is " + getSelectedIndex());
    }

    /**
     * Constructor with data
     * 
     * @param model
     */
    public AvailableItemComboBox(Available[] items) {
        super(items);
        setRenderer(new EnabledJComboBoxRenderer());
        initializeComboBox();
        System.out.println("selected index is " + getSelectedIndex());
    }

    private void initializeComboBox() {
        setSelectedIndex(findFirstAvailableItem());

        addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {    
                System.out.println("Listener called>>>>>>>>>>>>>>>>>>");                
                if (old != getSelectedIndex()) {
                    if (!((Available) getSelectedItem()).isEnabled()) {
                        setSelectedIndex(old);
                    } else {
                        old = getSelectedIndex();
                    }
                }
            }       
        });
    }

    // Finds first selecteditem and sets it as previous state
    private int findFirstAvailableItem() {
        int result = -1;

        for (int index = 0; index < this.getItemCount(); index++) {
            if (((Available)this.getItemAt(index)).isEnabled()) {
                result = index;
                break;
            }
        }

        old = result;
        return result;
    }
}

public class EnabledJComboBoxRenderer extends BasicComboBoxRenderer {
private static final long serialVersionUID = 1L;

private Color disabledColor = Color.lightGray;

/**
 * Constructs a new renderer for a JComboBox which enables/disables items
 * based upon the parameter model.
 * 
 * @param enabled
 */
public EnabledJComboBoxRenderer(){
    super();        
}

/**
 * Sets the color to render disabled items.
 *  
 * @param disabledColor
 */
public void setDisabledColor(Color disabledColor){
    this.disabledColor = disabledColor;
}

/**
 * Custom implementation to color items as enabled or disabled.
 * 
 * @inheritDoc
 */
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

    boolean enabled = ((Available) value).isEnabled();

    if (!enabled) {
        if (isSelected) {
            c.setBackground(UIManager.getColor("ComboBox.background"));
        } else {
            c.setBackground(super.getBackground());
        }
        c.setForeground(disabledColor);
    } else {
        c.setBackground(super.getBackground());
        c.setForeground(super.getForeground());
    }

    return c;
}

}

public interface Available {
public boolean isEnabled();
public void setEnabled(boolean enable);

}