Update
New features in the JavaFX 2.2+ releases have rendered this answer obsolete when applied with an FXML document.
An FXML based solution which uses new FXML features to allow the column size to be set via FXML is provided in: FXML set TableView column resize policy.
<TableView fx:id="tableView" layoutX="110.0" layoutY="78.0" prefHeight="200.0" prefWidth="396.0">
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/></columnResizePolicy>
<columns>
<TableColumn prefWidth="75.0" text="Column X" />
<TableColumn prefWidth="75.0" text="Column X" />
</columns>
</TableView>
If you are not using FXML the TableView's resize policy can still be set by code as defined in this answer:
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
In your FXML, you have defined the preferred width of the table to 837, and the preferred width of each column to 75. By default, the JavaFX column resizing policy will keep the columns at their preferred widths. You aren't going to get the table columns filling the width of your entire table unless you either set a new column resizing policy or set the sum of the preferred widths of all of your columns to add up to your table width.
JavaFX does provide a mechanism to modify the default column resizing policy. By setting the column resize policy to a CONSTRAINED_RESIZE_POLICY, the sum of the widths of every column will be constrained to fill the width of the tableview - even if the tableview is resized or individual columns are resized. This is likely the behaviour which you desire.
While there may be a way to set a CONSTRAINED_RESIZE_POLICY on a tableView from FXML, I am not aware of one (though my skills in fxml are rudimentary - so perhaps somebody more informed can accomplish the configuration using pure fxml).
What you can do is define a controller for your fxml file and then configure the resize policy in the controller. The following code demonstrates a complete example (written against the JavaFX 2.2b17 preview).
FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="tableview.TableViewController">
<children>
<TableView fx:id="tableView" layoutX="110.0" layoutY="78.0" prefHeight="200.0" prefWidth="396.0">
<columns>
<TableColumn prefWidth="75.0" text="Column X" />
<TableColumn prefWidth="75.0" text="Column X" />
</columns>
</TableView>
</children>
</AnchorPane>
Controller class:
package tableview;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
/* controller class for the tableview fxml definition */
public class TableViewController implements Initializable {
@FXML // fx:id="tableView"
private TableView<?> tableView; // Value injected by FXMLLoader
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
assert tableView != null : "fx:id="tableView" was not injected: check your FXML file 'tableview.fxml'.";
// initialize your logic here: all @FXML variables will have been injected
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
}
Application class:
package tableview;
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
/** Main application class for tableview fxml demo application */
public class TableViewApplication extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) throws IOException {
AnchorPane layout = FXMLLoader.load(
new URL(TableViewApplication.class.getResource("tableview.fxml").toExternalForm())
);
stage.setScene(new Scene(layout));
stage.show();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…