Update gridPane in javafx

In my project javafx, I have a ComboBox with several values. When my application starts, I do the following:

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

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

Where gui.columnsis a list of lines.

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

EDIT: What I'm trying to do is add a value in comboBox to the listener, and then add it again to gridPane:

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

comboBox, , combobox gridpane, comboBox. gui.course gridPane.

EDIT2. .

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

, , 1- . gui.course_P = new ComboBox<>(gui.columns); for, , - , . ?

+4
1

. , , combobox . ObservavleList<String> combobox. .

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

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

    private Pane createPane() {
        GridPane pane = new GridPane();
        Button addButton = new Button("add new item");
        addButton.setPrefWidth(150);
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            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);
        pane.setPrefHeight(200);
        pane.setPrefWidth(200);
        return pane;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

0 - :

enter image description here

1 - :

enter image description here

2 - :

enter image description here

+2

Source: https://habr.com/ru/post/1661034/


All Articles