我試圖從數據填充兩個組合框,但不知道如何將兩個組合框之間劃分這個數據。數據現在填充在兩個組合框中。如何從文本文件填充在Java中兩個組合框?
這裏是我的數據文本文件:
[Gates]
value1
value2
value3
[Mids]
customer1
customer2
,這裏是我的Java內部代碼Swing GUI應用程序:
private void populateCombos() throws FileNotFoundException {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(frmGottApplication);
BufferedReader input=new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
if (result == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
textFieldLoadConfig.setText(selectedFile.getAbsolutePath());
lblConfRes.setText("Loaded " + selectedFile.getAbsolutePath().toString());
} else {
lblConfRes.setText("You didn't load...");
}
List<String> strings = new ArrayList<String>();
try {
String line = null;
try {
while ((line = input.readLine()) != null) {
strings.add(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
try {
input.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
String[] lineArrayGates = strings.toArray(new String[] {});
comboBoxGate.removeAllItems();
comboBoxMid.removeAllItems();
for (String str : lineArrayGates) {
comboBoxGate.addItem(str);
comboBoxMid.addItem(str);
}
}
當它從代碼看到我在讀從外部文本數據文件,然後嘗試在兩個不同的組合框中加載它。但是如何編寫將門的值分爲第一個組合和中間值分配到第二個組合的代碼。 任何想法建議? 感謝