gpt4 book ai didi

javafx - 从 TextField JavaFX 移除焦点

转载 作者:行者123 更新时间:2023-12-02 06:53:13 25 4
gpt4 key购买 nike

我无法从 TextField 中移除焦点。在我看来,我只有一个可编辑的内容:TextField 中的文本。问题是,作为用户,一旦 TextField 被聚焦,就永远不会摆脱焦点(因为没有其他东西可以聚焦)。我想从 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;
}


}

文件格式

<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>

最佳答案

requestFocus 可用于任何其他 Node 以从 TextField 中移除焦点。当然,您可以找到一个 Node ,其中焦点不会造成伤害...

以下示例通过在 onAction 的根 requestFocus 上调用 Pane 来移除 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();
}

关于javafx - 从 TextField JavaFX 移除焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38039114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com