【问题标题】:JCombobox change another JComboboxJCombobox 改变另一个 JCombobox
【发布时间】:2011-06-05 21:31:59
【问题描述】:

我正在尝试合并 2 个 jcombobox。 1 个组合框用于显示费用类别。第二个组合框正在从文本文件中读取文件以显示产品类型。如果我更改第一个组合框,我希望第二个组合框会根据用户在第一个中选择的内容而改变。

有没有机会我仍然可以从文本文件中加载其他组合框?该子项不会是数组,而是与之前相同,因为它位于 cboperson 的代码底部。

编辑代码:

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();
    }
}

【问题讨论】:

  • 你在调用 loadDataToCboItem() 吗?您应该在 displaySelectedItem() 方法中从您的 actionPerformed 方法中调用它。
  • 请再编辑您的答案。代码格式不正确,不清楚这些方法属于哪个组合框。

标签: java jcombobox


【解决方案1】:

例如

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

【讨论】:

  • 您好,这是完美的,有没有机会可以从文本文件中提取字符串子项作为我的其他组合框???
  • 这太完美了有没有机会我可以像以前一样从文本文件中加载它们? private void loadDataToCboItem() { Scanner fileReader = new Scanner(getClass().getResourceAsStream(itemChange));尝试 { DefaultComboBoxModel 模型 = 新 DefaultComboBoxModel(); while (fileReader.hasNextLine()) { model.addElement(fileReader.nextLine()); } subComboBox.setModel(model); } 最后 { fileReader.close(); } } 只需将其更改为子项并在那里添加文本文件 ????非常感谢。
  • @Petr:请编辑您的问题以包含新的代码示例。这看起来像狗的早餐!
【解决方案2】:

一方面,不要将字符串与 == 进行比较,而是使用 equals 或 equalsIgnoreCase 方法。例如,

改变这个:

if (item == "Groceries") {

对此:

if ("Groceries".equalsIgnoreCase(item.toString())) {

您需要在 item 上调用 toString() 以确保将 String 与 String 进行比较。在执行任何操作之前,您还需要确保该项目不为空。

【讨论】:

    【解决方案3】:
     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");
     }
    

    或者你可以在数组中包含太多的项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多