从数字中显示组合框值 [英] Display Combobox values from numbers

查看:106
本文介绍了从数字中显示组合框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个int值,我想用它进行配置。它可以有2个值 - 0表示活动,1表示阻止。我想将它显示在友好的组合框中:

I have a int value which I want to use for configuration. It can have 2 values - 0 for active and 1 for Blocked. I want to display this into friendly combo box:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MainApp extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {
        int state = 0;

        ObservableList<String> options = FXCollections.observableArrayList(
            "Active",
            "Blocked"
        );
        ComboBox comboBox = new ComboBox(options);
        BorderPane bp = new BorderPane(comboBox);
        bp.setPrefSize(800, 800);
        Scene scene = new Scene(bp);
        stage.setScene(scene);
        stage.show();
    }

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

我不清楚我是如何实现这个的进入JavaFX Combobox。
当我有0时我想将其显示为活动当我有1时我想显示已阻止以及当我更改ComboBox值以更新 int state value。

It's not clear for me how I have to implement this into JavaFX Combobox. When I have 0 I want to display this as Active and when I have 1 I want to display Blocked and also when I change the ComboBox value to update also int state value.

推荐答案

有不同的方法可以解决这个问题。我列出了以下三种解决方案。您可以使用以下任何一种您认为适合您的方案的解决方案。

There are different ways to solve this problem. I have listed three of the solutions below. You can use any one of the below solutions which you feel is apt for your scenario.

创建自定义类 KeyValuePair ,用于存储字符串及其对应的值。暴露了必填字段的getter。

Create a custom class KeyValuePair, for storing the string and its corresponding value. Exposed the getters for the required fields.

后来,我使用了comboxbox的 setCellFactory()来显示所需的数据。使用StringConverter来代替对象显示密钥。

Later, I have used the setCellFactory() of the comboxbox to show the required data. Use StringConverter to show the key in place of the object.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception
    {
        KeyValuePair keyValuePair1 = new KeyValuePair("Active", 0);
        KeyValuePair keyValuePair2 = new KeyValuePair("Blocked", 1);


        ObservableList<KeyValuePair> options = FXCollections.observableArrayList();
        options.addAll(keyValuePair1, keyValuePair2);

        ComboBox<KeyValuePair> comboBox = new ComboBox<>(options);

        // show the correct text
        comboBox.setCellFactory((ListView<KeyValuePair> param) -> {
            final ListCell<KeyValuePair> cell = new ListCell<KeyValuePair>(){

                @Override
                protected void updateItem(KeyValuePair t, boolean bln) {
                    super.updateItem(t, bln);

                    if(t != null){
                        setText(String.valueOf(t.getKey()));
                    }else{
                        setText(null);
                    }
                }

            };
            return cell;
        });


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

            @Override
            public KeyValuePair fromString(String string) {
                return null; // No conversion fromString needed.
            }
        });


        // print the value
        comboBox.valueProperty().addListener((ov, oldVal, newVal) -> {
            System.out.println(newVal.getKey() + " - " + newVal.getValue());
        });

        BorderPane bp = new BorderPane(comboBox);
        bp.setPrefSize(800, 800);
        Scene scene = new Scene(bp);
            stage.setScene(scene);
        stage.show();
    }

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

    class KeyValuePair {
        private final String key;
        private final int value;

        public KeyValuePair(String key, int value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public int getValue() {
            return value;
        }
    }
}



不使用额外的课程



正如@kleopatra所建议的那样,你甚至可以在不使用额外课程的情况下这样做。

Without using an extra class

As suggested by @kleopatra, you can even do this without using an extra class.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class Main extends Application {

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

        ObservableList<Integer> options = FXCollections.observableArrayList();
        options.addAll(1, 0);

        ComboBox<Integer> comboBox = new ComboBox<>(options);

        // show the correct text
        comboBox.setCellFactory((ListView<Integer> param) -> {
            final ListCell<Integer> cell = new ListCell<Integer>(){

                @Override
                protected void updateItem(Integer t, boolean bln) {
                    super.updateItem(t, bln);

                    if(t != null){
                        setText(t == 1 ? "Active" : "Blocked");
                    }else{
                        setText(null);
                    }
                }

            };
            return cell;
        });


        comboBox.setConverter(new StringConverter<Integer>() {
              @Override
              public String toString(Integer object) {
                  return object == 1 ? "Active" : "Blocked";
              }

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

        // print the value
        comboBox.valueProperty().addListener((ov, oldVal, newVal) -> {
            System.out.println("Changed from " + oldVal + " to " + newVal);
        });

        BorderPane bp = new BorderPane(comboBox);
        bp.setPrefSize(800, 800);
        Scene scene = new Scene(bp);
        stage.setScene(scene);
        stage.show();
    }

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



使用绑定



如果您不想承担创建新课程的痛苦,也可以使用Bindings,总是有两个元素,即Active和Blocked。

Using Bindings

You can also use Bindings if you don't want to take the pain of creating a new class and you will always have two elements i.e. Active and Blocked.

将组合框的valueProperty()绑定到状态,该状态应该存储值0或1。

Just bind the valueProperty() of your combobox to the state, which is supposed to store the value i.e. 0 or 1.

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        IntegerProperty state = new SimpleIntegerProperty();
        ObservableList options = FXCollections.observableArrayList("Active", "Blocked");

        ComboBox<String> comboBox = new ComboBox<>(options);
        state.bind(Bindings.when(comboBox.valueProperty().isEqualTo("Active")).then(0).otherwise(1));

        BorderPane bp = new BorderPane(comboBox);
        bp.setPrefSize(800, 800);
        Scene scene = new Scene(bp);
        stage.setScene(scene);
        stage.show();
    }

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

这篇关于从数字中显示组合框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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