简体   繁体   English

有没有一种方法可以选择代码中的文本字段而无需单击它(javafx)?

[英]Is there a way to select a text field in code without clicking on it (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. 如果确实是这样做的方法,那么我有一个可以修改的模板,以演示如何实现。

Here is the template/sample code: 这是模板/示例代码:

Main class: 主班:

    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 class: MainControl类:

    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>

You can use 您可以使用

field1.requestFocus(); 

in your initialize() method, so your TextField field1 will be focused after your app is started. 在您的initialize()方法中,因此在您的应用启动后,您的TextField field1将被聚焦。 But notice, you have to wrap the requestFocus() call within a 但请注意,您必须将requestFocus()调用包装在

Platform.runLater(new Runnable() {

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

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 Application线程而不是Launcher线程上完成,所以如果只调用field1.requestFocus()则不会对我们的TextField产生任何影响。

This is very simple 这很简单

filedname.requestFocus();
fieldname.selectAll();

you have to first get the focus then use selectAll()function . 您必须首先获得焦点,然后使用selectAll()function。 if you will not use requestFocus() function then selectAll() will not work 如果您不使用requestFocus()函数,则selectAll()将不起作用

The solution is simple for classic java. 对于经典Java,该解决方案很简单。 You can use code below for select text in a textField without doubleclicking on it (no idea about javafx). 您可以使用下面的代码在textField中选择文本,而无需双击它(对javafx不了解)。

field1.requestFocus(); 
field1.setSelectionStart(0);
field1.setSelectionEnd(field1.getText().length());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM