Wednesday, May 29, 2013

Simple example of JavaFX ComboBox

Simple example of JavaFX ComboBox
Simple example of JavaFX ComboBox


package javafxcombobox;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXComboBox extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        final ComboBox comboBox = new ComboBox();
        comboBox.getItems().addAll(
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4");
        comboBox.setValue("Item 1");
        
        final Label label = new Label();
        
        Button btn = new Button();
        btn.setText("Read comboBox");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                label.setText("selectd: " + comboBox.getValue());
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(label, comboBox, btn);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


Next: JavaFX ComboBox for custom object

No comments:

Post a Comment