Display Rectangles in a ComboBox : ComboBox « JavaFX « Java






Display Rectangles in a ComboBox

 

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 300, Color.WHITE);

        GridPane gridpane = new GridPane();

        ComboBox<Rectangle> cmb = new ComboBox<Rectangle>();
        cmb.getItems().addAll(
            new Rectangle(10, 10, Color.RED), 
            new Rectangle(10, 10, Color.GREEN), 
            new Rectangle(10, 10, Color.BLUE));
        
        gridpane.add(cmb, 2, 0);
        
        root.getChildren().add(gridpane);   
        primaryStage.setScene(scene);

        primaryStage.show();
    }

}

   
  








Related examples in the same category

1.Make ComboBox editable
2.Using CellFactory to change the width of a ComboBox and text color
3.Set Prompt Text for ComboBox
4.Set and get value for ComboBox
5.Set value to null to clear the ComboBox
6.Add change listener to ComboBox valueProperty
7.custom cell factory