2012-07-14 67 views
19

我試圖創建一個表有兩列。 我使用netbeans 7.2中包含的場景生成器。 在我所看到的一切,你需要做的所有例子是表列拖到表,將採取全尺寸的,這不是我的情況屬實。 這是場景生成器生成的fxml文件。 只是爲了清楚我不改變java的表格屬性。表列將不走表視圖的全尺寸在JavaFX

的FXML:當與FXML文檔應用

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

<?import java.lang.*?> 
<?import java.net.*?> 
<?import java.util.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.image.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 

<AnchorPane id="AnchorPane" prefHeight="758.0" prefWidth="956.0" styleClass="screen" xmlns:fx="http://javafx.com/fxml" fx:controller="bgu.dcr.az.cpu.ui.expb.ExperimentBuilderScreen"> 
    <children> 
    <GridPane id="gridPane1" prefHeight="517.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <children> 
     <Label id="label1" alignment="CENTER" contentDisplay="CENTER" prefHeight="38.0" prefWidth="9999.0" styleClass="caption" text="Create New Experiment" textFill="#990000" GridPane.columnIndex="0" GridPane.rowIndex="0"> 
      <font> 
      <Font name="Consolas Bold" size="20.0" /> 
      </font> 
     </Label> 
     <HBox id="hBox1" alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="5.0" GridPane.columnIndex="0" GridPane.rowIndex="1"> 
      <children> 
      <Label id="label2" graphicTextGap="0.0" styleClass="field-label" text="Experiment Name"> 
       <font> 
       <Font name="Consolas" size="15.0" /> 
       </font> 
       <HBox.margin> 
       <Insets top="3.0" /> 
       </HBox.margin> 
      </Label> 
      <TextField id="textField1" prefWidth="200.0" HBox.hgrow="ALWAYS" /> 
      <Button id="button2" fx:id="saveButton" styleClass="dialog-button" text="Save" /> 
      <Button id="button3" styleClass="dialog-button" text="Dismiss" /> 
      </children> 
      <padding> 
      <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x3" /> 
      </padding> 
     </HBox> 
     <VBox id="VBox" alignment="CENTER" spacing="5.0" GridPane.columnIndex="0" GridPane.rowIndex="2"> 
      <children> 
      <Label id="label3" prefWidth="9999.0" styleClass="field-label" text="Experiment Tests"> 
       <VBox.margin> 
       <Insets left="5.0" top="5.0" /> 
       </VBox.margin> 
      </Label> 
      <HBox id="HBox" alignment="CENTER" spacing="5.0"> 
       <children> 
       <Button id="button1" prefHeight="999.0" styleClass="add-button" text="New"> 
        <HBox.margin> 
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x3" /> 
        </HBox.margin> 
       </Button> 
       <ListView id="listView1" fx:id="tests" orientation="HORIZONTAL" prefHeight="999.0" prefWidth="200.0" HBox.hgrow="ALWAYS"> 
        <HBox.margin> 
        <Insets bottom="4.0" right="5.0" top="4.0" /> 
        </HBox.margin> 
       </ListView> 
       </children> 
      </HBox> 
      </children> 
     </VBox> 
     <VBox id="vBox1" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="0" GridPane.rowIndex="3"> 
      <children> 
      <GridPane id="gridPane2" VBox.vgrow="ALWAYS"> 
       <children> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.rowSpan="4"> 
        <children> 
        <Label id="label4" text="Algorithms"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_algo.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" fx:id="newAlgorithmButton" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" /> 
        </children> 
        <GridPane.margin> 
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" fx:id="x2" /> 
        </GridPane.margin> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="1" GridPane.margin="$x2" GridPane.rowIndex="1"> 
        <children> 
        <Label id="label4" text="Problem Generator"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_pgen.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="2" GridPane.margin="$x2" GridPane.rowIndex="1"> 
        <children> 
        <Label id="label4" prefWidth="9999.0" text="Correctness tester"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_ctest.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" maxWidth="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="1" GridPane.margin="$x2" GridPane.rowIndex="2"> 
        <children> 
        <Label id="label4" prefWidth="130.0" text="Message delayer"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_mdel.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" maxWidth="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="2" GridPane.margin="$x2" GridPane.rowIndex="2"> 
        <children> 
        <Label id="label4" prefWidth="130.0" text="Limiter"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_limiter.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" maxHeight="999.0" maxWidth="999.0" prefHeight="999.0" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" VBox.vgrow="ALWAYS" /> 
        </children> 
       </VBox> 
       <VBox id="vBox2" prefHeight="321.0" prefWidth="150.0" styleClass="with-dashed-border" GridPane.columnIndex="3" GridPane.margin="$x2" GridPane.rowIndex="0" GridPane.rowSpan="4"> 
        <children> 
        <Label id="label4" prefWidth="9999.0" text="Statistic collectors"> 
         <graphic> 
         <ImageView id="emptyImageView1" fitHeight="32.0" fitWidth="32.0" preserveRatio="true"> 
          <image> 
          <Image url="@_scol.png" preserveRatio="false" smooth="false" /> 
          </image> 
         </ImageView> 
         </graphic> 
        </Label> 
        <Button id="button1" prefWidth="999.0" styleClass="add-button" text="New" VBox.margin="$x3" /> 
        </children> 
       </VBox> 
       <HBox id="hBox1" alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="5.0" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="0"> 
        <children> 
        <Label id="label2" graphicTextGap="0.0" styleClass="field-label" text="Name"> 
         <HBox.margin> 
         <Insets top="7.0" /> 
         </HBox.margin> 
        </Label> 
        <TextField id="textField1" maxHeight="28.0" minHeight="28.0" prefHeight="28.0" prefWidth="200.0" text="Empty Test" HBox.hgrow="ALWAYS" /> 
        </children> 
        <padding> 
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" fx:id="x3" /> 
        </padding> 
       </HBox> 
       <VBox id="vBox3" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="3" /> 
       </children> 
       <columnConstraints> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       </columnConstraints> 
       <rowConstraints> 
       <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
       <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
       <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
       <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
       </rowConstraints> 
      </GridPane> 
      </children> 
     </VBox> 
     <VBox id="vBox4" alignment="CENTER" styleClass="with-dashed-border" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.rowIndex="4" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS"> 
      <children> 
      <TableView id="tableView1" prefHeight="54.0" prefWidth="837.0" VBox.vgrow="ALWAYS"> 
       <columns> 
       <TableColumn prefWidth="75.0" text="Column X" /> 
       <TableColumn prefWidth="75.0" text="Column X" /> 
       </columns> 
      </TableView> 
      </children> 
      <GridPane.margin> 
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" fx:id="x2" /> 
      </GridPane.margin> 
     </VBox> 
     </children> 
     <columnConstraints> 
     <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
     </columnConstraints> 
     <rowConstraints> 
     <RowConstraints maxHeight="38.0" minHeight="38.0" prefHeight="38.0" vgrow="NEVER" /> 
     <RowConstraints maxHeight="32.0" minHeight="32.0" prefHeight="32.0" vgrow="NEVER" /> 
     <RowConstraints maxHeight="100.0" minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" /> 
     <RowConstraints vgrow="SOMETIMES" /> 
     <RowConstraints maxHeight="100.0" prefHeight="100.0" valignment="CENTER" vgrow="ALWAYS" /> 
     </rowConstraints> 
    </GridPane> 
    </children> 
    <stylesheets> 
    <URL value="@_style.css" /> 
    <URL value="@../_style.css" /> 
    </stylesheets> 
</AnchorPane> 

回答

41

更新

在JavaFX的2.2+版本的新功能已經使這個答案已經過時了。 FXML set TableView column resize policy:在設置

其使用新FXML特徵,以允許經由FXML設置列大小的FXML基於溶液。

<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> 

如果你不使用FXML的TableView中的大小調整政策,仍然可以通過代碼設置爲這個答案定義:

tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 

在你FXML,您所定義的首選寬度將表格設置爲837,並將每列的首選寬度設置爲75.默認情況下,JavaFX列調整大小策略將使列保持其首選寬度。除非您設置新的列大小調整策略,或者將所有列的首選寬度總和設置爲與表格寬度相加,否則不會獲取填充整個表格寬度的表格列。

的JavaFX確實提供了一個機制來modify the default column resizing policy。由列大小調整策略設置爲一個CONSTRAINED_RESIZE_POLICY,每列的寬度之和將被限制在填補的tableview的寬度 - 即使實現代碼如下調整大小或單個列調整。這可能是你想要的行爲。雖然可能有一種方法可以在FXML的tableView上設置CONSTRAINED_RESIZE_POLICY,但我並不知道其中的一種(儘管我在fxml中的技能是基本的 - 所以也許有人更加了解可以使用純fxml完成​​配置)。

你可以做的是定義一個控制器爲您FXML文件,然後配置控制器容量調整政策。以下代碼演示了一個完整的示例(針對JavaFX 2.2b17預覽)。

FXML文件:

<?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> 

控制器類:

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); 
    } 
} 

應用類:

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(); 
    } 
} 
+0

非常感謝@jewelsea你是第一個也是唯一的地方,我找到了控制列重新調整大小的方法。如果我碰到一種方法來從fxml控制表,我會在這裏發佈它。 – raven 2012-07-22 22:03:05

+0

似乎很奇怪我在控制器中添加UI配置。你可以通過在.fxml文件中添加一個XML標籤來做同樣的事情,如以下鏈接所示:http://stackoverflow.com/questions/14095708/fxml-set-tableview-column-resize-policy – gabuzo 2013-01-06 13:13:42

+0

感謝鏈接gabuzo ,我通過回答更新以反映新的JavaFX 2.2功能,該功能現在允許您從FXML定義表的列大小調整策略。 – jewelsea 2013-01-06 22:13:26

2

如果您使用JavaFX GUI構建器「SceneBuilder 2.0」 - 使用TableView屬性ColumnResizePolicy「constrained-resize」值。