将 HashMap 值分配给动态 jComboBoxes [英] assign HashMap values to dynamic jComboBoxes
问题描述
我正在将文本文件内容加载到 GUI 并使用此代码计算 HashMap
值:
I am loading text file contents to GUI and counting HashMap
values using this code:
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);
jButton1
的 ActionListener
借鉴自 这里.
现在我要分配AUTHOR
(HashMap
中的项目数是12,所以jButton1
将动态 12 jComboBoxes
) 值添加到动态创建的 jComboBoxes
.
Now I want to assign AUTHOR
(the number of items in HashMap
is 12, so jButton1
will add dynamic 12 jComboBoxes
) values to dynamically created 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 个值中分配 12 个值code>AUTHOR 字段并将它们显示在 jcb
上.
But this code read from input.txt all lines (70 lines), but I want to assign just that 12 values from AUTHOR
field and show them on jcb
.
我该如何解决?
推荐答案
您不必为了完成 GUI 的设置而再次重新阅读整个文本文件.我只会读取一次文本文件,然后使用 Map
对象以完成 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.
这篇关于将 HashMap 值分配给动态 jComboBoxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!