首页 > 解决方案 > 在 JavaFX 中同时启用/禁用组按钮

问题描述

我想在 JavaFX 中禁用多个相同“类型”的按钮。例如,我有多个用于不同操作的取消按钮,但我想同时启用/禁用它们,但似乎我无法在 LinkedList 中访问它们。

在下面的“最小且可重现”示例中,当按下开始时,它应该启用(在 fxml 中默认禁用)取消按钮,但它没有。

演示控制器.java

public class DemoController {
    @FXML
    Button startBtn;
    @FXML
    Button cancelBtn1; // disabled by default
    @FXML
    Button cancelBtn2; // disabled by default
    @FXML
    Tab tab1;

    public SDK sdk;

    List<Node> cancelBtnGroup = new LinkedList<Node>();

    public void initialize() {
        cancelBtnGroup.add(cancelBtn1);
        cancelBtnGroup.add(cancelBtn2);

        startBtn.setOnAction(event -> {
            tab1.setDisable(true);
            startBtn.setDisable(true);
            System.out.println("Disabling cancel buttons");
            HashMap<String, String> test = new HashMap<String, String>();
            test.put("disableCancel", "false");
            sdk.onEvent(test);
        });

        initializeSDK();
    }

    public void initializeSDK() {
        sdk = new SDK() {
            @Override
            public void onEvent(HashMap<String, String> e) {
                if(e.containsKey("disableCancel")) {
                    for(Node btn : cancelBtnGroup) {
                        btn.setDisable(Boolean.parseBoolean(e.get("disableCancel")));
                    }
                }
            }
        };
    }
}

似乎我以某种方式覆盖了按钮,但我不明白如何。我觉得我不了解某些功能的范围。我试图使代码尽可能简化和可读,所以如果我遗漏了一些非常明显的东西,这可能就是原因。SDK 对象不包含具有相同名称的对象,以确保这不是我更改名称的问题。

标签: javajavafxscenebuilder

解决方案


从您发布的代码中,我无法确定它为什么不起作用。但是,这是一个使用 disable-the-common-ancestor 方法和 FXML 的最小示例:

应用程序.java

package com.example;

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

public class App extends Application {

  @Override
  public void start(Stage primaryStage) throws IOException {
    var root = FXMLLoader.<Parent>load(getClass().getResource("/App.fxml"));
    var scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

应用程序.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns="http://javafx.com/javafx/13" xmlns:fx="http://javafx.com/fxml/1"
      fx:controller="com.example.Controller" spacing="10" prefWidth="500" prefHeight="300"
      alignment="CENTER">

    <padding>
        <Insets topRightBottomLeft="10"/>
    </padding>

    <ToggleButton fx:id="toggleBtn"/>

    <HBox fx:id="container" alignment="CENTER" spacing="10">
        <Button text="Button #1"/>
        <Button text="Button #2"/>
        <Button text="Button #3"/>
        <Button text="Button #4"/>
    </HBox>

</VBox>

控制器.java

package com.example;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.HBox;

public class Controller {

  @FXML private ToggleButton toggleBtn;
  @FXML private HBox container;

  @FXML
  private void initialize() {
    toggleBtn.textProperty().bind(
        Bindings.when(toggleBtn.selectedProperty())
          .then("Disable Buttons")
          .otherwise("Enable Buttons")
    );
    container.disableProperty().bind(toggleBtn.selectedProperty().not());
  }
}

注意:除了在代码中绑定容器的disable属性之外,还可以使用以下方法完成:

<!-- Everything else omitted for brevity -->
<HBox disable="${!toggleBtn.selected}">

当然,如果您使用 a 以外的其他东西,ToggleButton那么在 FXML 中实现绑定可能不可行。


推荐阅读