英文:
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.
- 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.
- The updated list doesn't allow me to choose one of the item, it only select the first item
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
基本上,removeAllItems
和 addItem
的组合会导致 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论