调用setText()时JavaFX文本不会更改

时间:2019-05-14 23:06:45

标签: java javafx

我正在尝试为网络爬虫创建GUI。当他们按下搜索按钮时,我希望出现一个显示“正在搜索...”的文本

public class Main extends Application {

    private CrawlerTest c;
    private Scene scene1, scene2;
    private String urls;
    private Text urlsText, searchText;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        this.c = new CrawlerTest();
        primaryStage.setTitle("WebCrawler");



        //layout1
        TextField searchInput = new TextField();
        TextField keyword = new TextField();
        Label searchLabel = new Label("Website URL");
        searchLabel.setLabelFor(searchInput);
        Label keywordLabel = new Label("Search for keyword");
        keywordLabel.setLabelFor(keyword);
        Button searchButton = new Button();
        searchButton.setText("Search!");
        this.searchText = new Text("");
        this.searchText.setFont(new Font(20));
        searchButton.setOnAction(e -> {
            if (this.checkURL(searchInput.getText())) {
                this.searchText.setText("Searching... This may take a while.");
                this.urls = this.c.run(searchInput.getText(), keyword.getText());
                primaryStage.setScene(scene2);
                this.urlsText.setText(this.urls);
            }
            else {
                AlertError.display("Error!", "Entered URL format is incorrect");
            }
        });

        FlowPane container = new FlowPane();
        container.getChildren().addAll(searchInput, keyword, searchButton);
        VBox layout1 = new VBox(10);
        layout1.setPadding(new Insets(20, 20, 20, 20));
        layout1.getChildren().addAll(searchLabel, searchInput, keywordLabel, keyword, this.searchText, searchButton);
        scene1 = new Scene(layout1, 300, 250);

问题是 this.searchText.setText("Searching... This may take a while."); 在if语句if (this.checkURL(searchInput.getText()))中时,它不会更改文本,但它确实在if语句之外起作用。我知道在尝试调试它时输入了if语句,并且它起作用了。单击按钮时,有人可以帮我更改searchText的文本吗?谢谢!

1 个答案:

答案 0 :(得分:-1)

该条目将为您提供帮助:How do I update Text Using Java.fx?

这是因为您需要使用:

| Product_Code | Account_Id | Channel_Desc | Min_Range | Max_Range       | openingbalance | Split_Balance | weighted_Avg           |
| 2000-0100    | 31179111   | Broker BDMs  | 0.000     | 4999.990        | 122314.480     | 4999.990      | 0.04087815277471645221 |
| 2000-0100    | 31179111   | Broker BDMs  | 5000.000  | 9999.990        | 122314.480     | 4999.990      | 0.04087815277471645221 |
| 2000-0100    | 31179111   | Broker BDMs  | 10000.000 | 19999.990       | 122314.480     | 9999.990      | 0.08175638730590196679 |
| 2000-0100    | 31179111   | Broker BDMs  | 20000.000 | 49999.990       | 122314.480     | 29999.990     | 0.24526932543064402513 |
| 2000-0100    | 31179111   | Broker BDMs  | 50000.000 | 99999999999.990 | 122314.480     | 72314.480     | 0.59121765468814485414 |

将所有GUI更新都添加到Platform.runLater(runnable);方法,它将被更新。