当前位置: 首页 > 知识库问答 >
问题:

如何在FXML加载后立即请求将焦点集中在文本字段上?

仲学真
2023-03-14
if(textField.isVisible()) textField.requestFocus();
    //The controller class for the FXML containing the button that is pressed to go to the FXML with the TextField
public class StartScene {
    @FXML
    BorderPane contentPane; //Where the other fxml file will be loaded
    private String path = "../fxml/fxmlFile.fxml";
    private static Parent component = null;
    @FXML
    private void initialize() throws IOException {

        component = FXMLLoader.load(getClass().getResource(path));
    }
    @FXML
    //This method is set to the onAction of a button
    private void loadComponent(){
        if (!contentPane.getChildren().contains(component)) {
            contentPane.getChildren().clear();
            contentPane.setCenter(component);
        }
        else { System.out.println("Component already inserted"); }
     }
}
public class TextFieldSceneController {
    @FXML
    TextField textField;
    @FXML
    private void initialize() {
        textField.setPromptText("Prompt");
    }
}

StartScene得FXML:

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.StackPane?>

<StackPane minHeight="500" minWidth="900" xmlns:fx="http://javafx.com/fxml"
       fx:controller="StartScene" >
    <BorderPane fx:id="borderPane">
        <left>
            <VBox minHeight="500" prefWidth="250" minWidth="10">
                    <Button text="BUTTON" minHeight="30" minWidth="100" prefWidth="250"  alignment="CENTER" onAction="#loadComponent"/>
             </VBox>
        </left>
        <center>
            <BorderPane minWidth="600" minHeight="500" fx:id="contentPane">
                 <children>
                 </children>
            </BorderPane>
        </center>
    </BorderPane>
</StackPane>

具有TextField场景的FXML:

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?> 

<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
       fx:controller="TextFieldSceneController" prefHeight="600.0" maxWidth="800.0" fx:id="root">
    <VBox fx:id="base">
        <children>
            <TextField fx:id="textField" alignment="CENTER"/>
        </children>
    </VBox>
</StackPane>

共有1个答案

阳福
2023-03-14

你有几个问题。

首先,您正在实际需要组件之前加载它。这意味着initialize()方法早在您在BorderPane中实际显示组件之前就已经调用并完成了运行。

相反,您应该只在单击按钮后加载FXML文档(将加载移到LoadComponent()方法)。

 类似资料:
  • 问题内容: 我有一个用netbeans开发的应用程序,我想在显示面板时将焦点设置为一定。我已经阅读了许多文章,并尝试了各种方法,但没有奏效。主要问题之一是在哪里放置所需的代码,我认为在我的情况下是 有一些指示使用Window Listener的帖子,但是随着netbeans生成GUI,我无法看到如何实现接口,因为我无法编辑创建jPANEL等的代码。整个过程非常令人沮丧,我真的没有相信这应该很困难。

  • 问题内容: 网页加载后如何自动将焦点设置到文本框? 是否有HTML标记可以执行此操作,还是必须通过Javascript完成? 问题答案: 如果您使用的是jquery: 或原型: 或纯JavaScript: 尽管请记住,这将替换其他加载处理程序,所以请在Google中查找addLoadEvent()以获取添加加载处理程序而不是替换的安全方法。

  • 如何设置组件的焦点? 我已经尝试了上述代码,但不起作用:(

  • 我有两个小部件。一旦用户完成了第一个文本字段,我将关注下一个。在Flutter里有办法做到这一点吗?目前,完成按钮只关闭键盘。我猜测类可能是解决这个问题的答案,但不确定它是如何工作的,有人有focusNode类的好例子吗?提前谢了。

  • 我想得到一个文本字段,每当我点击/点击/聚焦该字段时,它可以选择该字段中当前的全部文本。我自己也在其他React应用程序中使用了一个执行,但这种方法似乎不适用于材质UI。使用Material UI TextFields,我可以看到所选内容简短地覆盖了全文,然后返回到文本末尾闪烁的光标。 你知道怎么做吗?

  • 登录后我有一个文本字段。登录后,光标将自动聚焦在该文本字段上。如何验证该文本字段上是否存在光标/焦点? 以下是文本字段的HTML代码: