JComboBox
의 항목을 비활성화 할 방법이 없습니다. 당신은 여기 위치에서 제거하는 방법입니다 수 있습니다 : -
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);
가 JComboBox
이 list
의 항목을 만들 것입니다. 클릭 이벤트에 사용되는 내부 클래스 ActionListener
내부에서 c1이 호출되기 때문에 final
이 사용됩니다. index=c1.getSelectedIndex();
은 c1
에 선택한 항목의 index location
을 가져옵니다. c2.removeItemAt(index);
은 index
위치에있는 항목을 제거합니다. c1
및 c2
에는 모두 비슷한 항목이 있으므로 index
항목의 위치는 같습니다. 당신은 다음 어느 시점에서 C2에서 항목을 다시 삽입 제거 할 항목의 인덱스 위치와
index=c1.getSelectedIndex();
item=c2.getItemAtIndex(index);
c2.removeItemAt(index);
은 다음 항목을 복원하여 제거되어야하는 항목의 이름을 저장하려면 그것은 외부에서 사용할 수있는 경우 외부 ActionListener
를 선언해야한다 index
및 item
-
c2.insertItemAt(item,index);
주에게를 사용하여.
이 답변으로 문제가 해결되면 눈금을 클릭하여 해결 된 문제를 표시하고 대답을 유용하게 표시하십시오. –
좋아, 그걸 시도해 보겠습니다. –
@EduardoSoriano 만약 당신이 답안을 좋아한다면 그것을 유용하고 문제가 해결 된 문제를 풀면 해결할 수 있습니다. –