Wednesday, 20 June 2018

JavaFX: Editable combo boxes

ComboBox widgets are not editable by default. By setting the editable property to 'true', you can make the combo box editable.

Ex
ObservableList<String> hobbies = FXCollections.observableArrayList("Tennis", "Football", "Cricket", "CoCO", "Rugby", "kabaddy");
ComboBox<String> hobbiesComboBox = new ComboBox<>(hobbies);
hobbiesComboBox.setEditable(true);

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> hobbies = FXCollections.observableArrayList("Tennis", "Football", "Cricket", "CoCO",
    "Rugby", "kabaddy");
  ComboBox<String> hobbiesComboBox = new ComboBox<>(hobbies);
  hobbiesComboBox.setVisibleRowCount(3);
  hobbiesComboBox.setValue("Cricket");
  hobbiesComboBox.setEditable(true);

  HBox hBox = new HBox(10, 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);
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment