【问题标题】:assign HashMap values to dynamic jComboBoxes将 HashMap 值分配给动态 jComboBoxes
【发布时间】:2016-05-22 14:14:22
【问题描述】:

我正在将文本文件内容加载到 GUI 并使用此代码计算 HashMap 值:

Map<String, ArrayList<String>> sections = new HashMap<>();
Map<String, String> sections2 = 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);
        }
        authors.add(v);
        lastKey = k;
    }
} catch (IOException e) {
}

// to get the number of authors
int numOfAuthors = sections.get("AUTHOR").size();
// to count HashMap value
jButton1.addActionListener(new Clicker(numOfAuthors));
jButton1.doClick();

// convert the list to a string to load it in a GUI
String authors = "";
for (String a : sections.get("AUTHOR"))
{
    authors += a;
}
   jcb1.setSelectedItem(authors);

jButton1ActionListener 是从here 借来的。

现在我要分配AUTHORHashMap 中的项目数为12,所以jButton1 将添加动态12jComboBoxes)值动态创建jComboBoxes

我试过这段代码:

BufferedReader br = new BufferedReader(new FileReader ("input.txt"));
String str=null;
int i = 0;
while( (str = br.readLine()) !=null ) {
    String v = str.substring(12, str.length() - 61).trim();

    if(i == 0) {              
        jcb1.setSelectedItem(v);

    } else {
        SubPanel panel = (SubPanel) jPanel2.getComponent(i - 1);
        JComboBox jcb = panel.getJcb();
        jcb.setSelectedItem(v);
    }

    i++;
}

但这段代码从 input.txt 读取所有行(70 行),但我只想从 @ 分配 12 值987654333@ 字段并在jcb 上显示。

我该如何解决?

【问题讨论】:

  • 抱歉,您还想做什么?请澄清,请发布您的minimal reproducible example 或尽可能多的 mcve。
  • 对于您的问题、您的代码和您的问题,我仍然非常不清楚,但希望迈克尔的回答是您所需要的。看来他以前帮助过你,所以比大多数人更了解你的设置。不过,如果您可以在这个问题上投入更多精力,那么对未来的访问者来说会更好。另附注:从不有此代码:catch (IOException e) { }。这不仅仅是糟糕的代码,它是彻头彻尾的危险代码。

标签: java swing


【解决方案1】:

您不必为了完成 GUI 的设置而再次重新读取整个文本文件。我只需读取文本文件一次,然后使用 Map&lt;String, ArrayList&lt;String&gt;&gt; sections = new HashMap&lt;&gt;(); 对象完成 GUI 的设置。

这可能是适合您的过程:

1) 读取整个文件并返回sections HashMap。

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

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

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

读取文件

示例(改编自您的另一个问题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的任何地方,我只是把所有的放在下面的方法中作为例子。

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。只是一个考虑。无论哪种情况,您都可以使用以上示例作为起点。

【讨论】:

  • 感谢您的回复。我想问一下for (int i = 1; i &lt; institute.size(); i++) { int index = i; SubPanel panel = (SubPanel) jPanel2.getComponent(index-1); jcb1.setSelectedItem(institute.get(0)); JComboBox jcb = panel.getJcb(); jcb.setSelectedItem(institute.get(i)); 的情况,我在动态jComboBoxes 中获得了所有 13 HashMap 值,但最后它添加了额外的 14thjComboBox。我做错了什么?附言关于 GUI 布局:谢谢,我会考虑更好的方法。
  • 机构列表的大小是多少?
  • 研究所大小为13
  • 它必须在您的代码中的其他位置。那个for循环没有添加任何jComboBox。它只是在其中设置选定的项目。查看你创建它们的代码,看看你实际创建了多少以及为什么。
  • 我发现ArrayList authors包含空行,所以添加空jcb,我想问我如何删除authors字段中的空行?
猜你喜欢
  • 2011-03-12
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 2020-07-27
  • 2021-11-15
  • 2020-11-05
  • 2018-12-08
相关资源
最近更新 更多