最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaFX TableView - Scroll Bars and auto resized TableColumns - Stack Overflow

programmeradmin2浏览0评论

I am trying to have a TableView with auto resized columns and scroll bars.

The TableView is in my FXML file, and I don't have it wrapped in any pane. I have read that TableView should automatically add scroll bars as needed, and you don't need it within a ScrollPane. I would also like the TableColumns to size the width as the headers (1st row) is added (doesn't have to resize to data, once that is added later).

This is my TableView, on a Tab, in my FXML file:

        <Tab text="Prefill PDFs from Excel Template">
          <content>
                <TableView fx:id="tvReadExcelFile1" prefHeight="515.0" prefWidth="1004.0" />
          </content>
        </Tab>

Here is where I populate the TableView with the column headers (from excel file). Works as expected but TableView doesn't automatically add Scroll bars, and I can't seem to get the width of the columns to autosize. You will see a ScrollPane being referenced, but that is for a TextFlow control.

protected void handleOpenPrefillTemplate(ActionEvent actionEvent) throws IOException, InvalidFormatException {
int nColCount;
int nRowCount;

tvReadExcelFile1.getItems().clear();
tvReadExcelFile1.getColumns().clear();

File filePrefillTemplate = XT4FileDialog.handleOpenFileDialog("Excel File", "*.xlsx");

if (filePrefillTemplate == null) {
    Text txtNullFile = new Text("File selected is null.\n");
    tfInfo.getChildren().add(txtNullFile);
    scrollPaneInfo.setVvalue(1.0);
    return;
}

try {

    XSSFWorkbook xWorkbook = new XSSFWorkbook(filePrefillTemplate);
    XSSFSheet xSheet = xWorkbook.getSheet("info");
    XSSFSheet ySheet = xWorkbook.getSheet("data");

    nColCount = ySheet.getRow(0).getLastCellNum();
    nRowCount = ySheet.getLastRowNum();

    Row yRow = ySheet.getRow(0);

    for (int cCounter = 0; cCounter < nColCount; cCounter++) {
        Cell yCell = yRow.getCell(cCounter);
        final int finCounter = cCounter;

        final TableColumn<ObservableList, String> column = new TableColumn<>(yCell.getStringCellValue().toString());
        tvReadExcelFile1.getColumns().add(column);

        column.setCellValueFactory(new Callback<>() {
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
                return new SimpleStringProperty(param.getValue().get(finCounter).toString());
            }
        });
    }

    tvReadExcelFile1.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);

    xWorkbook.close();

} catch (IOException ioException) {
    // IO Exception
} catch (InvalidFormatException invalidFormatException) {
    // Invalid format exception
}

}

I am trying to have a TableView with auto resized columns and scroll bars.

The TableView is in my FXML file, and I don't have it wrapped in any pane. I have read that TableView should automatically add scroll bars as needed, and you don't need it within a ScrollPane. I would also like the TableColumns to size the width as the headers (1st row) is added (doesn't have to resize to data, once that is added later).

This is my TableView, on a Tab, in my FXML file:

        <Tab text="Prefill PDFs from Excel Template">
          <content>
                <TableView fx:id="tvReadExcelFile1" prefHeight="515.0" prefWidth="1004.0" />
          </content>
        </Tab>

Here is where I populate the TableView with the column headers (from excel file). Works as expected but TableView doesn't automatically add Scroll bars, and I can't seem to get the width of the columns to autosize. You will see a ScrollPane being referenced, but that is for a TextFlow control.

protected void handleOpenPrefillTemplate(ActionEvent actionEvent) throws IOException, InvalidFormatException {
int nColCount;
int nRowCount;

tvReadExcelFile1.getItems().clear();
tvReadExcelFile1.getColumns().clear();

File filePrefillTemplate = XT4FileDialog.handleOpenFileDialog("Excel File", "*.xlsx");

if (filePrefillTemplate == null) {
    Text txtNullFile = new Text("File selected is null.\n");
    tfInfo.getChildren().add(txtNullFile);
    scrollPaneInfo.setVvalue(1.0);
    return;
}

try {

    XSSFWorkbook xWorkbook = new XSSFWorkbook(filePrefillTemplate);
    XSSFSheet xSheet = xWorkbook.getSheet("info");
    XSSFSheet ySheet = xWorkbook.getSheet("data");

    nColCount = ySheet.getRow(0).getLastCellNum();
    nRowCount = ySheet.getLastRowNum();

    Row yRow = ySheet.getRow(0);

    for (int cCounter = 0; cCounter < nColCount; cCounter++) {
        Cell yCell = yRow.getCell(cCounter);
        final int finCounter = cCounter;

        final TableColumn<ObservableList, String> column = new TableColumn<>(yCell.getStringCellValue().toString());
        tvReadExcelFile1.getColumns().add(column);

        column.setCellValueFactory(new Callback<>() {
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
                return new SimpleStringProperty(param.getValue().get(finCounter).toString());
            }
        });
    }

    tvReadExcelFile1.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);

    xWorkbook.close();

} catch (IOException ioException) {
    // IO Exception
} catch (InvalidFormatException invalidFormatException) {
    // Invalid format exception
}

}

Share Improve this question asked Nov 19, 2024 at 14:33 Trevor HTrevor H 111 bronze badge 2
  • Provide a minimal reproducible example, it should provide everything required to replicate your issue using only copy and paste with no change, addition, or third-party dependencies. Instead of data from Excel, use hardcoded data, or lorem ipsum generated data, like this example – jewelsea Commented Nov 19, 2024 at 19:34
  • What I'm understanding is that, you want the table view to have scroll bars added as the content increases and size of column to adjust dynamically according to the maximum content of the cell, I had a similar problem where the scrollbars were not appearing in my table view, I fixed it by setting the minimum width of the column and the scroll bars appeared. I believe same can be done in your case, instead of setting minimum width upfront, you can first get the maximum size of column required from the excel sheet and then set it as the width size, I hope it works. – Veerendra Singh Commented Nov 24, 2024 at 4:23
Add a comment  | 

1 Answer 1

Reset to default 0

Thank you so much for all of the suggestions. What ended up working was I wrapped the TableView in a StackPane, and I also think the TableView needed the data. The empty columns by themselves didn't add the scrollbar. The Columns are now resizing too, they're just very slightly to small to fit entire column header so I think I just need to expand the relative widths slightly.

发布评论

评论列表(0)

  1. 暂无评论