简体   繁体   中英

Why is JavaFX tableView's content incomplete?

I'm trying to creating a JavaFX TableView with "tower-shape" columns. Some columns are covered and can not be seen like this:

滚动条已经到了最后

In the picture, you can see, the scroll bar has already been at the end, but there are still some columns can't be seen.

Here is my code:

import java.util.LinkedList;
import java.util.Queue;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class PaneDemo extends Application {

    private TableView<String> table = new TableView<>();

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

    private static int getTotal(int layer) {
        int total = 0;
        for(int i=1;i<=layer;i++) {
            int start = 1;
            for(int j=0;j<i;j++) {
                start *= 5;
            }
            total += start;
        }
        return total;
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());   
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setPrefWidth(900);
        anchorPane.setPrefHeight(600);
       table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
        TableColumn<String, String> Test1 = new TableColumn<>("Test1");
        TableColumn<String, String> Test2 = new TableColumn<>("Test2");
        TableColumn<String, String> Test3 = new TableColumn<>("Test3");
        TableColumn<String, String> Test4 = new TableColumn<>("Test4");
        TableColumn<String, String> Test5 = new TableColumn<>("Test5");
        Queue<TableColumn<String, ?>> queue = new LinkedList<>();
        table.getColumns().addAll(Test1, Test2, Test3, Test4, Test5);
        table.getItems().add("test");
        queue.addAll(table.getColumns());
        int index = 0;
        while(index < getTotal(3)) {
            TableColumn<String, ?> root = queue.poll();
            TableColumn<String, String> test1 = new TableColumn<>("test1");
            TableColumn<String, String> test2 = new TableColumn<>("test2");
            TableColumn<String, String> test3 = new TableColumn<>("test3");
            TableColumn<String, String> test4 = new TableColumn<>("test4");
            TableColumn<String, String> test5 = new TableColumn<>("test5");
            root.getColumns().addAll(test1, test2, test3, test4, test5);
            queue.addAll(root.getColumns());
            index++;
        }
        while(!queue.isEmpty()) {
            generateCellFactory((TableColumn<String, String>) queue.poll());
        }    
        table.prefHeightProperty().bind(anchorPane.heightProperty());
        table.prefWidthProperty().bind(anchorPane.widthProperty());
        anchorPane.getChildren().add(table);
        ((Group) scene.getRoot()).getChildren().addAll(anchorPane);
        stage.setScene(scene);
        stage.show();
    }

    private void generateCellFactory(TableColumn<String, String> column) {
        column.setCellFactory(cell -> {
            return new TableCell<String, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    setText("Test");
                }
            };
        });
    }
}

I finally find the answer. It happans when the column prefwidth is double or you didn't set prefwidth for last layer columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM