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

Setting TableView column width using only CSS doesn't work properly

XMLWordPrintable

    • generic
    • generic

      Edit 2024/05/20: it is yet unclear whether we should add this capability to TableColumn. The original report is not a bug.

      A DESCRIPTION OF THE PROBLEM :
      When TableColumn width is set only using CSS it gives wrong results.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the provided code. You will see this result https://i.stack.imgur.com/atAvA.png . So, when column width is set using Java code then everything seems to be OK. However, if you try to set the width using only CSS: markColumn.setStyle("-fx-min-width: 200; -fx-max-width: 200;"); then you will get this result: https://i.stack.imgur.com/nJKwV.png . So, when only CSS is used then column width is not set properly.

      Moreover, if you try this CSS style: markColumn.setStyle("-fx-min-width: 200; -fx-max-width: 200; -fx-background-color:yellow; -fx-pref-width:200"); and after that try to resize column you will get this result:
      https://i.stack.imgur.com/M2czD.png

      This problem was also discussed here https://stackoverflow.com/questions/78303658/how-to-set-tableview-column-width-using-css-in-javafx


      ---------- BEGIN SOURCE ----------
      public class JavaFxTest7 extends Application {

          private static class Student {

              private int id;

              private int mark;

              public Student(int id, int mark) {
                  this.id = id;
                  this.mark = mark;
              }

              public int getId() {
                  return id;
              }

              public int getMark() {
                  return mark;
              }
          }

          private TableView<Student> table = new TableView<>(FXCollections.observableList(
                  List.of(new Student(1, 3), new Student(2, 4), new Student(3, 5))));

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

          @Override
          public void start(Stage primaryStage) {
              var idColumn = new TableColumn<Student, Integer>();
              idColumn.setCellValueFactory((data) -> new ReadOnlyObjectWrapper<>(data.getValue().getId()));
              var markColumn = new TableColumn<Student, Integer>();
              markColumn.setCellValueFactory((data) -> new ReadOnlyObjectWrapper<>(data.getValue().getMark()));
              markColumn.setMaxWidth(200);//this code works
              markColumn.setMinWidth(200);//this code works
              //markColumn.setStyle("-fx-min-width: 200; -fx-max-width: 200;");//this code doesn't work
              table.getColumns().addAll(idColumn, markColumn);

              VBox root = new VBox(table);
              var scene = new Scene(root, 400, 300);
              primaryStage.setScene(scene);
              primaryStage.show();

          }

      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. without_css.png
          without_css.png
          9 kB
        2. with_css.png
          with_css.png
          9 kB
        3. JavaFxTest7.java
          2 kB

            angorya Andy Goryachev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: