有没有办法在代码中选择文本字段而不点击它(javafx)? [英] Is there a way to select a text field in code without clicking on it (javafx)?

查看:73
本文介绍了有没有办法在代码中选择文本字段而不点击它(javafx)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何或甚至可以在代码中选择文本字段。我在这个问题上找不到任何东西。如果这确实是一种方法,我有一个模板可以修改,以证明如何做到这一点。

I am not sure how to or even if it is possible to select text fields in code. I could not find anything on the subject. If the is indeed a way to do so i have a template that could be modified to demonstrate how this could be done.

这是模板/示例代码:

主类:

    package application;

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


    public class Main extends Application {
public void start(Stage primaryStage) {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
        Scene scene = new Scene(root,600,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test");
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

MainControl类:

MainControl class:

    package application;

    import java.net.URL;
    import java.util.ResourceBundle;

    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;

    public class MainControl implements Initializable {
@FXML
Button button;
@FXML
TextField field1;
@FXML
TextField field2;
boolean field1selected=true;
public void initialize(URL arg0, ResourceBundle arg1) {

}
public void switchTextFeild(ActionEvent event){
/*
The code for switch on witch TextField is selected would go here
*/
}
}

Main.fxml:

Main.fxml:

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

    <?import javafx.scene.text.*?>
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>


    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainControl">
    <children>
    <TextField fx:id="field1" layoutX="6.0" layoutY="2.0" prefHeight="394.0" prefWidth="149.0" />
    <TextField fx:id="field2" layoutX="445.0" layoutY="2.0" prefHeight="394.0" prefWidth="149.0" />
  <Button fx:id="button" layoutX="177.0" layoutY="14.0" mnemonicParsing="false" onAction="#switchTextFeild" prefHeight="95.0" prefWidth="239.0" text="Switch" textAlignment="CENTER" wrapText="true">
     <font>
        <Font size="24.0" />
     </font>
  </Button>
  </children>
  </AnchorPane>


推荐答案

您可以使用

field1.requestFocus(); 

,所以应用程序启动后,您的 TextField field1 将重点关注。
但请注意,您必须在

in your initialize() method, so your TextField field1 will be focused after your app is started. But notice, you have to wrap the requestFocus() call within a

Platform.runLater(new Runnable() {

            @Override
            public void run() {
                field1.requestFocus();
            }
        });

因为这应该在JavaFX Application Thread上完成,而不是在Launcher Thread上完成,所以如果你愿意的话只调用 field1.requestFocus()这对我们的 TextField 没有任何影响。

because this should be done on the JavaFX Application Thread and not on the Launcher Thread, so if you would only call field1.requestFocus() this wont have any effect on our TextField.

这篇关于有没有办法在代码中选择文本字段而不点击它(javafx)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆