How to dynamically add data to the tableView only in the last rows without disturbing other rows with observavbleList in javafx?

StackOverflow https://stackoverflow.com/questions/22581775

Pergunta

In nutshell I want to add the row silently at the end of the table without affecting other rows. This is my controller class code excerpt to add entries to the table. The problem is when I add an item to the sourceTree, instead of adding a row at the end silently it disturbs the complete table. Suppose there were already some entries in the table and the user expands one of the titled pane in the table, now when a new row will be added to the table, complete table will blink and this titled pane will automatically shrink. This blinking led me to conclude that instead of adding an entry whole table is refreshing probably. Please help...

 @FXML
public static TableView<NodeInfo> tableView;
@FXML
public static TableColumn<NodeInfo, TitledPane> nodeTree;
@FXML
private TableColumn<NodeInfo, String> name;
@FXML
private TableColumn<NodeInfo, CheckBox> favourite;
@FXML
private TableColumn<NodeInfo, Button> updates;
@FXML
public static ObservableList<NodeInfo> sourceTree = FXCollections.observableArrayList();

    tableView.setPlaceholder(new Label("You have no Nodes in the network at the moment!!!"));
    nodeTree.setCellValueFactory(new PropertyValueFactory<NodeInfo, TitledPane>("TitledPaneNode"));
    nodeTree.prefWidthProperty().bind(tableView.widthProperty().divide(3));
    name.setCellValueFactory(new PropertyValueFactory<NodeInfo, String>("name"));
    name.prefWidthProperty().bind(tableView.widthProperty().divide(3));
    favourite.setCellValueFactory(new PropertyValueFactory<NodeInfo, CheckBox>("favourite"));
    favourite.prefWidthProperty().bind(tableView.widthProperty().divide(6));
    updates.setCellValueFactory(new PropertyValueFactory<NodeInfo, Button>("NoOfUpdates"));
    updates.prefWidthProperty().bind(tableView.widthProperty().divide(6));

    tableView.setItems(sourceTree);
Foi útil?

Solução

The following is a SSCCE code I have simulated your use case. At the result there was no table disturbing, or pane shrinking. Test it yourself and compare it with yours:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TitledPane;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class PaneDemo extends Application {

    private TableView<NodeInfo> table = new TableView<NodeInfo>();
    private final ObservableList<NodeInfo> data = FXCollections.observableArrayList();

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

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setWidth(450);
        stage.setHeight(500);

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<NodeInfo, String>("firstName"));

        TableColumn paneCol = new TableColumn("Pane");
        paneCol.setMinWidth(100);
        paneCol.setCellValueFactory(new PropertyValueFactory<NodeInfo, TitledPane>("titledPane"));

        for (int i = 0; i < 5; i++) {
            TitledPane pane = new TitledPane("title " + i, new Text("text " + i));
            data.add(new NodeInfo("name " + i, pane));
        }

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, paneCol);

        Button btn = new Button("add new item");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                TitledPane pane = new TitledPane("title new", new Text("text new"));
                data.add(new NodeInfo("name new", pane));
            }
        });

        final VBox vbox = new VBox(20);
        vbox.getChildren().addAll(table, btn);
        ((Group) scene.getRoot()).getChildren().addAll(vbox);
        stage.setScene(scene);
        stage.show();
    }

    public static class NodeInfo {
        private final SimpleStringProperty firstName;
        private TitledPane titledPane;

        private NodeInfo(String fName, TitledPane titledPane) {
            this.firstName = new SimpleStringProperty(fName);
            this.titledPane = titledPane;
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public TitledPane getTitledPane() {
            return titledPane;
        }

        public void setTitledPane(TitledPane fName) {
            titledPane = fName;
        }
    }

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top