0

I'm trying to insert a row into a grid pane.

How can I achieve this?

Further reading/information/effots:

I've been able to append the row with this:

    add.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            Peak thisPeak = new Peak();
            MenuPeak dispPeak = new MenuPeak(thisPeak);
            int row = 1+thisPeak.peakCount();
            //psMenu.addRow(1+row);
            psMenu.add(dispPeak.name,1,row,1,1);
            psMenu.add(dispPeak.retTime,2,row,1,1);
            psMenu.add(dispPeak.tol,3,row,1,1);
        }

    });

Which takes my original menu layout from

Start

to

enter image description here

I tried googling, some people suggested using an addRow. I've tried a few variants (currently one is commented out in the snippet I provided.

This ends up just overwriting my buttons as seen:

enter image description here

I am ultimately wanting it to insert the row between the buttons as such (hard-coded the peak line):

enter image description here

Perhaps there is a better solution, but I found some success by re-adding the element buttons again, but this is producing some runtime errors yet is working?

    add.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            Peak thisPeak = new Peak();
            MenuPeak dispPeak = new MenuPeak(thisPeak);
            int row = thisPeak.peakCount();
            psMenu.addRow(1+row);
            psMenu.add(dispPeak.name,1,row,1,1);
            psMenu.add(dispPeak.retTime,2,row,1,1);
            psMenu.add(dispPeak.tol,3,row,1,1);
            row++;
            psMenu.add(save,1,row,1,1);
            psMenu.add(cancel,2,row,1,1);
            psMenu.add(okay,3,row,1,1);
        }

    }); 

Which did make this

enter image description here

But again, the run time error complained about adding duplicate children.

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentExcepti on: Children: duplicate children added: parent = Grid hgap=10.0, vgap=10.0, alig nment=TOP_CENTER at javafx.scene.Parent$2.onProposedChange(Parent.java:454) at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDeco rator.java:206)

EDIT: The answer is correct and the exact implementation of my correction is:

    add.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            Peak thisPeak = new Peak();
            MenuPeak dispPeak = new MenuPeak(thisPeak);
            int row = 1+thisPeak.peakCount();

            psMenu.setRowIndex(save,row);
            psMenu.setRowIndex(cancel,row);
            psMenu.setRowIndex(okay,row);

            psMenu.addRow(row-1, dispPeak.name, dispPeak.retTime, dispPeak.tol);

        }

    });
Chemistpp
  • 2,006
  • 2
  • 28
  • 48

1 Answers1

1

You're adding the save, cancel and ok buttons although they are already children of the GridPane. Instead just adjust the row of those children. Also addRow adds nodes to a row. It does not "insert" a new row. If you do not pass any nodes to the varargs parameter, the method simply does nothing (If you'd start the Nodes of the peak in the first column, you could e.g. use psMenu.addRow(row, dispPeak.name, dispPeak.retTime, dispPeak.tol);):

@Override
public void handle(ActionEvent event) {

    Peak thisPeak = new Peak();
    MenuPeak dispPeak = new MenuPeak(thisPeak);
    int row = thisPeak.peakCount();

    // add new nodes
    psMenu.add(dispPeak.name,1,row);
    psMenu.add(dispPeak.retTime,2,row);
    psMenu.add(dispPeak.tol,3,row);
    row++;

    // adjust row of buttons
    GridPane.setRowIndex(save, row);
    GridPane.setRowIndex(cancel, row);
    GridPane.setRowIndex(okay, row);
}
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Wow!! I just figured this out too. I'm accepting this answer! Thank you! I was playing around a lot with it. I found I got better behavior by setting the row index before adding the new items, just FYI. I was using addrow to add my new peak and it was inserting after the buttons in the row – Chemistpp Nov 22 '16 at 18:27