Java的JComboBox更新问题。

huangapple go评论71阅读模式
英文:

Java JCombobox Update issue

问题

我正在尝试根据另一个JCombobox城市的值更改JCombobox项目列表城镇)。当我更改城市列表中的值时它会更改城镇列表中的项目但存在两个问题

1. 更新后的列表城镇显示的项目数量是原来的两倍但当点击它时它会显示正确数量的项目如第一张截图所示
2. 更新后的列表不允许我选择其中一个项目它只能选择第一个项目

以下是我的代码

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("信息示例");  
	    label1 = new JLabel("请在下面输入您的信息");
	    label1.setBounds(10, 20, 260, 30);
	  f.add(label1);
	    String question[]={"计算我的年龄","何时开车","何时投票"};
	    String city[]={"Asimah","Ahmadi","Hawalli"};
	   
	           
	     name= new JTextField("姓名");
	    name.setBounds(10,50,264,25);
	    f.add(name);
	     ID= new JTextField("身份证号");
	    ID.setBounds(10,80,264,25);
	    f.add(ID);
	     birth= new JTextField("出生日期");
	    birth.setBounds(10,110,264,25);
	    f.add(birth);
	    cb=new JComboBox(question);    
	    cb.setBounds(50, 150,180,20);    
	    f.add(cb);
	    b1= new JButton("获取");
	    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, 200,120,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");
	 }
	 
 }
}
英文:

I am trying to change JCombobox items list (towns) depending on the value of the other JCombobox (city). When I change the value in the city list, it changes the items in the towns list. But there are 2 issues.

  1. The updated list (towns) shows double of the items but when click on it then it shows the correct number of items as shown in the first screenshot.
  2. The updated list doesn't allow me to choose one of the item, it only select the first item

Java的JComboBox更新问题。

here is my code:

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("Information Example");  
label1 = new JLabel("Please input your information 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,264,25);
f.add(ID);
birth= new JTextField("DATE OF BIRTH");
birth.setBounds(10,110,264,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, 200,120,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");
}
}
}

答案1

得分: 1

基本上,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 属性,但以上是更简单、即时的解决方案。

英文:

So, basically, the combination of removeAllItems and addItem is causing the JComboBox to generate a ActionEvent, but only for the first new item added.

You should isolate your functionality to only perform certain actions based on the source of the event, for example...

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

You could also make use of the actionCommand property, but the above is the simpler, immediate solution

huangapple
  • 本文由 发表于 2020年9月15日 06:34:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63892620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定