- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to edit a comboBox in JavaFX?
A combo box is similar to a choice box it holds multiple items and, allows you to select one of them. It can be formed by adding scrolling to a drop-down list. You can create a combo box by instantiating the javafx.scene.control.ComboBox class.
The ComboBox class has a method known as editable (boolean), which specifies whether the current Combobox allows user input. You can set the value to this property using the setEditable() method.
Therefore, to edit a JavaFX ComboBox invoke the setEditable() method on it by passing the boolean value true as a parameter.
Example
import javafx.application.Application; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.stage.Stage; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; public class ComboBoxEditable extends Application { @Override public void start(Stage stage) { //Setting the label Label label = new Label("Select Desired Course:"); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); label.setFont(font); //Creating a combo box ComboBox<String> combo = new ComboBox<String>(); //Getting the observable list of the combo box ObservableList<String> list = combo.getItems(); //Adding items to the combo box list.add("Java"); list.add("C++"); list.add("Python"); list.add("Big Data"); list.add("Machine Learning"); //Setting the combo box editable combo.setEditable(true); HBox hbox = new HBox(15); //Setting the space between the nodes of a HBox pane hbox.setPadding(new Insets(75, 150, 50, 60)); hbox.getChildren().addAll(label, combo); //Creating a scene object Scene scene = new Scene(new Group(hbox), 595, 280, Color.BEIGE); stage.setTitle("Combo Box"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
Advertisements