0

Forgive the horribly generic GridPane name... it'll be changed as soon as I get some input on this.

I'm adding items to a GridPane, it'll take a button, it'll take a label... it'll take pretty much anything but this int.

        int[] listObjA = {312,23,241};

        GridPane gridpane = new GridPane();

        gridpane.add(new Label("Item Listing"), 1, 1);
        gridpane.add(listObjA[0], 1, 2);

Of course, leaving it like that tells me "int cannot be converted to Node". Any advice? I'm mystified by how difficult it's been simply to print a variable in Java. By the way, it must be a variable because I'll have a method changing it continuously, so simply changing it to new Label("312") isn't an option.

3 Answers3

0

Elements of the array int[] listObjA = {312,23,241}; can't be resolved as a node type. Instead of using that , you could use it as

           Label firstLabel=new Label("312");
           Label secondLabel=new Label("23");
           Label thirdLabel=new Label("241");
           gridpane.add(firstLabel, x, y);
           gridpane.add(secondLabel, x,y);
           gridpane.add(thirdLabel, x, y);

where x , y is variable contains position wherever you want to insert Node.

Keno
  • 2,018
  • 1
  • 16
  • 26
manikant gautam
  • 3,521
  • 1
  • 17
  • 27
  • 1
    You can't add the same node multiple times to a scene graph. You presumably meant to change the variable in each `add(...)` call. – James_D Mar 26 '16 at 04:17
0

Java is a strongly typed language. You can't just pretend that an int is some kind of UI component.

Create a label for each element of the array:

int[] listObjA = {312,23,241};

GridPane gridpane = new GridPane();

gridpane.add(new Label("Item Listing"), 0, 0);

for (int i = 0 ; i < listObjA.length; i++) {
    Label label = new Label(Integer.toString(listObjA[i]));
    gridpane.add(label, i+1, 0);
}
James_D
  • 201,275
  • 16
  • 291
  • 322
0

If you need to change the text in the labels by assigning the values in your datastructure, you need a datastructure that supports notifying other parts of your program of changes. ObservableList would be one such datastructure. You can use the bindings API to change bind the text in the Labels to your ObservableList's contents:

ObservableList<Integer> observableList = FXCollections.observableList(Arrays.asList(312, 23, 241));

GridPane gridpane = new GridPane();

gridpane.add(new Label("Item Listing"), 0, 0);

for (int i = 0, size = observableList.size(); i < size; i++) {
    Label l = new Label();
    // bind text to content at list position
    l.textProperty().bind(Bindings.valueAt(observableList, i).asString());
    gridpane.add(l, 0, i + 1);
}

Creating the binding here means the content of the labels automatically gets updated, if the content of the list changes. E.g.

Scene scene = new Scene(gridpane);

scene.setOnMouseClicked(new EventHandler<MouseEvent>() {

    private int index;

    @Override
    public void handle(MouseEvent event) {
        int i = index++ % observableList.size();
        observableList.set(i, 10 + observableList.get(i));
    }
});

Increments one of the values by 10 on a mouse click, which the UI shows.

fabian
  • 80,457
  • 12
  • 86
  • 114