Wednesday 20 June 2018

ComboBox: Restrict the number of visible rows

By using the method 'setVisibleRowCount', you can set the number of visible rows of combo box at any point of time.

Ex
hobbiesComboBox.setVisibleRowCount(3);
Above statement sets the maximum number of rows visible to 3.

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);

  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