0

按照 Oracle 的 ListView 示例,我已经能够获得一个 ComboBox 颜色渲染颜色列表

在此处输入图像描述

我的问题是,一旦选择了一种颜色,ComboBox 就会显示字符串名称,而我想显示颜色本身,不管有没有颜色名称。

如何更改此代码以显示所选颜色?

谢谢大家。

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.ContentDisplay;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Callback;


public class ProvaComboRendering extends Application {

@Override
public void start(Stage primaryStage) {


    StackPane root = new StackPane();
    ComboBox<String> cb = new ComboBox<String>();

    cb.setPrefSize(150, 20);
    root.getChildren().add(cb);


    Scene scene = new Scene(root, 300, 250);

    primaryStage.setScene(scene);
    primaryStage.show();

ObservableList<String> data = FXCollections.observableArrayList(
        "chocolate", "salmon", "gold", "coral", "darkorchid",
        "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
        "blueviolet", "brown");

  cb.setItems(data);

  cb.setCellFactory(new Callback<ListView<String>, ListCell<String>>(){
      @Override
      public ListCell<String> call(ListView<String> list){
          return new ColorRectCell();
      }

  });
}
  static class ColorRectCell extends ListCell<String>{
      @Override
      public void updateItem(String item, boolean empty){
          super.updateItem(item, empty);
          Rectangle rect = new Rectangle(120,18);
          if(item != null){
              rect.setFill(Color.web(item));
              setGraphic(rect);
      }
  }
  }   

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

编辑:当鼠标指针位于颜色列表之一上时,是否可以显示显示颜色名称的工具提示?

在此处输入图像描述

4

1 回答 1

2

使用 ComboBox 的按钮单元格属性。您可以使用相同的 cellfactory:

@Override
public void start(Stage primaryStage) {
    StackPane root = new StackPane();
    ComboBox<String> cb = new ComboBox<String>();

    cb.setPrefSize(150, 20);
    root.getChildren().add(cb);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setScene(scene);
    primaryStage.show();

    ObservableList<String> data = FXCollections.observableArrayList(
            "chocolate", "salmon", "gold", "coral", "darkorchid",
            "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
            "blueviolet", "brown");

    cb.setItems(data);

    Callback<ListView<String>, ListCell<String>> factory = new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> list) {
            return new ColorRectCell();
        }
    };

    cb.setCellFactory(factory);
    cb.setButtonCell(factory.call(null));

}

或为纽扣电池定义一个新的电池工厂,如下所示,它添加了一个 tooltipablity :

@Override
public void start(Stage primaryStage) {
    StackPane root = new StackPane();
    ComboBox<String> cb = new ComboBox<String>();

    cb.setPrefSize(150, 20);
    root.getChildren().add(cb);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setScene(scene);
    primaryStage.show();

    ObservableList<String> data = FXCollections.observableArrayList(
            "chocolate", "salmon", "gold", "coral", "darkorchid",
            "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
            "blueviolet", "brown");

    cb.setItems(data);

    Callback<ListView<String>, ListCell<String>> factory = new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> list) {
            return new ColorRectCell();
        }
    };
    cb.setCellFactory(factory);

    Callback<ListView<String>, ListCell<String>> factoryTooltip = new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> list) {
            return new ColorRectTooltipCell();
        }
    };
    cb.setButtonCell(factoryTooltip.call(null));

}


static class ColorRectTooltipCell extends ColorRectCell {
    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (item != null) {
            Tooltip.install(this.getParent(), new Tooltip(item));
        }
    }
}
于 2013-07-03T12:29:17.233 回答