Tuesday 19 June 2018

JavaFX: Working with ComboBox widget

By using ComboBox class, you can create combo boxes. ComboBox class provides below constructors to define combo box widgets.

public ComboBox()
public ComboBox(ObservableList<T> items)

Ex
ComboBox<String> countriesComboBox = new ComboBox<>();
countriesComboBox.setItems(countries);

ComboBox<String> hobbiesComboBox = new ComboBox<>(hobbies);

Find the below working application.

ComboBoxApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ComboBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws Exception {
  ObservableList<String> countries = FXCollections.observableArrayList("India", "Japan", "Germany");
  ObservableList<String> hobbies = FXCollections.observableArrayList("Tennis", "Football", "Cricket");

  ComboBox<String> countriesComboBox = new ComboBox<>();
  countriesComboBox.setItems(countries);

  ComboBox<String> hobbiesComboBox = new ComboBox<>(hobbies);

  HBox hBox = new HBox(10, countriesComboBox, hobbiesComboBox);

  primaryStage.setScene(new Scene(hBox));
  primaryStage.setTitle("Combo Box Example");
  primaryStage.setWidth(900);
  primaryStage.setHeight(500);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ComboBoxApp.class, args);
 }
}




      ComboBox: Add items to the combo box dynamically
      Restrict the number of visible rows
      ComboBox: Set a value to the combo box
      Editable combo boxes
      ComboBox: Set the prompt text of combo box
      ComboBox: Event handling
Previous                                                 Next                                                 Home

No comments:

Post a Comment