Eramol
Eramol

Reputation: 33

How to most efficiently move multiple rectangles in JavaFX

I have got a ListView which has a custom CellFactory defined.

These cell rows contain multiple rectangles which represent shop open hours, customers times etc. This gantt graph however must react to the horizontal scrolling. Every time the horizontal bar moves, all bars are relocated.

The problem is that it is very slow.

public class shopCellWorker extends ListCell<Shop> {
    ...
    public ShopCellWorker(ScrollBar hbar, List<CustomerVisit> visits) {
         this.hbar = hbar;
         visits.forEach(v -> {
             Rectangle r = new Rectangle();
             ... setup ...

             rlist.add(r);
         }
         hbar.valueProperty().addListener(new UpdateListener());

    }

    private class UpdateListener implements ChangeListener {

        @Override
        public void changed(ObservableValue observable, Object oldValue, Object newValue) {
            rlist.forEach(rec -> {
                ... do some simple math to determine the position ...
                relocate(x, 3);
            })
        }
    }
}

Is there some trick to make the rendering faster?

Upvotes: 0

Views: 656

Answers (1)

Dmytro Maslenko
Dmytro Maslenko

Reputation: 2287

Try to use Group.

Here just a draft version:

public class shopCellWorker extends ListCell<Shop> {
    Group rectGroup = new Group();
    public ShopCellWorker(ScrollBar hbar, List<CustomerVisit> visits) {
         visits.forEach(v -> {
             Rectangle r = new Rectangle();
             ... setup ...

             rectGroup.getChildren().add(r);
         }
         hbar.valueProperty().addListener(new UpdateListener());

    }

    private class UpdateListener implements ChangeListener {
        @Override
        public void changed(ObservableValue observable, Object oldValue, Object newValue) {
          // Have a calculated new possition here
          rectGroup.setTranslateX(calculatedNewXPos);
        }
    }
}

Upvotes: 1

Related Questions