2 Replies Latest reply: Jul 4, 2013 3:07 AM by acepsut RSS

    Show tooltip on color list

    acepsut

      Is it possibile to show a tooltip to display the color name?

       

      http://s10.postimg.org/94rd93scp/Z034.png

       

      Here is my code for ComboBox color rendering

       

      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);
           
            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));
           
          }
            static class ColorRectCell extends ListCell<String>{
                @Override
                public void updateItem(String item, boolean empty){
                    super.updateItem(item, empty);
                    Rectangle rect = new Rectangle(120,13);
                    rect.setStroke(Color.BLACK);
                    if(item != null){
                        rect.setFill(Color.web(item));
                        setGraphic(rect);
                }
              }
            }  
      
          public static void main(String[] args) {
              launch(args);
          }        
      }
      
      
      
      
      

       

      Thanks.