设计:2个jcomboboxes,box 2列表取决于框1中的选择,XML中的数据

时间:2012-10-16 16:19:34

标签: java xml swing jcombobox

我为尝试为这个问题设计解决方案而感到不知所措 - 这是缺乏经验的副产品。

我的目标是读取XML输入文件,存储XML中的信息,并使用XML中的数据填充两个组合框。第二个组合框的内容将根据第一个组合框中的选择进行更改。

鉴于此XML结构:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Category xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Node>
    <ID>Unique string</ID>
    <Name>Unique string</Name>
    <Code>Generic string<Code>
    <Kind>Generic string</Kind>
    <Frame>Generic string</Frame>
            ...
</Node>
    ...
</Category>

第一个组合框: 必须仅包含在“种类”部分中找到的唯一值。

第二个组合框: 包含来自每个节点的所有名称条目,其种类等于在第一个组合框中选择的种类。

关于XML源: 它是外部维护和生成的。 ID部分中的值始终是唯一的。 “名称”部分中的值始终是唯一的。 架构(假设)永远不会改变。 未来,“种类”部分中可能会出现新的唯一值。

我建议的解决方案: 创建一个类XMLNode以表示XML源中的节点。 XMLNode类的成员对应于每个Node中的标记。 遍历所有节点并为每个节点创建一个XMLNode。 循环通过节点时:   在哈希映射中添加XMLNode对象,其中Keys = XMLNode.ID,vals = XMLNode。   创建一个独特的种类数组。

从Kind条目数组中填充组合框1。 从每个的名称数据中填充组合框2。

这是一种合适的方法,还是我忽略了更好/更容易/更优雅的解决方案? 如果我走在正确的轨道上,我建议的解决方案是否有任何明显的缺陷?

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

我最终得到了一个N​​odeImporter类和一个Node类。 Node表示节点,NodeImporter解析XML。在NodeImporter中,XML源被解析并表示为HashMaps的HashMap。外部HashMap(Key,Value)是(Kind,(HashMap(Key,Value))。内部HashMap(Key,Value)是(UinqueID,Node),用于(Kind,(UniqueID,Node))的最终结果。我将最终结果称为“filteredMap”。所有NodeImporter的字段和方法都是私有的,除了构造函数和filterMap的getter。 需要数据来构建组合框的类从NodeImporter的实例获取filteredMap。然后它可以获取外部HashMap的键来构建第一个ComboBoxModel。它可以很容易地将内部HashMap用作第二个组合框的HashMap。

设置的伪代码:

使用类成员构建的NodeImporter类:

arrayList(String) uniqueKinds = null  
arrayList(Node) allNodes  = null  
HashMap(String, HashMap(String,Node))) filteredNodes = null  

类NodeImporter构造函数:

open XML  
while xml source has next  
  {  
  add next node to allNodes, key = node.uniqueId, Val = node  
  if next node.kind not in uniqueKinds, add node.kind to uniqueKinds  
  }



ClassNodeImporter method makeFilteredeMap:  

    private boolean makeFilteredeMap() {
        if (uniqueKinds.isEmpty()) {
            return false;
        } else {
            for (String k : uniqueKinds) {
                HashMap<String, Node> aMap = new HashMap<String, Node>();
                for (Node n : allNodes) {
                    if (n.getKind().equals(k)) {
                        aMap.put(n.getCode(), n);
                    }
                }
                filteredNodes.put(k, aMap);
            }
            return true;
        }
    }
相关问题