Wie kann ich das speichern / laden von Werten und Einstellungen in einer Datei / aus Datei in JavaFX?

Wie kann ich Werte speichern (von Textfelder, Texteingabefelder, radiobuttons, etc.) und Einstellungen (position auf der Bühne, Vollbild oder normal-Größe, etc.) in eine Datei und laden Sie von dieser Datei ? Ich habe eine Lösung, aber diese Lösung scheint nicht die beste Lösung zu sein, denn es ist sehr viel "code" (wenn ich viele Komponenten).

Ich hatte einen Blick auf dies:
Lesen Properties Datei in Java

http://www.mkyong.com/java/java-properties-file-examples/

Aber diese Beispiele sind sehr einfach:

prop.setProperty("database", "localhost");
prop.setProperty("dbuser", "mkyong");
prop.setProperty("dbpassword", "password");

System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));

Aber 'System.out.println (prop.getProperty("value"));' ist nicht wirklich "laden" für mich.
Es wird nicht gezeigt, wie ich es in Komponenten wie textfiels, radiobuttons, Checkboxen und so weiter.

In meinem code-snippet, das ich mit anderen Komponenten (textfield, datepicker, rb, cb usw).
Hier ist mein code-snippet:

Sparen:

public void speichern (ActionEvent event) throws IOException 
    { 
        if (filename == null) 
            speichernUnter (event); 
        else 
        { 
            Properties prop = new Properties ( ); 
            FileOutputStream fos = null; 
            try 
            { 

            prop.setProperty ("Name", textField.getText ( ) ); //Save a textfield 
            if (radioButton.isSelected ( ) ) 
                prop.setProperty ("RadioButtonState", "yes"); //Save rb state 
            if (checkBox.isSelected ( ) ) 
            { 
                prop.setProperty ("CheckBoxState", "yes"); //Save cb state 
            } else 
                prop.setProperty ("CheckBoxState", "no"); //or not 

            prop.setProperty ("Date", datePicker.getValue ( ).toString ( ) ); //Save date
            prop.setProperty ("Text", textArea.getText ( ) ); //Save long text 


            fos = new FileOutputStream (filename); 


            //Save settings in file  
            prop.storeToXML (new FileOutputStream (filename), "Values and settings", "UTF-8"); 
        } catch (IOException ex) 
        { 
            ex.printStackTrace ( ); 
        } finally 
        { 
            if (fos != null) 
                fos.close ( ); 
        } }
    } 
    public void speichernUnter (ActionEvent event) 
    { 
        init ( ); 
        FileChooser fileChooser = new FileChooser ( ); 

        //Set extension filter 
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter ("PROPERTIES Dateien (*.properties)", "*.properties"); 
        fileChooser.getExtensionFilters ( ).add (extFilter); 

        //Show save file dialog 
        File file = fileChooser.showSaveDialog (stage); 
        if (file != null) 
        { 
            try 
            { 
                FileWriter fileWriter = new FileWriter (file); 
                fileWriter.close ( ); 
                filename = file.toString ( ); 
                speichern (event); 
            } catch (IOException ex) 
            { 
                Logger.getLogger (Kontroller.class.getName ( ) ).log (Level.SEVERE, null, ex); 
            } 
        } 
    }

Laden:

public void laden (ActionEvent event) throws IOException 
    { 
        if (filename == null) 
            ladenVon (event); 
        else 
        { 
            Properties prop = new Properties ( ); 
            FileInputStream fis = null; 
            try 
            { 

                fis = new FileInputStream (filename); 

                //Load Properties from saved XML file 
                prop.loadFromXML (fis); 

                textField.setText (prop.getProperty ("Name") ); //load the text 

                if (prop.getProperty ("RadioButtonState").equals ("yes") ) //load rb state 
                { 
                    radioButton.setSelected ( ); 
                } 

                    if (checkBox.isSelected ( ) ) //Load cb state 
                        checkBox.setSelected (false); 
                    checkBox.setSelected (true); 
                } 

                datePicker.setValue (LocalDate.parse (prop.getProperty ("Date") ) ); //load date 

                textArea.setText (prop.getProperty ("Text") ); //Load long text 
            } catch (IOException ex) 
            { 
                ex.printStackTrace ( ); 
            } finally 
            { 
                if (fis != null) 
                    fis.close ( ); 
            } 
        } 
    } 
    public void ladenVon (ActionEvent event) throws IOException 
    { 
        init ( ); 
        FileChooser fileChooser = new FileChooser ( ); 


        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter ("PROPERTIES Dateien (*.properties)", "*.properties"); 
        fileChooser.getExtensionFilters ( ).add (extFilter); 

        //Show load file dialog 
        File file = fileChooser.showOpenDialog (stage); 
        if (file != null) 
        { 
            try 
            { 
                FileReader fileReader = new FileReader (file); 
                fileReader.close ( ); 
                filename = file.toString ( ); 
                laden (event); 
            } catch (IOException ex) 
            { 
                Logger.getLogger (Kontroller.class.getName ( ) ).log (Level.SEVERE, null, ex); 
            } 
        } 
    }

Tun, ich habe wirklich zu tun " prop.setProperty ("Name", textField.getText ( ) ); ' für speichern und dieses 'textField.setText (prop.getProperty ("Name") ); //load the text ' für das laden für alle Komponenten?

Ist meine Lösung die "richtige"?
Es funktioniert alles wie ich es möchte.

Aber gibt es eine bessere und schnellere Lösung?

InformationsquelleAutor user3487276 | 2014-10-03
Schreibe einen Kommentar