简体   繁体   中英

How to disable a Jcombobox item from a listener?

I have a problem with my project, since my goal is to let the user manually fill 6 fields with items in an array; I thought of 6 JComboBox es with the same items, when you select an item in one box, it becomes disabled in the rest. I'm starting, and although I've searched I only found the way to do it inside its constructor.

cb1.addActionListener(new ActionListener(){ 

@Override
public void actionPerformed(ActionEvent e) {
     if(cb1.getSelectedIndex()==1) {
         // this is as far as I go, but disables the entire jcombobox
         cb2.setEnabled(false);

         // this is more like I want, but it doesn't work.
         cb2.setSelectedIndex(1).setEnabled(false);                            
 }}});

If anyone knows a more efficient way to make possible to the user assign manually array items to many fields I would welcome it.

There is no way for you to disable the item of JComboBox . You can just remove it from the location here is how:-

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Combobox extends JFrame{
Combobox(){
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String[] list={"car","bus","bike"};
    final JComboBox c1=new JComboBox(list);
    final JComboBox c2=new JComboBox(list);
    Container c=this.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(c1);
    c.add(c2);
    c1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            int index=c1.getSelectedIndex();
            c2.removeItemAt(index);
            }
    });
    this.pack();
}
    public static void main(String[] args) {
        new Combobox();
    }
}

final JComboBox c1=new JComboBox(list); will make a JComboBox have items of list . final is used since c1 is called inside inner class ActionListener which is used for clicking events. index=c1.getSelectedIndex(); will get the index location of the selected item in c1 . c2.removeItemAt(index); will remove the item which is at index location of c2. Since c1 and c2 both contains similar items so index position of items is same. If you want to re-insert the item in c2 at some point then save the index location of the item to be removed and the name of the item that is to be removed using

index=c1.getSelectedIndex();
item=c2.getItemAtIndex(index);
c2.removeItemAt(index);

then restore the items using

c2.insertItemAt(item,index);

Note - index and item should be declare outside ActionListener if there are to be used outside it.

Try to enable ComboItem. Function setEnabled is used to for object, at your case cb2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM