2

I have built a GUI with JavaFX/Scenebuilder which has multiple dropdowns with similar functions. I want to use the same function for all dropdowns so I have to check where the action event came from. My current code is:

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

And it works, however it gives the following warning:

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

So from what I understand the getSource() returns a general Object, which is not guaranteed to be cast-able to ComboBox ? What would be the solution to this problem?

lutkeveld
  • 49
  • 3
  • Use `instanceof` to validate that the event source is the right type before you cast it. – Jason May 12 '19 at 23:53
  • @Jason how would that work? I tried "if(event.getSource() instanceof ComboBox)" but that does not seem to fix the problem – lutkeveld May 13 '19 at 00:06

2 Answers2

2

If you are sure that whatever that will come from event.getSource(); will be a ComboBox, then you can use @SuppressWarnings("unchecked") right above that line:

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

So it won't bother anymore and will only affect that line.

Jeremy Then
  • 525
  • 2
  • 12
2

Note: This does not directly answer the posted question, but is meant to provide another means of accomplishing the OP's apparent overall goal.

While you could certainly suppress the warning as another answer suggests, I wonder if you might be better off implementing a Listener on your ComboBox instead.

You can easily add a Listener that will execute code whenever a new value is selected from the ComboBox:

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

This has a couple of benefits over your current implementation:

  • You do not need to worry about casting or checking the event source.
  • Your current implementation would throw a NulLPointerException if no value is selected. The if (newValue != null) checks for that.
  • You do not need to write a separate method to handle selection changes.

Here is a quick sample application to demonstrate:

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();
    }
}
Zephyr
  • 9,885
  • 4
  • 28
  • 63