A.Sharma A.Sharma - 3 months ago 8
Java Question

Increase Height of TableRow on Drag

I'm working on a JavaFX app via SceneBuilder and I'm trying to work with adding functionality to the TableView. My current task is to allow the user to drag and increase the height of each TableRow on drag. Essentially, I'm trying to make it function like Excel.

Java Code

tv.setRowFactory(t -> {

TableRow tr = new TableRow();
tr.setPrefHeight(54);

tr.setOnMouseDragged(event -> {
tr.setMinHeight(event.getY());
});

return tr;

});


I tested the getY() function on drag and saw that it was iteratively increasing as I moved down on the screen.

I tried searching for a way to do this, but I couldn't find any leads on this. If you have a link to some documentation on the methods that are required, that would be great as well.

Also, if SceneBuilder has a built in way to allow for this functionality in a TableView, that may work as well.

Answer

Try the following:

table.setRowFactory(t -> {
            final TableRow tr = new TableRow();
            final double prefHeight = 54;
            tr.setPrefHeight(prefHeight);

            tr.setOnMouseDragged(event -> {
                final double newHeight = event.getY();
                if(newHeight < prefHeight) {
                    return;
                }
                tr.setPrefHeight(newHeight);
                tr.autosize();
            });

            return tr;

        });
Comments