The code below displays a TableView with 20 columns. The columns don't fit on the screen so I would expect a horizontal scroll bar to be displayed. It is not. If I add some items to the TableView then the scroll bar is displayed as expected.
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class ManyColumnsTest extends Application {
@Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
int numColumns = 20;
TableView<String[]> table = new TableView<>();
for (int i = 0; i < numColumns; i++) {
final int column = i;
TableColumn<String[], String> childColumn = new TableColumn<>("Child Column " + i);
childColumn.setCellValueFactory(stringCellDataFeatures -> new ReadOnlyStringWrapper(stringCellDataFeatures.getValue()[column]));
table.getColumns().add(childColumn);
}
List<String[]> data = new ArrayList<>(5);
for (int j = 0; j < 5; j++) {
String[] row = new String[numColumns];
for (int i = 0; i < numColumns; i++) {
row[i] = "(" + j + ", " + i + ")";
}
data.add(row);
}
// table.setItems(FXCollections.observableArrayList(data));
pane.setCenter(table);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class ManyColumnsTest extends Application {
@Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
int numColumns = 20;
TableView<String[]> table = new TableView<>();
for (int i = 0; i < numColumns; i++) {
final int column = i;
TableColumn<String[], String> childColumn = new TableColumn<>("Child Column " + i);
childColumn.setCellValueFactory(stringCellDataFeatures -> new ReadOnlyStringWrapper(stringCellDataFeatures.getValue()[column]));
table.getColumns().add(childColumn);
}
List<String[]> data = new ArrayList<>(5);
for (int j = 0; j < 5; j++) {
String[] row = new String[numColumns];
for (int i = 0; i < numColumns; i++) {
row[i] = "(" + j + ", " + i + ")";
}
data.add(row);
}
// table.setItems(FXCollections.observableArrayList(data));
pane.setCenter(table);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
- duplicates
-
JDK-8089225 TableView does not show horizontal scrollbar when there is no data
-
- Open
-