Jose Ramon Jose Ramon - 8 months ago 88
Java Question

Update a gridPane in javafx

In my

project I have got a ComboBox with several values. When my application begins I do the following:

gui.course_P = new ComboBox<String>();
for (int i = 1; i < gui.columns.size(); i++) {

gui.createTestButtonPane.add(gui.course_P, 2, 1);

is a list of strings.

Meanwhile in the application this value can be changed. However since I have already add this comboBox to my GridPane and then to my scene when this value change (gui.columns) the new value did not displayed in the comboBox. Since I have already add the old gui.course. Is there a way to update the createTestButtonPane with the new gui.course_P?

EDIT: What I am trying to do is to add the value on comboBox on a listener and then add it again to the gridPane:

String temp1 = course_name.getText();
gui.course_P = new ComboBox<String>();
//gui.createTestButtonPane.add(gui.course_P, 2, 1);

The course is added properly to comboBox, but then when I add again the combobox to gridpane, I have as a result two comboBox. What I want is just to update the gui.course to my gridPane.

EDIT2: I followed the example of the answer post for my code.

//gui.course_P = new ComboBox<>(gui.columns);
gui.course_P = new ComboBox<String>();
for (int i = 1; i < gui.columns.size(); i++) {

The problem is that I want everything from the list except the 1st element.
When I am using
gui.course_P = new ComboBox<>(gui.columns);
instead of the for loop it is working but I have for some reason two times the elements of the list and I got also the first item. Can I add it without the first item?


This works fine for me. Whenever the button is pressed, a new item gets added to combobox. I just used an ObservavleList<String> as combobox's item property. There is no need for clearing or adding it again.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class ComboBoxTest extends Application {
    private ObservableList<String> source = FXCollections.observableArrayList();

    public void start(Stage primaryStage) {
        Pane root = createPane();
        primaryStage.setScene(new Scene(root));;

    private Pane createPane() {
        GridPane pane = new GridPane();
        Button addButton = new Button("add new item");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                source.add("Item" + source.size()); // editing the source

        ComboBox<String> comboBox = new ComboBox<>(source);

        pane.add(addButton, 1, 0);
        pane.add(comboBox, 1, 1);
        return pane;

    public static void main(String[] args) {

0 - Initial state:

enter image description here

1 - Button is pressed:

enter image description here

2 - Button is pressed again:

enter image description here