Java JCombobox更改另一个JCombobox
我正在尝试合并两个JComboxes。1组合框用于显示费用类别。第二个组合框是从文本文件中读取文件以显示产品类型。如果我更改第一个组合框,我希望第二个组合框将根据用户在第一个组合框中选择的内容进行更改 有没有可能我仍然可以从文本文件加载另一个组合框?子项将不是数组,而是和以前一样,位于cboperson代码的底部 编辑代码:Java JCombobox更改另一个JCombobox,java,jcombobox,Java,Jcombobox,我正在尝试合并两个JComboxes。1组合框用于显示费用类别。第二个组合框是从文本文件中读取文件以显示产品类型。如果我更改第一个组合框,我希望第二个组合框将根据用户在第一个组合框中选择的内容进行更改 有没有可能我仍然可以从文本文件加载另一个组合框?子项将不是数组,而是和以前一样,位于cboperson代码的底部 编辑代码: private JComboBox cboCategory; private JComboBox cboPerson; private JComboBox cboItem;
private JComboBox cboCategory;
private JComboBox cboPerson;
private JComboBox cboItem;
public String itemChange = "groceries.txt";
public ExpenditureTracker() {......
String[] items = {"Select Item", "Groceries", "Bills", "Travelling", "Leasure", "Other"};
mainComboBox = new JComboBox(items);
mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
//prevent action events from being fired when the up/down arrow keys are used
//mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
mainComboBox.setBounds(113, 138, 85, 20);
importPanel.add(mainComboBox);
subComboBox = new JComboBox();// Create sub combo box with multiple models
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
subComboBox.addItemListener(this);
subComboBox.setBounds(113, 188, 85, 20);
importPanel.add(subComboBox);
String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
subItems.put(items[1], subItems1);
String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
subItems.put(items[2], subItems2);
String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[3], subItems3);
String[] subItems4 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[4], subItems3);
String[] subItems5 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[5], subItems3);
loadDataTocboPerson();
}
private void loadDataToCboPerson() {
Scanner fileReader = new Scanner(getClass().getResourceAsStream(
itemChange));
try {
DefaultComboBoxModel model = new DefaultComboBoxModel();
while (fileReader.hasNextLine()) {
model.addElement(fileReader.nextLine());
}
cboItem.setModel(model);
} finally {
fileReader.close();
}
}
首先,不要将字符串与==进行比较,而是使用equals或equalsIgnoreCase方法。e、 g 更改此项:
if (item == "Groceries") {
为此:
if ("Groceries".equalsIgnoreCase(item.toString())) {
您需要对项调用toString(),以确保将字符串与字符串进行比较。在执行任何操作之前,您还需要确保该项不为null。例如
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener {
private static final long serialVersionUID = 1L;
private JComboBox mainComboBox;
private JComboBox subComboBox;
private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();
public ComboBoxTwo() {
String[] items = {"Select Item", "Color", "Shape", "Fruit"};
mainComboBox = new JComboBox(items);
mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
//prevent action events from being fired when the up/down arrow keys are used
//mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
getContentPane().add(mainComboBox, BorderLayout.WEST);
subComboBox = new JComboBox();// Create sub combo box with multiple models
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
subComboBox.addItemListener(this);
getContentPane().add(subComboBox, BorderLayout.EAST);
String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
subItems.put(items[1], subItems1);
String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
subItems.put(items[2], subItems2);
String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[3], subItems3);
// mainComboBox.setSelectedIndex(1);
}
@Override
public void actionPerformed(ActionEvent e) {
String item = (String) mainComboBox.getSelectedItem();
Object o = subItems.get(item);
if (o == null) {
subComboBox.setModel(new DefaultComboBoxModel());
} else {
subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
}
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == mainComboBox) {
if (mainComboBox.getSelectedIndex() != 0) {
FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this,
mainComboBox.getSelectedItem().toString(), "Please wait, Searching for ..... ");
}
}
}
}
private class FirstDialog extends JDialog {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent, String winTitle, String msgString) {
super(parent, winTitle);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JLabel myLabel = new JLabel(msgString);
JButton bNext = new JButton("Stop Processes");
add(myLabel, BorderLayout.CENTER);
add(bNext, BorderLayout.SOUTH);
bNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
t.setRepeats(false);
t.start();
setLocationRelativeTo(parent);
setSize(new Dimension(400, 100));
setVisible(true);
}
}
public static void main(String[] args) {
JFrame frame = new ComboBoxTwo();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
公共类ComboBoxTwo扩展JFrame实现ActionListener、ItemListener{
私有静态最终长serialVersionUID=1L;
专用JComboBox主组合框;
专用JComboBox子omboBox;
私有哈希表子项=新哈希表();
公营机构{
字符串[]项={“选择项”、“颜色”、“形状”、“水果”};
mainComboBox=新的JComboBox(项目);
mainComboBox.addActionListener(这个);
mainComboBox.addItemListener(此);
//使用向上/向下箭头键时防止触发操作事件
//mainComboBox.putClientProperty(“JComboBox.isTableCellEditor”,Boolean.TRUE);
getContentPane().add(mainComboBox,BorderLayout.WEST);
subComboBox=new JComboBox();//创建具有多个模型的子组合框
subComboBox.setPrototypeDisplayValue(“xxxxxxxxx”);//JDK1.4
subComboBox.addItemListener(此);
getContentPane().add(subComboBox,BorderLayout.EAST);
字符串[]子项1={“选择颜色”、“红色”、“蓝色”、“绿色”};
子项目。投入(项目[1],子项目1);
字符串[]子项2={“选择形状”、“圆”、“正方形”、“三角形”};
子项目。投入(项目[2],子项目2);
String[]subItems3={“选择水果”、“苹果”、“橘子”、“香蕉”};
子项目。投入(项目[3],子项目3);
//主组合框。设置所选索引(1);
}
@凌驾
已执行的公共无效操作(操作事件e){
String item=(String)mainComboBox.getSelectedItem();
对象o=子项。获取(项);
如果(o==null){
setModel(新的DefaultComboxModel());
}否则{
setModel(新的DefaultComboxModel((字符串[])o));
}
}
@凌驾
公共无效itemStateChanged(ItemEvent e){
如果(如getStateChange()==ItemEvent.SELECTED){
如果(例如getSource()==mainComboBox){
如果(mainComboBox.getSelectedIndex()!=0){
FirstDialog FirstDialog=新建FirstDialog(ComboBoxTwo.this,
mainComboBox.getSelectedItem().toString(),“请稍候,正在搜索…”;
}
}
}
}
私有类FirstDialog扩展JDialog{
私有静态最终长serialVersionUID=1L;
第一个对话框(最终帧父级、字符串winTitle、字符串msgString){
超级(家长,winTitle);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JLabel myLabel=新的JLabel(msgString);
JButton bNext=新JButton(“停止进程”);
添加(myLabel,BorderLayout.CENTER);
添加(bNext,BorderLayout.SOUTH);
bNext.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
setVisible(假);
}
});
javax.swing.Timer t=newjavax.swing.Timer(1000,newActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
setVisible(假);
}
});
t、 设置重复(假);
t、 start();
setLocationRelativeTo(父级);
设置尺寸(新尺寸(400100));
setVisible(真);
}
}
公共静态void main(字符串[]args){
JFrame frame=new ComboBoxTwo();
frame.setDefaultCloseOperation(关闭时退出);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
}
或者,如果数组中的项太多,则可以将其包含在数组中。是否调用loadDataToCboItem()?您应该从displaySelectedItem()方法中的yout actionPerformed方法调用它。请进一步编辑您的答案。代码格式不好,不清楚方法属于哪个组合框。您好,这很完美。字符串子项是否有可能像我的其他组合框一样从文本文件中提取???这很完美。我是否有可能像以前一样从文本文件中加载它们?私有void loadDataToCboItem(){Scanner fileReader=new Scanner(getClass().getResourceAsStream(itemChange));尝试{DefaultComboxModel=new DefaultComboxModel();while(fileReader.hasNextLine()){model.addElement(fileReader.nextLine());}subComboBox.setModel(model);}最后{fileReader.close();}}只需将其更改为子项并在其中添加文本文件????非常感谢。@Petr:请编辑您的问题以包含新的代码示例。那看起来像狗的早餐!
if(jComboBox1.getSelectedItem() == "First Choice"){
jComboBox2.removeAllItems();
jComboBox2.addItem("First Choice Item 1");
}
if(jComboBox1.getSelectedItem() == "Another Choice"){
jComboBox2.removeAllItems();
jComboBox2.addItem("Another Choice Item 1");
}