Important disclaimer: I'm using fx that's packaged with JDK 8u202
I'm creating a TableView
with about 100 TableColumn
s 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)
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);