5

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

2 回答 2

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

欲知详情

于 2013-02-13T11:35:40.073 回答
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);

}

于 2014-04-03T13:03:32.903 回答