如何在 JavaFX 中为 ComboBox 中的项目添加值 [英] How do I add a value to items in a ComboBox in JavaFX

查看:27
本文介绍了如何在 JavaFX 中为 ComboBox 中的项目添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为组合框中的商品添加值,以便当用户从 ComboBox 中选择商品时,我可以显示该商品的价格

How can I add a value to items in a combo box so when the user selects an item from the ComboBox I am able to display the price for that item

例如.如果用户选择一种动物,我可以显示该动物的价格.这用户选择dog然后我可以显示$45的价格.

Eg. if the user selects an animal I can display the price of that animal. The the user selects dog then I can display the price of $45.

public class comboBox extends Application {

    Stage window;
    Scene scene;
    Button button;
    ComboBox<String> comboBox;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("ComboBox");
        button = new Button("Submit");

        comboBox = new ComboBox<>();
        comboBox.getItems().addAll(
            "cat",
            "dog",
            "bird"
        );

        comboBox.setPromptText("Please select one");
        button.setOnAction(e -> printPrice());

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(60, 60, 60, 60));
        layout.getChildren().addAll(comboBox, button);

        scene = new Scene(layout, 450, 350);
        window.setScene(scene);
        window.show();
    }

    private void printPrice(){
        System.out.println(comboBox.getValue());
    }
}

我试图修复代码,这就是我得到的结果还有一些错误有人知道我做错了什么吗?

I have tried to fix the code and this is what I got there is still a few errors anyone know what I am doing wrong?

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.collections.FXCollections;

public class animals extends Application {

Stage window;
Scene scene;
Button button;
ComboBox<String> comboBox;




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

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("ComboBox ");
    button = new Button("Submit");

    comboBox.setConverter(new StringConverter<Animal>() {
@Override
public String toString(Animal object) {
    return object.getName();
}

@Override
public Animal fromString(String string) {
    return null;
}
});


ComboBox<Animal> comboBox = new ComboBox<Animal>();
comboBox.setItems(FXCollections.observableArrayList(new Animal("Dog", 30.12),  new Animal("Cat", 23.23), new Animal("Bird", 15.0)));

comboBox.valueProperty().addListener((obs, oldVal, newVal) ->  System.out.println("Price of the " + newVal.getName() + " is : "  +  newVal.getPrice()));    }

VBox layout = new VBox(10);
    layout.setPadding(new Insets(60, 60, 60, 60));
    layout.getChildren().addAll(comboBox, button);

    scene = new Scene(layout, 500, 350);
    window.setScene(scene);
    window.show();

}

public class Animal {
private String name;
private Double price;

public Double getPrice() {
    return price;
}

public String getName() {
    return name;
}

public Animal(String name, Double price) {
    this.name = name;
    this.price = price;

}
}

此外,在用户选择动物后,我如何能够在组合框下显示价格?所以它会说动物成本的价格"

also, how would I be able to display the price under the combo box after the user selects an animal? so it would say 'the price for that animal cost'

推荐答案

您应该为 ComboBox 提供一个数据模型,用于存储动物的名称和价格,例如类的实例动物.

You should provide a data model to the ComboBox which stores the name and the price of the animal, for example instances of the class Animal.

public class Animal {
    private String name;
    private Double price;

    public Double getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public Animal(String name, Double price) {
        this.name = name;
        this.price = price;
    }
}

然后在您的 ComboBox 中,您可以显示这些 Animal 实例:

Then in your ComboBox you can display these Animal instances:

ComboBox<Animal> comboBox = new ComboBox<Animal>();
comboBox.setItems(FXCollections.observableArrayList(
    new Animal("Dog", 30.12),
    new Animal("Cat", 23.23), 
    new Animal("Bird", 15.0)));

comboBox.valueProperty().addListener((obs, oldVal, newVal) -> 
    System.out.println("Price of the " + newVal.getName() + " is : " + newVal.getPrice()));

唯一剩下的就是在 ComboBox 上显示动物的名称而不是对象本身.为此,您可以使用例如 StringConverter:

The only thing left to display the name of the animals on the ComboBox rather than the objects itself. To achieve this, you can use for example a StringConverter:

comboBox.setConverter(new StringConverter<Animal>() {
    @Override
    public String toString(Animal object) {
        return object.getName();
    }

    @Override
    public Animal fromString(String string) {
        return null;
    }
});

值改变时,输出如下:

Price of the Cat is : 23.23
Price of the Dog is : 30.12
Price of the Bird is : 15.0

MCVE:

public class Animals extends Application {
    private ComboBox<Animal> comboBox = new ComboBox<>();
    private Text textNamePrice = new Text();

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

    @Override
    public void start(Stage primaryStage) throws Exception {

        comboBox.setConverter(new StringConverter<Animal>() {
            @Override
            public String toString(Animal object) {
                return object.getName();
            }

            @Override
            public Animal fromString(String string) {
                return null;
            }
        });

        comboBox.setItems(FXCollections.observableArrayList(new Animal("Dog", 30.12),
                new Animal("Cat", 23.23),
                new Animal("Bird", 15.0)));

        comboBox.valueProperty().addListener((obs, oldVal, newVal) -> {
            String selectionText = "Price of the " + newVal.getName() + " is : " + newVal.getPrice();

            System.out.println(selectionText);
            textNamePrice.setText(selectionText);
        });

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(60, 60, 60, 60));
        layout.getChildren().addAll(comboBox, textNamePrice);

        Scene scene = new Scene(layout, 500, 350);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public class Animal {
        private String name;
        private Double price;

        public Double getPrice() { return price; }

        public String getName() { return name; }

        public Animal(String name, Double price) {
            this.name = name;
            this.price = price;
        }
    }
}

这篇关于如何在 JavaFX 中为 ComboBox 中的项目添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆