/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication4; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.ContentDisplay; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.layout.HBox; import javafx.scene.text.Font; import javafx.stage.Stage; import javafx.util.Callback; public class JavaApplication4 extends Application { final static int INITIAL_FONT_SIZE = 8; final static int MAX_FONT_SIZE = 36; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { final ComboBox cmb = new ComboBox(); cmb.setCellFactory(new Callback, ListCell>() { @Override public ListCell call(ListView param) { return new ListCell() { { setContentDisplay(ContentDisplay.TEXT_ONLY); } @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { setText(null); } else { setText(item); setFont(new Font(Double.valueOf(item))); } } }; } }); HBox root = new HBox(10f); Button btn = new Button("Populate ComboBox"); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { for (int i = INITIAL_FONT_SIZE; i <= MAX_FONT_SIZE; i += 2) { cmb.getItems().add(String.valueOf(i)); } } }); Button btnClear = new Button("Clear"); btnClear.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { cmb.getItems().clear(); } }); Button btnSetEditable = new Button("Set editable"); btnSetEditable.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { cmb.setEditable(true); } }); root.getChildren().addAll(cmb, btn, btnClear, btnSetEditable); Scene scene = new Scene(root, 500, 200); primaryStage.setScene(scene); primaryStage.show(); } }