2017-05-14 35 views
0

我试图从数据填充两个组合框,但不知道如何将两个组合框之间划分这个数据。数据现在填充在两个组合框中。如何从文本文件填充在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); 
    } 
} 

当它从代码看到我在读从外部文本数据文件,然后尝试在两个不同的组合框中加载它。但是如何编写将门的值分为第一个组合和中间值分配到第二个组合的代码。 任何想法建议? 感谢

回答

1

与文件及其内容的问题开始,定义更舒适格式的属性文件...您可以使用JSON,XML,YAML,或者只是属性...

我会做与老同学Java属性的例子

文件:

盖茨=值,值2,值3

MIDS = customer1表,的customer2

然后读取作为属性,拆分到字符串数组,并与

public static void main(String[] args) { 
Properties prop = new Properties(); 
InputStream input = null; 

try { 
    input = new FileInputStream("./file2.txt"); 
    prop.load(input); 
    String[] gates = prop.getProperty("Gates").split(","); 
    String[] mids = prop.getProperty("Mids").split(","); 
    JFrame myJFrame = new JFrame(); 
    myJFrame.setTitle("Example"); 
    myJFrame.setSize(250, 250); 
    JPanel panel = new JPanel(); 

    JComboBox<String> myComboGates = new JComboBox<>(gates); 
    JComboBox<String> myComboMids = new JComboBox<>(mids); 
    panel.add(myComboGates); 
    panel.add(myComboMids); 

    myJFrame.add(panel); 
    myJFrame.setVisible(true); 

} catch (IOException ex) { 
    ex.printStackTrace(); 
} finally { 
    if (input != null) { 
    try { 
     input.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

} 

结果为2个连击用2种不同的相关信息的从支柱填充盒。文件:enter image description here

0

感谢答复,并很好的决定,但在没有我的情况,因为我在init方法组合框的初始化,它不能为空。 但我想用HashMap和它分裂,但不知什么原因,只需要最后一个键和值:

这是我的文本文件:

门:gate01

门:gate02

门:AWS

中秋节:SSS

中秋节:RRRR

这里是更新的代码,但问题出在哪里。为什么只用了最后一个值,每个组合框:

@SuppressWarnings("unchecked") 
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..."); 
    } 
    HashMap<String, String> map = new HashMap<String, String>(); 
    try { 
     String line; 
     try { 
      while ((line = input.readLine()) != null) { 
       String[] parts = line.split(":", 2); 

        String key = parts[0]; 
        String value = parts[1]; 
        map.put(key, value); 


      } 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } finally { 
     try { 
      input.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 

    comboBoxGate.removeAllItems(); 
    comboBoxMid.removeAllItems(); 

    for (String key : map.keySet()) { 
     if (key.startsWith("Gate")) { 
      comboBoxGate.addItem(map.get(key)); 
     } else { 
      comboBoxMid.addItem(map.get(key)); 
     } 
    } 
}