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

TableColumHeader: initial auto-size broken if has graphic

XMLWordPrintable

    • b14

      Happens, if the column has a graphic. To reproduce, run the example below and see the column's text truncated. Double-click into the resize-region and watch it getting wider, text no longer truncated.

      The reason probably is the same that resulted in the crappy code in TableSkinUtils.resizeColumnToFitContent:

      // c&p from core

              TableColumnHeader header = tableSkin.getTableHeaderRow().getColumnHeaderFor(tc);
              // wrong: calculate width requirement manually
              double headerTextWidth = Utils.computeTextWidth(header.label.getFont(), tc.getText(), -1);
              Node graphic = header.label.getGraphic();
             // wrong: asymetric handling of text vs graphic
              double headerGraphicWidth = graphic == null ? 0 : graphic.prefWidth(-1) + header.label.getGraphicTextGap();

      Had been wondering about the manual calc - after all a Label knows exactly what it needs, right? - and what's different for the graphics - here it uses the prefWidth, no chance to do differently as there's no knowledge, right? - until I noticed that the initial auto-size happens at a time when the label's skin has not yet been applied.

      A fix would have to move the auto-size effort to a time when the skin has been set. Listening to the label's skinProperty (vs. listening to the header's sceneProperty) might do the trick.


      The example:

      import java.util.Locale;
      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      /**
       * TableColumn: auto-size broken when column has graphic
       */
      public class TableColumnGraphicSizeBug extends Application {

          private Parent getContent() {
              TableView<Locale> table = new TableView<>(FXCollections.observableArrayList(
                      Locale.getAvailableLocales()));
              TableColumn<Locale, String> countryCode = new TableColumn<>("Code");
              countryCode.setCellValueFactory(new PropertyValueFactory<>("country"));
              // arbitrary graphic
              countryCode.setGraphic(new Button("X"));
              table.getColumns().addAll(countryCode);
              BorderPane pane = new BorderPane(table);
              return pane;
          }

          @Override
          public void start(Stage primaryStage) throws Exception {
              primaryStage.setScene(new Scene(getContent(), 800, 400));
              primaryStage.show();
          }
          
          public static void main(String[] args) {
              launch(args);
          }

      }

            mhanl Marius Hanl
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: