JavaFX:从文件加载字符串并设置为Label

问题描述 投票:0回答:1

由于这个问题,我不仅失去了5个小时,而且几乎失去了理智。

我从Txt文件加载一个字符串,并希望将其设置为标签sourceURL。在我在System.out.println中检查它之前(“Firstline is:”+ brTest.readLine());它显示了String correctli。但随后UI弹出,标签为空。

看起来它无法加载它并需要刷新。你知道什么吗?

DashBoardController

    import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.DirectoryChooser;

public class DashboardController {

    @FXML
    private Label userFeedback;

    @FXML
    private Font x1;

    @FXML
    private Label sourceURL;

    @FXML
    private Label targetURL;

    @FXML
    public void initialize(){
        BufferedReader brTest;
        try {
            brTest = new BufferedReader(new FileReader(new File("settings/sourceurl.txt")));
            System.out.println("Firstline is : " + brTest.readLine());

            String temp = brTest.readLine();
            sourceURL.setText(brTest.readLine());
            sourceURL.setText(temp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @FXML
    void convert(ActionEvent event) {

    }

    @FXML
    void openFolder(ActionEvent event) {

    }

    @FXML
    void selectSource(ActionEvent event) throws FileNotFoundException {
        //DirectoryChooser initialisieren
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("Quellordner wählen");

        //URL speichern
        File selectedDirectory = directoryChooser.showDialog(null);
        sourceURL.setText(selectedDirectory.getAbsolutePath());

        //Ordner erstellen
        File settings = new File("settings");
        settings.mkdir();

        //Datei erstellen und URL abspeichern
        PrintWriter printWriter = new PrintWriter("settings/sourceurl.txt");
        printWriter.println(selectedDirectory.getAbsolutePath());
        printWriter.close();
    }

    @FXML
    void selectTarget(ActionEvent event) {
        sourceURL.setText("sdfkjasdöfl");
    }

    @FXML
    void showInfo(ActionEvent event) {

    }

    @FXML
    void showSupport(ActionEvent event) {

    }
}

仪表板

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Dashboard extends Application {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub

        //Pane laden
        Pane pane = (Pane) FXMLLoader.load(Dashboard.class.getResource("Dashboard.fxml"));

        //Stage einrichten
        stage.setScene(new Scene(pane));
        stage.setTitle("");
        stage.setResizable(false);
        stage.show();
    }

}
java javafx
1个回答
0
投票

如果要在加载期间设置TextField的文本,则必须使用lookup()函数(在JavaFX中)获取ID:

initialize(){
     scene = Pane.getScene();   
     TextField sourceURL= (TextField) scene .lookup("#sourceURL");
     sourceURL.setText("your text");
}
© www.soinside.com 2019 - 2024. All rights reserved.