【问题标题】:Fix unchecked cast from Object to ComboBox<String> javafx [duplicate]修复从 Object 到 ComboBox<String> javafx 的未经检查的强制转换 [重复]
【发布时间】:2019-05-12 23:48:03
【问题描述】:

我用 JavaFX/Scenebuilder 构建了一个 GUI,它有多个具有类似功能的下拉菜单。我想对所有下拉菜单使用相同的功能,所以我必须检查动作事件的来源。我当前的代码是:

public void dropdownPressed(ActionEvent event) {
    ComboBox<String> comboBox = (ComboBox<String>) event.getSource();
    Label.setText(comboBox.getSelectionModel().getSelectedItem());
}

它有效,但是它给出了以下警告:

Type safety: Unchecked cast from Object to ComboBox<String>

所以据我了解 getSource() 返回一个通用对象,不能保证可以转换为 ComboBox ?有什么办法可以解决这个问题?

【问题讨论】:

  • 使用instanceof 来验证事件源是正确的类型,然后再进行转换。
  • @Jason 这将如何工作?我试过“if(event.getSource() instanceof ComboBox)”,但这似乎并不能解决问题

标签: java string object javafx combobox


【解决方案1】:

如果你确定那将来自 event.getSource();将是一个 ComboBox,然后您可以在该行上方使用@SuppressWarnings("unchecked")

@SuppressWarnings("unchecked")
ComboBox<String> comboBox = (ComboBox<String>) event.getSource();

所以它不会再麻烦了,只会影响那条线。

【讨论】:

    【解决方案2】:

    注意:这并不直接回答发布的问题,而是提供另一种实现 OP 明显总体目标的方法。

    虽然您当然可以像另一个答案所暗示的那样抑制警告,但我想知道您是否最好在ComboBox 上实施Listener

    您可以轻松地添加一个Listener,只要从ComboBox 中选择一个新值,它就会执行代码:

    comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            label.setText(newValue);
        }
    });
    

    与您当前的实施相比,这有几个好处:

    • 您无需担心投射或检查事件源。
    • 如果未选择任何值,您当前的实现将抛出 NulLPointerExceptionif (newValue != null) 会检查这一点。
    • 您无需编写单独的方法来处理选择更改。

    这里有一个快速的示例应用程序来演示:

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.Label;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class DropDownListener extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            // Simple interface
            VBox root = new VBox(5);
            root.setPadding(new Insets(10));
            root.setAlignment(Pos.CENTER);
    
            // Simple ComboBox
            ComboBox<String> comboBox = new ComboBox<>();
            comboBox.getItems().addAll("One", "Two", "Three", "Four", "Five");
    
            // Label to show selection
            Label label = new Label();
    
            // Use a listener to update the Label when a new item is selected from the ComboBox
            comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
                if (newValue != null) {
                    label.setText(newValue);
                }
            });
    
            root.getChildren().addAll(comboBox, label);
    
            // Show the Stage
            primaryStage.setWidth(300);
            primaryStage.setHeight(300);
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-02-03
      • 2013-12-15
      相关资源
      最近更新 更多