【问题标题】:How to block text field when user change database from drop down menu?用户从下拉菜单更改数据库时如何阻止文本字段?
【发布时间】:2021-07-04 13:03:36
【问题描述】:

我有 GUI 应用程序可以连接几个数据库,并且从每个数据库中我使用不同的字段进行连接。主要问题是如何根据用户选择的数据库禁用字段,例如当用户选择 MySQL 时,如下图所示,dbName 和 db 路径等字段被禁用。

但是当用户选择其他,例如 SQLite 时,应该禁用 localhost、端口、用户名和密码等字段。现在我禁用了 MySQL 的字段,但是当我更改为其他数据库时,相同的字段仍然被禁用。

下面是我的代码:

private void choseFieldsWhichNeedToDb() {
    String value = databaseChoiceBox.getValue();
    if (value.equals("MySql")){
        databaseNameTextField.setDisable(true);
        databasePathTextField.setDisable(true);
    }
}

private void fillChoiceBox() {
    DatabaseType[] types = DatabaseType.values();
    for (DatabaseType type : types) {
        databaseChoiceBox.getItems().add(type.getType());
    }
    databaseChoiceBox.setValue("MySql");
}

【问题讨论】:

  • 那么当用户选择不同的数据库时启用字段(例如field.setDisable(false))?

标签: java user-interface javafx


【解决方案1】:

不是一个完整的答案,但希望足以让您完成您的应用。

您需要创建一个ChangeListener 来监听ChoiceBox 所选值的变化,并根据ChoiceBox 中所选值启用或禁用相关的TextFields。

以下代码创建了一个类似于屏幕截图中的 GUI,但仅根据所选数据库启用(或禁用)dbNamepath 文本字段。

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class ChooseDb extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane root = new GridPane();
        root.setHgap(10.0);
        root.setVgap(10.0);
        root.setPadding(new Insets(20, 20, 20, 20));
        Label hostLabel = new Label("Host");
        root.add(hostLabel, 0, 0);
        TextField hostTextField = new TextField();
        root.add(hostTextField, 1, 0);
        Label portLabel = new Label("Port");
        root.add(portLabel, 2, 0);
        TextField portTextField = new TextField();
        root.add(portTextField, 3, 0);
        Label usernameLabel = new Label("Username");
        root.add(usernameLabel, 0, 1);
        TextField usernameTextField = new TextField();
        root.add(usernameTextField, 1, 1);
        Label passwordLabel = new Label("Password");
        root.add(passwordLabel, 2, 1);
        TextField passwordTextField = new TextField();
        root.add(passwordTextField, 3, 1);
        Label dbNameLabel = new Label("Database name");
        root.add(dbNameLabel, 0, 2);
        TextField dbNameTextField = new TextField();
        dbNameTextField.setDisable(true);
        root.add(dbNameTextField, 1, 2);
        Label dbPathLabel = new Label("Path to database file");
        root.add(dbPathLabel, 2, 2);
        TextField dbPathTextField = new TextField();
        dbPathTextField.setDisable(true);
        root.add(dbPathTextField, 3, 2);
        ChoiceBox<String> dbNames = new ChoiceBox<>(FXCollections.observableArrayList("MySQL", "SQLite"));
        dbNames.setValue("MySQL");
        dbNames.valueProperty().addListener(new ChangeListener<String>() {

            @Override
            public void changed(ObservableValue<? extends String> observable,
                                String oldValue,
                                String newValue) {
                switch (newValue) {
                    case "MySQL":
                        dbNameTextField.setDisable(true);
                        dbPathTextField.setDisable(true);
                        break;
                    case "SQLite":
                        dbNameTextField.setDisable(false);
                        dbPathTextField.setDisable(false);
                        break;
                }
            }
        });
        root.add(dbNames, 0, 3);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

【讨论】:

  • 好的,我添加了监听器,但是当我启动应用程序并且默认值等于 MySQL 时,所有字段都可以填写
  • @mroma 您需要初始设置禁用字段以匹配初始ChoiceBox 选择。请注意,在我的代码中,我最初将 dbNameTextField 设置为禁用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
相关资源
最近更新 更多