JavaFx 2 - 在ComboBox中显示颜色

时间:2013-07-03 12:08:25

标签: colors combobox javafx-2 render

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

enter image description here

我的问题是,一旦选择了颜色,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);
}         
}

编辑:当鼠标指针悬停在某个颜色列表上时,是否可以显示工具提示替换颜色名称?

enter image description here

1 个答案:

答案 0 :(得分: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));

}

或为按钮单元格定义一个新的单元格工厂,如下所示,它增加了一个工具可用性:

@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));
        }
    }
}
相关问题