Problem Binding TableView prefWidthProperty to it's Parent Container's prefWidthProperty

Kamper K :

Relevant FXML :

<ScrollPane fx:id="scrollpane_var" hbarPolicy="NEVER" prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="3" GridPane.rowIndex="2" GridPane.rowSpan="5">
         <content>
            <TableView fx:id="tableview_var" prefHeight="169.0" prefWidth="292.0">
              <columns>
                <TableColumn prefWidth="69.0" text="Quantité" />
                <TableColumn prefWidth="161.0" text="Nom" />
                  <TableColumn prefWidth="56.0" text="Prix" />
              </columns>
            </TableView>
         </content>
      </ScrollPane>

Controller class Code :

public class Lancer {

    @FXML // fx:id="scrollpane_var
    private ScrollPane scrollpane_var; // Value injected by FXMLLoader

    @FXML // fx:id="tableview_var"
    private TableView<String> tableview_var; // Value injected by FXMLLoaderte void initialize()

    public void initialize() {
    tableview_var.prefWidthProperty().bind(scrollpane_var.prefWidthProperty());
    }

Visual Representation :

Click to see Scenebuilder screenshot

as the title says i'm trying to bind a TableView's width to it's Parent container and the documentation is a bit confusing as i am very new to GUI Applications .

This line of code is the furthest i've gottent and i dont know why it's not working :

 public void initialize() {
        tableview_var.prefWidthProperty().bind(scrollpane_var.prefWidthProperty());
        }
James_D :

A TableView manages its own scrolling, so you should not be putting it inside a ScrollPane at all. Just omit the scroll pane from the layout entirely.

In general, if you want the content of a scroll pane to fit the available width of the scroll pane, you would use the fitToWidth property, instead of trying to use a binding. In general, binding the pref width like this is a bad idea; if the layout or container you are using doesn't provide the functionality you need, and it almost always will, you should subclass Pane and override layoutChildren() and other methods. Here, the TableView already provides scrolling, and in more general cases, the ScrollPane allows you to specify the content should fit the width of the scroll pane.

<ScrollPane ... fitToWidth="true>
    <!-- ... -->
</ScrollPane>

But again, here the scroll pane is redundant.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238492&siteId=1