Search code examples
javajavafxfxml

JavaFX Responsive TableView


The image is pretty self explanatory. The TableView doesn't auto resize when I resize the window. In C#, I used to do this by using Anchor/Dock properties. How am I supposed to do it in JavaFX? I can't find information about it, probably because Java uses different terms.

enter image description here

table.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<DialogPane prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.TableController">
    <header>
        <AnchorPane minHeight="50.0" minWidth="0.0" prefHeight="80.0" prefWidth="400.0">
            <children>
                <HBox alignment="TOP_CENTER" prefHeight="100.0" prefWidth="400.0">
                    <children>
                        <Label fx:id="descriptionLabel" alignment="CENTER" contentDisplay="CENTER" prefWidth="400.0" text="Text:" textAlignment="JUSTIFY">
                            <font>
                                <Font size="22.0" />
                            </font>
                        </Label>
                    </children>
                </HBox>
            </children>
        </AnchorPane>
    </header>
    <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="400.0">
            <children>
                <TableView fx:id="tableView" layoutY="-31.0" prefHeight="252.0" prefWidth="400.0">
                    <columns>
                    </columns>
                </TableView>
            </children>
        </AnchorPane>
    </content>
</DialogPane>

TableController.java

package gui;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

import java.sql.ResultSet;
import java.sql.SQLException;

public class TableController {

    @FXML
    private Label descriptionLabel;

    @FXML
    private TableView tableView;

    public void setTableResultset(String label, ResultSet resultSet) throws SQLException {
        ObservableList<ObservableList> data = FXCollections.observableArrayList();

        // Columns
        for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
            final int j = i;
            TableColumn col = new TableColumn(resultSet.getMetaData().getColumnName(i + 1));
            col.setCellValueFactory((Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>) param -> {
                return new SimpleStringProperty(param.getValue().get(j).toString());
            });
            tableView.getColumns().add(col);
        }

        // Add records
        while (resultSet.next()) {
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
                row.add(resultSet.getString(i));
            }
            data.add(row);
        }
        tableView.setItems(data);

        // Set label text
        descriptionLabel.setText(label);
    }
}


Solution

  • You need to set AnchorPane's top, bottom, right and left anchors:

    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="400.0">
        <children>
            <TableView fx:id="tableView" layoutY="-31.0" prefHeight="252.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                <columns>
                </columns>
            </TableView>
        </children>
    </AnchorPane>