Do not Scroll TableView When Adding More Items












2















I would like to add items to a table dynamically.
I do it like this:



public class TestApp extends Application {

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

private final AtomicLong counter = new AtomicLong();

@Override
public void start(final Stage primaryStage) {

final VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

final TableView<String> tableView = new TableView<>();
final TableColumn<String, String> column = new TableColumn<>("Text");
column.setCellValueFactory(f -> new SimpleStringProperty(f.getValue()));

tableView.getColumns().add(column);

// Add some sample items to our TableView
for (int i = 0; i < 100; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}

final Button button = new Button("Add items");
final long oldElement = counter.get();
button.setOnAction(e -> {
// Add more elements
for (int i = 0; i < 10; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
tableView.scrollTo("Item #" + oldElement);
});

root.getChildren().add(button);
root.getChildren().add(tableView);

// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}


(Example taken from here)



What I would like to change now, is, that when the table is already scrolled down completely, and I add more items, the table automatically scrolls down.



The 'scroll-state' seems to be maintained, which is why I still see the very bottom of the table (and the new elements).



How can I add items to the table but keeping the scroll at the current position / current row?



The 'scrollTo' works, but moves the last element from the bottom of the view to the top, which is also a litte irritating.










share|improve this question




















  • 1





    Perhaps this similar question will lead you in the right direction.

    – Zephyr
    Jan 1 at 17:01
















2















I would like to add items to a table dynamically.
I do it like this:



public class TestApp extends Application {

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

private final AtomicLong counter = new AtomicLong();

@Override
public void start(final Stage primaryStage) {

final VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

final TableView<String> tableView = new TableView<>();
final TableColumn<String, String> column = new TableColumn<>("Text");
column.setCellValueFactory(f -> new SimpleStringProperty(f.getValue()));

tableView.getColumns().add(column);

// Add some sample items to our TableView
for (int i = 0; i < 100; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}

final Button button = new Button("Add items");
final long oldElement = counter.get();
button.setOnAction(e -> {
// Add more elements
for (int i = 0; i < 10; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
tableView.scrollTo("Item #" + oldElement);
});

root.getChildren().add(button);
root.getChildren().add(tableView);

// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}


(Example taken from here)



What I would like to change now, is, that when the table is already scrolled down completely, and I add more items, the table automatically scrolls down.



The 'scroll-state' seems to be maintained, which is why I still see the very bottom of the table (and the new elements).



How can I add items to the table but keeping the scroll at the current position / current row?



The 'scrollTo' works, but moves the last element from the bottom of the view to the top, which is also a litte irritating.










share|improve this question




















  • 1





    Perhaps this similar question will lead you in the right direction.

    – Zephyr
    Jan 1 at 17:01














2












2








2


2






I would like to add items to a table dynamically.
I do it like this:



public class TestApp extends Application {

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

private final AtomicLong counter = new AtomicLong();

@Override
public void start(final Stage primaryStage) {

final VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

final TableView<String> tableView = new TableView<>();
final TableColumn<String, String> column = new TableColumn<>("Text");
column.setCellValueFactory(f -> new SimpleStringProperty(f.getValue()));

tableView.getColumns().add(column);

// Add some sample items to our TableView
for (int i = 0; i < 100; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}

final Button button = new Button("Add items");
final long oldElement = counter.get();
button.setOnAction(e -> {
// Add more elements
for (int i = 0; i < 10; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
tableView.scrollTo("Item #" + oldElement);
});

root.getChildren().add(button);
root.getChildren().add(tableView);

// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}


(Example taken from here)



What I would like to change now, is, that when the table is already scrolled down completely, and I add more items, the table automatically scrolls down.



The 'scroll-state' seems to be maintained, which is why I still see the very bottom of the table (and the new elements).



How can I add items to the table but keeping the scroll at the current position / current row?



The 'scrollTo' works, but moves the last element from the bottom of the view to the top, which is also a litte irritating.










share|improve this question
















I would like to add items to a table dynamically.
I do it like this:



public class TestApp extends Application {

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

private final AtomicLong counter = new AtomicLong();

@Override
public void start(final Stage primaryStage) {

final VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

final TableView<String> tableView = new TableView<>();
final TableColumn<String, String> column = new TableColumn<>("Text");
column.setCellValueFactory(f -> new SimpleStringProperty(f.getValue()));

tableView.getColumns().add(column);

// Add some sample items to our TableView
for (int i = 0; i < 100; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}

final Button button = new Button("Add items");
final long oldElement = counter.get();
button.setOnAction(e -> {
// Add more elements
for (int i = 0; i < 10; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
tableView.scrollTo("Item #" + oldElement);
});

root.getChildren().add(button);
root.getChildren().add(tableView);

// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}


(Example taken from here)



What I would like to change now, is, that when the table is already scrolled down completely, and I add more items, the table automatically scrolls down.



The 'scroll-state' seems to be maintained, which is why I still see the very bottom of the table (and the new elements).



How can I add items to the table but keeping the scroll at the current position / current row?



The 'scrollTo' works, but moves the last element from the bottom of the view to the top, which is also a litte irritating.







java javafx tableview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 12:04







kerner1000

















asked Jan 1 at 11:56









kerner1000kerner1000

1,2231529




1,2231529








  • 1





    Perhaps this similar question will lead you in the right direction.

    – Zephyr
    Jan 1 at 17:01














  • 1





    Perhaps this similar question will lead you in the right direction.

    – Zephyr
    Jan 1 at 17:01








1




1





Perhaps this similar question will lead you in the right direction.

– Zephyr
Jan 1 at 17:01





Perhaps this similar question will lead you in the right direction.

– Zephyr
Jan 1 at 17:01












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995223%2fdo-not-scroll-tableview-when-adding-more-items%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995223%2fdo-not-scroll-tableview-when-adding-more-items%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter