remove focus from TextField JavaFX

13,141

requestFocus can be used on any other Node to remove the focus from the TextField. Surely you can find a Node where the focus does no harm...

The following example removes the focus in the onAction event by calling requestFocus on the root Pane of the Scene:

@Override
public void start(Stage primaryStage) {
    TextField tf = new TextField();

    StackPane root = new StackPane(tf);

    Scene scene = new Scene(root, 300, 200);

    tf.setOnAction((ActionEvent event) -> {
        System.out.println(tf.getText());
        tf.clear();
        root.requestFocus();
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}
Share:
13,141
D.Stifler
Author by

D.Stifler

Updated on June 18, 2022

Comments

  • D.Stifler
    D.Stifler almost 2 years

    I can not remove focus from the TextField. On my view I only have one editable content: Text in a TextField. The problem is that as a user one never gets rid of the focus (because there is nothing else to focus on), once the TextField is focused. I want to remove the focus from the TextField.

    enter image description here

    enter image description here

    enter image description here

    public class ControllerMain implements Initializable {
        private MainApp app;
        private ObservableList<UserData> data = FXCollections.observableArrayList();
        @FXML
        private Button btnAdd;
        @FXML
        public TableView<UserData> table = new TableView<>();
        @FXML
        private TableColumn<UserData, String> column1;
        @FXML
        private TableColumn<UserData, Integer> column2;
        @FXML
        private TableColumn<UserData, String> column3;
        @FXML
        private TableColumn<UserData, String> column4;
        @FXML
        private TableColumn<UserData, Integer> column5;
        @FXML
        private TextField txtSearch;
    
        public ControllerMain() {
        }
    
        public void setApp(MainApp instance) {
            this.app = instance;
        }
    
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            String companyName = "companyName";
            column1.setCellValueFactory(new PropertyValueFactory<>(companyName));
            String phone = "phone";
            column2.setCellValueFactory(new PropertyValueFactory<>(phone));
            String address = "address";
            column3.setCellValueFactory(new PropertyValueFactory<>(address));
            String other = "other";
            column4.setCellValueFactory(new PropertyValueFactory<>(other));
            String id = "id";
            column5.setCellValueFactory(new PropertyValueFactory<>(id));
            column5.setVisible(false);
            initSearchFilter();
        }
    
        private void initSearchFilter() {
            txtSearch.textProperty().addListener(o -> {
                if (txtSearch.textProperty().get().isEmpty()) {
                    table.setItems(data);
                    return;
                }
                ObservableList<UserData> tableItems = FXCollections.observableArrayList();
                ObservableList<TableColumn<UserData, ?>> cols = table.getColumns();
                for (UserData aData : data) {
                    for (TableColumn<UserData, ?> col1 : cols) {
                        String cellValue = col1.getCellData(aData).toString();
                        cellValue = cellValue.toLowerCase();
                        if (cellValue.contains(txtSearch.textProperty().get().toLowerCase())) {
                            tableItems.add(aData);
                            break;
                        }
                    }
                }
                table.setItems(tableItems);
            });
        }
    
        @FXML
        private void handleOpenAddForm() throws IOException {
            UserData userData = new UserData();
            app.initAddForm();
            getData().add(userData);
            loadDatabaseData();
        }
    
        @FXML
        private void deleteHandle() {
            try (Connection con = new DBConnect().getConnected();
                 PreparedStatement prep = con.prepareStatement("DELETE FROM job.job WHERE job.id = ?")) {
                UserData selectedItem = table.getSelectionModel().getSelectedItem();
                prep.setInt(1, selectedItem.getId());
                prep.execute();
                getData().remove(selectedItem);
                loadDatabaseData();
            } catch (Exception e) {
                System.err.println("Ошибка удаления: " + e.getMessage());
            }
        }
    
        @FXML
        private void handleOpenUpdateForm() throws IOException {
            UserData userData = table.getSelectionModel().getSelectedItem();
            app.initUpdateForm(userData);
            userData.setId(userData.idProperty().getValue());
            userData.setCompanyName(userData.companyNameProperty().getValue());
            userData.setPhone(userData.phoneProperty().getValue());
            userData.setAddress(userData.addressProperty().getValue());
            userData.setOther(userData.otherProperty().getValue());
            loadDatabaseData();
        }
    
        public void loadDatabaseData() {
            try (Connection con = new DBConnect().getConnected();
                 PreparedStatement preparedStatement = con.prepareStatement("SELECT  * FROM job.job");
                 ResultSet resultSet = preparedStatement.executeQuery()) {
                getData().clear();
                while (resultSet.next()) {
                    getData().add(new UserData(
                            resultSet.getInt("id"),
                            resultSet.getString("company_name"),
                            resultSet.getInt("phone"),
                            resultSet.getString("address"),
                            resultSet.getString("other")
                    ));
                    table.setItems(getData());
                }
    
                txtSearch.setFocusTraversable(false);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Error on Building Data");
            }
        }
    
        private ObservableList<UserData> getData() {
            return data;
        }
    
    
    }
    

    FXML

    <BorderPane 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="com.mysoft.controller.ControllerMain">
     <center>
            <TableView fx:id="table" prefHeight="370.0" prefWidth="613.0" BorderPane.alignment="CENTER"
                       focusTraversable="false">
                <columns>
                    <TableColumn fx:id="column1" prefWidth="139.0" text="Название компании"/>
                    <TableColumn fx:id="column2" minWidth="0.0" prefWidth="85.0" text="Телефон"/>
                    <TableColumn fx:id="column3" minWidth="0.0" prefWidth="217.0" text="Адрес"/>
                    <TableColumn fx:id="column4" minWidth="0.0" prefWidth="156.0" text="Другое"/>
                    <TableColumn fx:id="column5" minWidth="0.0" prefWidth="156.0" text="ID"/>
                </columns>
            </TableView>
        </center>
        <bottom>
            <GridPane BorderPane.alignment="CENTER">
                <columnConstraints>
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                </columnConstraints>
                <rowConstraints>
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                </rowConstraints>
                <BorderPane.margin>
                    <Insets left="30.0" right="20.0"/>
                </BorderPane.margin>
                <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="170.0" text="Добавить"
                        focusTraversable="false" onAction="#handleOpenAddForm"/>
                <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="170.0" text="Редактировать"
                        GridPane.columnIndex="1" focusTraversable="false" onAction="#handleOpenUpdateForm"/>
                <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="170.0" text="Удалить"
                        GridPane.columnIndex="2" focusTraversable="false" onAction="#deleteHandle"/>
            </GridPane>
        </bottom>
        <top>
            <GridPane BorderPane.alignment="CENTER">
                <columnConstraints>
                    <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="268.0" minWidth="10.0"
                                       prefWidth="259.0"/>
                    <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="357.0" minWidth="10.0"
                                       prefWidth="321.0"/>
                </columnConstraints>
                <rowConstraints>
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                </rowConstraints>
                <padding>
                    <Insets left="20.0"/>
                </padding>
                <TextField prefHeight="25.0" prefWidth="206.0" promptText="Введите данные для поиска . . ."
                            fx:id="txtSearch" focusTraversable="false">
                    <font>
                        <Font name="Arial Italic" size="12.0"/>
                    </font>
                </TextField>
            </GridPane>
        </top>
    </BorderPane>