且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 HashMap 值分配给动态 jComboBoxes

更新时间:2023-02-11 18:12:08

您不必为了完成 GUI 的设置而再次重新阅读整个文本文件.我只会读取一次文本文件,然后使用 Map>section = new HashMap(); 对象以完成 GUI 的设置.

You shouldn't have to re-read the entire text file again in order to complete the setup of the GUI. I would just read the text file once, then use the Map<String, ArrayList<String>> sections = new HashMap<>(); object to complete the setup of the GUI.

这可能是适合您的过程:

This could be the process for you:

1) 读取整个文件并返回 HashMap 部分.

1) Read the entire file and return the sections HashMap.

2) 通过添加 SubPanels 来设置 jPanel2(例如基于作者的数量).

2) Setup the jPanel2 by adding the SubPanels to it (e.g. based on the number of Authors).

3) 通过添加存储在 HashMap 中的数据(例如映射的 ArrayList 的)来设置 JComboBox 的.

3) Setup the JComboBox's by adding the data stored in the HashMap (e.g. the mapped ArrayList's).

对于数字 1),我将创建一个读取文件并返回 HashMap 的方法.

For number 1), I would just create a method that reads the file and returns the HashMap.

示例(改编自您的其他问题此处):

Example (Adapted from your other question here):

public Map<String, ArrayList<String>> getSections ()
{
    Map<String, ArrayList<String>> sections = new HashMap<>();
    String s = "", lastKey = "";
    try (BufferedReader br = new BufferedReader(new FileReader("input.txt")))
    {
        while ((s = br.readLine()) != null)
        {
            String k = s.substring(0, 10).trim();
            String v = s.substring(10, s.length() - 50).trim();
            if (k.equals(""))
                k = lastKey;

            ArrayList<String> authors = null;
            if (sections.containsKey(k))
            {
                authors = sections.get(k);
            }
            else
            {
                authors = new ArrayList<String>();
                sections.put(k, authors);
            }

            // don't add empty strings
            if (v.length() > 0)
            {
                authors.add(v);
            }
            lastKey = k;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return sections;
}

界面设置

注意:这段代码可以放在你现在设置 GUI 的任何地方,我只是把所有的都放在下面的方法中作为例子.

GUI Setup

Note: This code can be put wherever you are setting up the GUI now, I'm just placing all in the method below for an example.

public void setupGUI ()
{
    // read the file and get the map
    Map<String, ArrayList<String>> sections = getSections();

    // get the authors
    ArrayList<String> authors = sections.get("AUTHOR");

    // Setup the jPanel2 by adding the SubPanels
    int num = authors.size();
    jButton1.addActionListener(new Clicker(num));
    jButton1.doClick();

    // Setup the JComboBox's by adding the data stored in the map
    for (int i = 0; i < authors.size(); i++)
    {
        int index = i;
        // not sure if getComponent() is zero or 1-baed so adjust the index accordingly.
        SubPanel panel = (SubPanel) jPanel2.getComponent(index);

        // Not sure if you already have the JComboBox in the SubPanel
        // If not, you can add them here.

        JComboBox jcb = panel.getJcb();
        jcb.setSelectedItem(authors.get(i));
    }
}

旁注:我不确定您为什么要创建 12 个独立的子面板,每个子面板都有自己的 JComboBox?也许您想考虑如何更好地布局 GUI.只是一个考虑.无论哪种情况,您都可以使用以上示例作为起点.

Side Note: I'm not sure why you are creating 12 separate SubPanel's, each with its own JComboBox? Maybe you want to consider how you can better layout the GUI. Just a consideration. In either case, you can use the above examples are a starting point.