JavaFX 8 : Changing title of primary stage

标签 java javafx javafx-8

当窗口已经显示时,如何更改初级阶段的标题?

Stage#setTitle(String) 显然不是诀窍。

最佳答案

setTitle(...) 在这里似乎工作正常:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class UpdateStageTitle extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField titleField = new TextField();
        titleField.textProperty().addListener((obs, oldText, newText) -> 
            primaryStage.setTitle(newText));
        HBox root = new HBox(5, new Label("Window title: "), titleField);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 350, 75);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

更新以下评论。

更复杂的例子,如果需要在controller的initialize方法中设置这个,需要安排controller在FXMLLoader之前有stage的引用load() 方法被调用。您可以通过在加载器上调用 setController 或调用 setControllerFactory 来完成此操作。我通常更喜欢设置 Controller 工厂,因为它允许在 FXML 中使用 fx:controller 属性(直接设置 Controller 禁止这样做)。

所以:

PrimaryStageAware.java:

package application;

import javafx.stage.Stage;

public interface PrimaryStageAware {
    public void setPrimaryStage(Stage primaryStage);
}

主要.java:

package application;

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


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("TitleSetter.fxml"));
            loader.setControllerFactory((Class<?> type) -> {
                try {
                    Object controller = type.newInstance();
                    if (controller instanceof PrimaryStageAware) {
                        ((PrimaryStageAware) controller).setPrimaryStage(primaryStage);
                    }
                    return controller ;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            HBox root = loader.load();
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

标题 setter .fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>

<HBox alignment="CENTER" xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="application.TitleSettingController">
    <Label text="Update title: "/>
    <TextField  fx:id="titleField" />
</HBox>

标题设置 Controller .java:

package application;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class TitleSettingController implements PrimaryStageAware {
    private Stage stage ;

    @FXML
    private TextField titleField ;

    @Override
    public void setPrimaryStage(Stage primaryStage) {
        this.stage = primaryStage ;
    }

    public void initialize()  {
        updateTitle("Initial title");
        titleField.textProperty().addListener((obs, oldTitle, newTitle) -> updateTitle(newTitle));
    }

    private void updateTitle(String title) {
        if (stage != null) {
            stage.setTitle(title);
        } else {
            System.out.println("Warning: null stage");
        }
    }

}

(FXML 与 Java 文件位于同一个包中)。

关于JavaFX 8 : Changing title of primary stage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26494865/

相关文章:

java - 如何将节点从 xml 文档附加到现有的 xml 文档

java - 访问数组列表中的元素时出错

java - 通过单个类管理不同的 JavaFX 阶段

java - 如何正确避免特定的 UITableViewCell 可编辑?

java - 绑定(bind) StringProperty 时出现多线程错误

Java JEditorPane 用超链接替换选中的文本

java - Android Httpclient [NoSuchMethodError :org. apache.http.entity.ContentType.create]

javafx - 如何防止表格隐藏TornadoFX

java - 如何使用绑定(bind)使 JavaFX TextField 中的文本自动更改?

javafx-8 - 删除 JavaFX 输入的内部阴影