Установка детей из данных текстового файла JAVAFX - PullRequest
0 голосов
/ 26 октября 2019

Я пытаюсь прочитать данные .getChildren () из текстового файла, а затем установить эти данные для дочерних элементов панели. Сначала я назначаю linePaneContents = mainPassed.linePane.getChildren ();, затем запишите linePaneContents в текстовый файл.

 linePaneContents=mainPassed.linePane.getChildren();//////////
        public void writeFile() throws IOException {
    try {
        FileWriter fw = new FileWriter("map.txt");
        PrintWriter pw=new PrintWriter(fw);

        pw.println(linePaneContents);//////////

        pw.close();
    }
    catch (IOException e){
        System.out.println("error");
    }
}

Однако я не могу найти способ прочитать данные обратно в дочерние элементы панели.

Я пробовал следующее:

        public void readFile() {
    int lineCount=0;
    try {
        FileReader fr = new FileReader("map.txt");
        BufferedReader br = new BufferedReader(fr);

        String str;
        while ((str = br.readLine()) != null) {
            //System.out.println(str + "\n");
            if(str.isEmpty()){

            }
            else{
                if(lineCount==0){
                    ObservableList<Node> linePaneContents;
                    linePaneContents.add(str);
                    mainPassed.linePane.getChildren()=linePaneContents; //error here


                }
                System.out.println("str"+str);}
        }
        br.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
...