水平滚动TableView的右列未完全显示

时间:2016-09-19 16:08:49

标签: java javafx scroll tableview

我有一个包含许多列的TableView,因此我的表可以水平滚动。如果我将单元格内容左对齐,它看起来像预期的那样:

see screenshot

将其更改为右对齐后,最后一列的末尾部分将被垂直滚动滑块覆盖。因此,并非所有内容都可见:

see screenshot

知道怎么解决吗?

TableView类:

class DataTableView {

private TableView <DataTableModel> dataTable = new TableView<>();

DataTableView(DataObject dataObject)
{
    dataTable.setId("dataTable");
    dataTable.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);

    populateTable(dataObject);
}

private void populateTable(DataObject dataObject)
{
    dataTable.getItems().clear();
    dataTable.getColumns().clear();
    dataTable.setPlaceholder(new Label("Loading..."));

    Task<Void> task = new Task<Void>()
    {
        @Override
        protected Void call() throws Exception
        {
            List <String>headerTexts = new ArrayList<>();

            dataObject.getColumnsDataTable().forEach(name ->
                                headerTexts.add(name.getName()+'\n'+name.getUnit()));

            Platform.runLater(new Runnable()
            {
                @Override
                public void run()
                {
                    dataTable.getColumns().add(createIndexColumn());

                    for (int columnIndex = 0; columnIndex < headerTexts.size(); columnIndex++)
                    {
                        dataTable.getColumns().add(
                                createColumn(columnIndex, headerTexts.get(columnIndex)));
                    }
                }
            });

            Platform.runLater(new Runnable()
            {
                @Override
                public void run()
                {
                    for(DataTableModel data : prepareDataModel(dataObject))
                    {
                        dataTable.getItems().add(data);
                    }
                }
            });
            return null;
        }
    };
    Thread thread = new Thread(task);
    thread.setDaemon(true);
    thread.start();
}

private TableColumn <DataTableModel,String>  createIndexColumn ()
{
    TableColumn<DataTableModel,String> column = new TableColumn< >("No");

    column.setSortable(false);
    column.setCellFactory(col ->
    {
        TableCell<DataTableModel, String> cell = new TableCell<>();
        cell.textProperty().bind(Bindings.when(cell.emptyProperty())
                .then("")
                .otherwise(cell.indexProperty().add(1).asString()));

        return cell ;
    });
    return column;
}

private TableColumn <DataTableModel, String> createColumn(final int columnIndex, String columnTitle )
{
    TableColumn<DataTableModel, String> column = new TableColumn<>(columnTitle);

    column.setCellValueFactory(cellData -> cellData.getValue().getRow().get(columnIndex));

    column.setSortable(false);

    return column;
}

private ObservableList <DataTableModel> prepareDataModel(DataObject dataObject)
{
    ObservableList <DataTableModel> dataTableModel = FXCollections.observableArrayList();
    dataObject.getDataTableData().forEach(row -> dataTableModel.add(new DataTableModel(row)));

    return dataTableModel;
}

public TableView getDataTableView (){
    return dataTable;
}

}

第二个屏幕截图的CSS:

#dataTable .table-cell {
    -fx-alignment: center-right;
    -fx-padding: 0 0 0 0;
}

表格型号:

public class DataTableModel {

    private ObservableList<StringProperty> row = FXCollections.observableArrayList();

    public DataTableModel(List <String> data)
    {
        data.forEach(element -> row.add(new SimpleStringProperty(element)));
    }

    public void setRow(ObservableList<StringProperty> row) {
        this.row = row;
    }

    public ObservableList<StringProperty> getRow() {
        return row;
    }

    public int size() {
        return row.size();
    }
}

0 个答案:

没有答案