如何在JavaFX中向ComboBox项添加图像工具提示?

时间:2016-08-03 08:46:14

标签: java javafx combobox tooltip javafx-8

我想为ComboBox中的每个项目添加照片,因此当用户将鼠标悬停在ComboBox中该项目的名称上时,系统会显示该项目的照片。

这是我的代码:

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

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



    lights.setItems(FXCollections.observableArrayList
           (new Light ("Incandescent", 5.23),
            new Light ("Halogen", 5.75),
            new Light ("fluorescent",7.29),
            new Light ("Compact fluorescent bulbs",4.83),
            new Light ("LED",4.83)));


    lights.setPromptText("Please select a light");
    lights.setPrefWidth(100);


         lights.valueProperty().addListener((obs, oldVal, newVal) -> {
         String selectionText = "The price for the " + newVal.getName() + " light is : $" + newVal.getPrice();
       lightNamePrice.setText(selectionText);
    });

 private class Light {
    private String name;
    private Double price;

    private Double getPrice() {
        return price;
    }

    private String getName() {
        return name;
    }

    private Light(String name, Double price) {
        this.name = name;
        this.price = price;

    }

}

有人有任何想法吗?我在JavaFX上相当新,所以我能得到的任何帮助都会很棒。

1 个答案:

答案 0 :(得分:3)

您可以使用ComboBox的{​​{3}}方法自定义下拉列表中项目的呈现,以便返回setCellFactory Tooltip附加为ListCell的是Control的子类,因此它提供了ListCell方法。

示例

我已更新Light类,以使此类的每个实例都能拥有自己的图像:

private class Light {
    private String name;
    private Double price;
    private Image image;

    public Image getImage() {
        return image;
    }

    private Double getPrice() {
        return price;
    }

    private String getName() {
        return name;
    }

    private Light(String name, Double price, Image image) {
        this.name = name;
        this.price = price;
        this.image = image;
    }
}

然后我更新了填充数据的代码以使用(相同)Image

Image image =  new Image(getClass().getResource("light.png").toExternalForm(), 100, 100, true, true);

lights.setItems(FXCollections.observableArrayList
       (new Light ("Incandescent", 5.23, image),
        new Light ("Halogen", 5.75, image),
        new Light ("fluorescent",7.29, image),
        new Light ("Compact fluorescent bulbs",4.83, image),
        new Light ("LED",4.83, image)));

然后终于更新了细胞工厂:

lights.setCellFactory(param -> {
    return new ListCell<Light>() {

        @Override
        public void updateItem(Light item, boolean empty) {
            super.updateItem(item, empty);

            if (item != null) {
                setText(item.getName());

                // Add the Tooltip with the image of the current item
                Tooltip tt = new Tooltip();
                tt.setGraphic(new ImageView(item.getImage()));

                setTooltip(tt);
            } else {
                setText(null);
                setTooltip(null);
            }
        }
    };
});

结果:

setTooltip

您还可以查看有关图片工具提示的问题:enter image description here