【发布时间】:2014-11-28 15:48:53
【问题描述】:
我需要读取一个配置文件并将数据(它由键值对组成)放入一些我必须在 JFrame 中动态创建的文本字段中。 之后我想修改 textFields 并再次将更改保存到文件中。
到目前为止我所拥有的:
FileConfiguration fileConfig = new PropertiesConfiguration(new File("xesfile.properties"));
Iterator<String> keys = fileConfig.getKeys();
while (keys.hasNext()) {
String singleKey = keys.next();
//for the case that a key has multiple values
if (fileConfig.getProperty(singleKey).getClass().equals(ArrayList.class)) {
ArrayList<String> blaArray = (ArrayList<String>) fileConfig.getProperty(singleKey);
for (int i = 0; i < blaArray.size(); i++) {
this.keyLabel = new JLabel(this.nameKeyLabel + this.count);
this.entityLabel = new JLabel(this.nameEntityLabel + this.count);
this.keyTextField = new JTextField();
this.keyTextField.setName(this.nameKeyTextField + this.count);
this.keyTextField.setText(singleKey);
this.entityTextField = new JTextField();
this.entityTextField.setName(this.nameEntityTextField + this.count);
this.entityTextField.setText(blaArray.get(i));
this.count++;
this.configFrame.add(this.keyLabel);
this.configFrame.add(this.keyTextField);
this.configFrame.add(this.entityLabel);
this.configFrame.add(this.entityTextField);
this.configFrame.revalidate();
this.configFrame.repaint();
this.configFrame.pack();
}
//for the case a key has a single value
} else {
this.keyLabel = new JLabel(this.nameKeyLabel + this.count);
this.entityLabel = new JLabel(this.nameEntityLabel + this.count);
this.keyTextField = new JTextField();
this.keyTextField.setName(this.nameKeyTextField + this.count);
this.keyTextField.setText(singleKey);
this.entityTextField = new JTextField();
this.entityTextField.setName(this.nameEntityTextField + this.count);
this.entityTextField.setText((String) fileConfig.getProperty(singleKey));
this.count++;
this.configFrame.add(this.keyLabel);
this.configFrame.add(this.keyTextField);
this.configFrame.add(this.entityLabel);
this.configFrame.add(this.entityTextField);
this.configFrame.revalidate();
this.configFrame.repaint();
this.configFrame.pack();
}
}
正如您所见,TextField 会在 while 循环的每一次传递中一次又一次地创建。但是我需要访问我在该示例的循环的第一遍中创建的文本字段。
想如果我会用getName() 访问TextFields,它们会被返回,但事实并非如此。
谁能帮帮我?
【问题讨论】: