将HashMap值分配给动态jComboBoxes

时间:2016-05-22 14:14:23

标签: java swing

我正在使用以下代码将文本文件内容加载到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);

ActionListener的{​​{1}}来自here

现在我想分配jButton1AUTHOR中的项目数 12 ,因此HashMap将添加动态 12 jButton1)动态创建jComboBoxes的值。

我试过这段代码:

jComboBoxes

但是此代码从 input.txt 读取所有行( 70行),但我想从{仅分配 12 值{1}}字段并在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++; } 上显示。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

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

这可能是您的流程:

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

2)通过向jPanel2添加SubPanels来设置JComboBox's(例如,根据作者的数量)。

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

对于数字1),我只想创建一个读取文件并返回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; } 的方法。

阅读文件

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

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

GUI设置

注意:此代码可以放在您现在设置GUI的任何位置,我只是将所有内容放在下面的方法中作为示例。

//Listen on checkbox change event
$('input[type="checkbox"]').on('change',function() {
    var $this = $(this), //current changed checbox
        $work = $('.work'), //selector to add new behavior
        $visible = $('input[type="checkbox"]:visible'); //whole visible checkboxes

    //You can change it if you want to lookup check state from DOM
    //$(this).is(':checked')
    //or by attribute
    if ($this.prop('checked')) {
        $this.fadeOut('slow'); //hide it with animation if checked
    }

    //$visible only shows last visible checkboxes before it hides,
    //so it ends at 1 not 0
    if ($visible.length <= 1)
        $work.addClass('new__block'); //add class to $work
});

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