Java JCombobox更新问题

问题描述

我正在尝试根据其他JComboBox(城市)的值来更改JComboBox项目列表(城镇)。当我更改城市列表中的值时,它将更改城镇列表中的项目。但是有两个问题。

  1. 更新后的列表(城镇)显示了两倍的项目,但是单击它时,它显示了正确数量的项目,如第一个屏幕截图所示。
  2. 更新后的列表不允许我选择其中一项,只能选择第一项

a

这是我的代码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;   
public class Testing extends JFrame implements ActionListener {
    
    public static void main(String[] args )
    {
    new Testing();
    }
    JComboBox cb,cb1,cb2;
    JFrame f; 
    JLabel label1,label2;
    JButton b1;
    JTextField name,ID,birth;
    
    
 Testing(){    
        f=new JFrame("@R_407_4045@ion Example");  
        label1 = new JLabel("Please input your @R_407_4045@ion below");
        label1.setBounds(10,20,260,30);
      f.add(label1);
        String question[]={"Calculate my Age","When do I drive","When do I Vote"};
        String city[]={"Asimah","Ahmadi","Hawalli"};
       
               
         name= new JTextField("NAME");
        name.setBounds(10,50,264,25);
        f.add(name);
         ID= new JTextField("CIVIL ID");
        ID.setBounds(10,80,25);
        f.add(ID);
         birth= new JTextField("DATE OF BIRTH");
        birth.setBounds(10,110,25);
        f.add(birth);
        cb=new JComboBox(question);    
        cb.setBounds(50,150,180,20);    
        f.add(cb);
        b1= new JButton("Get");
        b1.setBounds(100,250,60,20);
        f.add(b1);
        cb1=new JComboBox(city);    
        cb1.setBounds(10,200,120,20);    
        f.add(cb1);
        cb2=new JComboBox();    
        cb2.setBounds(150,20);    
        f.add(cb2);
     
        f.setLayout(null);    
        f.setSize(300,400);    
        f.setVisible(true);
        cb.addActionListener(this);
        cb1.addActionListener(this);
        cb2.addActionListener(this);
}
 @Override
 public void actionPerformed(ActionEvent event)
 {
     if(cb1.getSelectedindex() == 0)
     {
         cb2.removeAllItems();
        
         cb2.addItem("Rawdhah");
         cb2.addItem("Abdahll");
     }
     else if(cb1.getSelectedindex() == 1)
     {
        
         cb2.removeAllItems();
         cb2.addItem("Siddiq");
         cb2.addItem("Aljabryha");
     }
     else
     {
         cb2.removeAllItems();
         cb2.addItem("Fintas");
         cb2.addItem("Abdahll");
     }
     
 }
}

解决方法

因此,基本上,removeAllItemsaddItem的组合使JComboBox生成ActionEvent,但仅对于添加的第一个新项目。

您应该隔离功能,使其仅基于事件的来源执行某些操作,例如...

@Override
public void actionPerformed(ActionEvent event) {
    if (event.getSource() == cb1) {
        if (cb1.getSelectedIndex() == 0) {
            cb2.removeAllItems();

            System.out.println(cb2.getItemCount());

            cb2.addItem("Rawdhah");
            cb2.addItem("Abdahll");
        } else if (cb1.getSelectedIndex() == 1) {

            cb2.removeAllItems();
            cb2.addItem("Siddiq");
            cb2.addItem("Aljabryha");
        } else {
            cb2.removeAllItems();
            cb2.addItem("Fintas");
            cb2.addItem("Abdahll");
        }
    }
}

您还可以使用actionCommand属性,但是以上是更简单,直接的解决方案