2017-05-14 108 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)); 
     } 
    } 
}