Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8090002

[TableView] updates on StringProperties are not reflected if they contain line breaks

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u20
    • javafx
    • Windows 7, JDK 8u20

    • Cause Known

      Updates on StringProperties are not reflected in the tableView if they contain line breaks. For example if a StringProperty contains "ABC" and one updates the property to "ABC\nNewLine" only "ABC" is shown in the column. Similar if a StringProperty contains "ABC" and one updates the property to "ABC123\nNewLine" only "ABC123" is shown. My guess is that the row is never resized appropriately if a StringProperty has text after a new line.

      Current workaround is to call (after each update):
      tableView.getColumns().get(0).setVisible(false);
      tableView.getColumns().get(0).setVisible(true);

      I've attached a SSCCE. To reproduce the bug, select a row, add a line break and some text in the textArea and hit "Change comment".

      package tableviewnewlinebug;

      import javafx.application.Application;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.event.ActionEvent;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.SelectionMode;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.TextArea;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.Pane;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      /**
       *
       * @author Kacper
       */
      public class TableViewNewLineBug extends Application {
          private Comment lastSelectedComment;
          
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              launch(args);
          }
          
          @Override
          public void start(Stage primaryStage) {
              TableView<Comment> tableView = new TableView<>();
              TableColumn<Comment, String> column1 = new TableColumn<>();
              TextArea commentTextArea = new TextArea();
              ObservableList<Comment> comments = FXCollections.observableArrayList();
          
              Button btn = new Button();
              btn.setText("Change comment");
              btn.setOnAction((ActionEvent event) -> {
                  if (lastSelectedComment != null) {
                      lastSelectedComment.commentProperty().setValue(commentTextArea.getText());
                  
                      /* workaround
                          tableView.getColumns().get(0).setVisible(false);
                          tableView.getColumns().get(0).setVisible(true);
                      */
                  }
              });
              
              Pane root = new VBox();
              root.getChildren().addAll(commentTextArea, btn, tableView);
              
              Scene scene = new Scene(root, 800, 600);
              
              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();
              
              Comment comment1 = new Comment();
              comment1.comment.setValue("ABC");
              
              Comment comment2 = new Comment();
              comment2.comment.setValue("123");
              
              comments.addAll(comment1, comment2);
              tableView.setItems(comments);
              
              tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
              
              tableView.getColumns().add(column1);
              column1.setCellValueFactory(new PropertyValueFactory<>("comment"));
              
              tableView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
              tableView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
                  if (newValue == null) {
                      return;
                  }
                  
                  commentTextArea.setText(newValue.commentProperty().getValue());
                  lastSelectedComment = newValue;
              });
          }
          
          public class Comment {
              private StringProperty comment = new SimpleStringProperty();
              
              public StringProperty commentProperty() {
                  return this.comment;
              }
          }
      }

            Unassigned Unassigned
            kacperjfx Kacper (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Imported: