Java 将HashMap值分配给动态JComboxes
我正在将文本文件内容加载到GUI,并使用以下代码计算Java 将HashMap值分配给动态JComboxes,java,swing,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 F
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);
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行),但我只想从AUTHOR
字段中分配12个值,并在jcb
上显示它们
如何解决此问题?您不必再次读取整个文本文件来完成GUI的设置。我只需读取一次文本文件,然后使用
Map sections=newhashmap()代码>对象以完成GUI的设置
这可能是您的流程:
1) 读取整个文件并返回HashMap部分
2) 通过向子面板添加子面板来设置jPanel2
(例如,基于作者数量)
3) 通过添加存储在HashMap
中的数据(例如映射的ArrayList的
),设置jcombox的
)
对于数字1),我只需要创建一个方法来读取文件并返回HashMap
读文件
示例(根据您的其他问题改编):
publicmap getSections()
{
Map sections=newhashmap();
字符串s=“”,lastKey=“”;
try(BufferedReader br=newbufferedreader(newfilereader(“input.txt”))
{
而((s=br.readLine())!=null)
{
字符串k=s.substring(0,10).trim();
字符串v=s.substring(10,s.length()-50).trim();
如果(k等于(“”)
k=最后一个键;
ArrayList authors=null;
if(第2(k)节)
{
作者=节。获取(k);
}
其他的
{
authors=newarraylist();
(k,作者);
}
//不要添加空字符串
如果(v.length()>0)
{
作者:添加(v);
}
lastKey=k;
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
回流段;
}
图形用户界面设置
注意:这段代码可以放在您现在设置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));
}
}
public void setupGUI()
{
//读取文件并获取地图
Map sections=getSections();
//找到作者
ArrayList authors=sections.get(“AUTHOR”);
//通过添加子面板来设置jPanel2
int num=authors.size();
jButton1.addActionListener(新的Clicker(num));
jButton1.doClick();
//通过添加地图中存储的数据来设置JComboBox
对于(int i=0;i
旁注:我不知道为什么要创建12个单独的子面板,每个子面板都有自己的JComboBox?也许你想考虑如何更好地布局GUI。只是考虑一下。在任何一种情况下,您都可以使用上述示例作为起点。对不起,您想再次做什么?请澄清,请发布你的或尽可能多的mcve。我仍然不清楚你的问题,你的代码和你的问题,但希望Michael的答案是你需要的。看来他以前帮助过你,所以比大多数人更了解你的设置。不过,如果你能在这个问题上投入更多精力,对未来的访问者来说会更好。还有一个旁注:永远不要有这样的代码:catch(IOException e){}
。这不仅仅是坏代码,它是彻头彻尾的危险代码。谢谢你的回复。我想在的情况下询问(int I=1;I > JCOMBOBOX 中得到所有13个<代码> HashMap <代码>值,但最后增加了第十四个空<代码> JCOMBOBOX 。我做错了什么?P.S.关于GUI布局:谢谢,我会尝试更好的方式。研究所列表的大小是什么?ode.for循环并没有添加任何jComboBox。它只是在其中设置选定的项。查看您创建它们的代码,看看您实际创建了多少个以及为什么。我发现ArrayList authors
包含空行,这就是为什么添加空jcb
,我想问一下如何删除authors字段?
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));
}
}