-2

I have an issue, let's say I have a Javafx UI (no FXML), in an MVC structure, I've been assigned a view where the user can choose a quantity of X (increased by the click of a button controlled in the view already) to be upgrated after clicking a Button PowerUp.

BUT I don't have just an item type X, I have multiple (2 or more).

How do I override the method everytime I choose a different item type? This is the method I came up with, It's just part of the controller class which does only this

buttonB.setOnAction(new EventHandler <ActionEvent>(){
@ Override
    public void handle(ActionEvent actionEvent) { 
        //commands bla bla bla;
}
}

It can only be one item type at a time, I cannot power up 2 types of items at the same time.

I should create multiple eventhandlers like this with a different ? if so, what should I put instead of the ActionEvent? Recalling item from the model class?

I have found a similar question Here

Thanks and kind regards.

  • This is completely unclear. Why do you need multiple event handlers? Why not just check which item type is selected in the `handle()` method of the one event handler? – James_D May 21 '22 at 16:46

1 Answers1

1

There are a few ways to do this, but the easiest, in my opinion, is to create a Pane for each of the ability types which contains an input and a button. Then place all of those Panes in a StackPane, and make sure only one is ever visible.

import java.text.NumberFormat;

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.util.converter.NumberStringConverter;

public class AbilityAdjuster
extends Application {
    public enum Ability { FIRE, WATER, EARTH, AIR }

    private ComboBox<Ability> abilitySelector;

    private Pane createInputPane(Ability ability) {
        TextFormatter<Number> formatter = new TextFormatter<>(
            new NumberStringConverter(NumberFormat.getIntegerInstance()));
        formatter.setValue(0);

        TextField field = new TextField();
        field.setTextFormatter(formatter);

        Label label = new Label(ability + ":");
        label.setLabelFor(field);

        Button button = new Button("\u25b2");
        button.setOnAction(e -> {
            Number oldValue = formatter.getValue();
            int newValue = oldValue.intValue() + 1;
            formatter.setValue(newValue);
        });

        HBox pane = new HBox(6, label, field, button);
        pane.setAlignment(Pos.BASELINE_LEFT);
        HBox.setHgrow(field, Priority.ALWAYS);
        pane.visibleProperty().bind(
            abilitySelector.valueProperty().isEqualTo(ability));

        return pane;
    }

    @Override
    public void start(Stage stage) {
        abilitySelector = new ComboBox<>(
            FXCollections.observableArrayList(Ability.values()));
        abilitySelector.setValue(Ability.FIRE);

        Label selectorLabel = new Label("Type:");
        selectorLabel.setLabelFor(abilitySelector);

        HBox selectorPane = new HBox(6, selectorLabel, abilitySelector);
        selectorPane.setAlignment(Pos.BASELINE_LEFT);

        StackPane abilityPane = new StackPane();
        for (Ability ability : Ability.values()) {
            abilityPane.getChildren().add(createInputPane(ability));
        }

        VBox sceneContents = new VBox(12, selectorPane, abilityPane);
        sceneContents.setPadding(new Insets(12));

        stage.setScene(new Scene(sceneContents));
        stage.setTitle("Ability Adjuster");
        stage.show();
    }

    public static void main(String[] args) {
        launch(AbilityAdjuster.class, args);
    }
}
VGR
  • 40,506
  • 4
  • 48
  • 63