0

Important disclaimer: I'm using fx that's packaged with JDK 8u202

I'm creating a TableView with about 100 TableColumns but when I scroll horizontally to the last TableColumn, that last TableColumn is clipped in half. (for reference, the 99th column is the last column) enter image description here

Here's a SSCCE of this:

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
    private static final int N_COLS = 100;
    private static final int N_ROWS = 1_000;

    public void start(Stage stage) throws Exception {
        TableView<ObservableList<String>> tableView = new TableView<>();

        // add columns
        for (int i = 0; i < N_COLS; i++) {
            final int finalIdx = i;
            TableColumn<ObservableList<String>, String> column = new TableColumn<>(String.valueOf(i));
            column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx)));
            tableView.getColumns().add(column);
            // column.setMinWidth(100);
        }

        // create rowData
        String[] rowData = new String[N_COLS];
        for (int i = 0; i < N_COLS; i++)
            rowData[i] = "Longggggg string";

        // add data to TableView
        for (int i = 0; i < N_ROWS; i++)
            tableView.getItems().add(FXCollections.observableArrayList(rowData));

        // Explicitly set PrefWidth for each TableColumn based on each column's header text, and every cell's width in that column
        // autoResizeColumns(tableView);

        Scene scene = new Scene(tableView, 800, 800);
        stage.setScene(scene);
        stage.show();
    }

    public static void autoResizeColumns( TableView<?> tableView )
    {
        //Set the right policy
        tableView.getColumns().forEach( (column) ->
        {
            Text t = new Text( column.getText() );
            double max = 0.0f;
            max = t.getLayoutBounds().getWidth();

            for ( int i = 0; i < tableView.getItems().size(); i++ )
            {
                //cell must not be empty
                if ( column.getCellData( i ) != null )
                {
                    t = new Text( column.getCellData( i ).toString() );
                    double calcwidth = t.getLayoutBounds().getWidth();
                    //remember new max-width
                    if ( calcwidth > max )
                        max = calcwidth;
                }
            }

            // set the new max-widht with some extra space
            column.setPrefWidth( max + 15.0d );
        } );
    }

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

I've tried explicitly setting the PrefWidth for each TableColumn based on the header's width, and the cell content's width and it appears that the width is indeed being updated, but the last TableColumn is still clipped in half. I did this by calling this method:

autoResizeColumns(tableView);

So far I've seen that setting each TableColumn's MinWidth to 100 fixes this bug. But I do not wish to use this method since I still prefer having each column's width being computed automatically given that some of the fields will be much shorter than these long string values I have here. This is the method I'm referring to:

column.setMinWidth(100);
Slaw
  • 37,820
  • 8
  • 53
  • 80
javaman
  • 13
  • 5
  • Does this help? [JavaFX 2 Automatic Column Width](https://stackoverflow.com/questions/10152828/javafx-2-automatic-column-width) – Abra Feb 02 '23 at 08:52
  • @Abra hmm yeah maybe I could try binding the last columns width property to a percentage of the TableViews width. I'll check it out – javaman Feb 02 '23 at 09:08
  • 3
    I know you're using Java 8u202, but thought I'd just point out that this bug does not appear to be present in JavaFX 19. – Slaw Feb 02 '23 at 10:04
  • 1
    It is just a waste of everybodies time to even think about potential bugs of such old software versions unless you have verified that they still persist in the most recent version. – mipa Feb 02 '23 at 10:41
  • it's likely a bug in fx column layout - there have been tons of fixes since fx8, if you insist/are forced to stick to that extremely outdated version, your best bet would be to look into the sources of the skin related classes and compare them to the current version (fx19). Note: in addition to those bug fixes, fx20 will contain considerately improved auto-sizing support :) – kleopatra Feb 02 '23 at 11:48

0 Answers0