table列在JavaFX中不会采用表格视图的完整大小 [英] table column won't take full size of the table view in javaFX

查看:75
本文介绍了table列在JavaFX中不会采用表格视图的完整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含两列的表. 我正在使用netbeans 7.2中包含的场景构建器. 在所有示例中,我所需要做的就是将表列拖动到表中,它将占用全部大小,在我的情况下,这是不正确的. 这是场景构建器生成的fxml文件. 为了清楚起见,我没有从Java更改表属性.

i am trying to create a table with two columns. i am using the scene builder included in netbeans 7.2. in all the examples i have seen all you need to do is drag the table column to the table and it will take the full size, this is not true in my case. this is the fxml file generated by the scene builder. just to be clear i am not changing the table properties from the java.

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>

推荐答案

更新

当与FXML文档一起使用时,JavaFX 2.2+发行版中的新功能已使该答案过时.

New features in the JavaFX 2.2+ releases have rendered this answer obsolete when applied with an FXML document.

如果您不使用FXML,仍然可以通过此答案中定义的代码来设置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);


在FXML中,您已将表的首选宽度定义为837,并将每列的首选宽度定义为75.默认情况下,JavaFX列大小调整策略会将列保持在其首选宽度.除非您设置新的列调整大小策略或设置所有列的首选宽度的总和以总计表的宽度,否则您将无法使表的列填充整个表的宽度.


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确实提供了一种机制来修改默认的列调整大小策略.通过将列调整大小策略设置为 CONSTRAINED_RESIZE_POLICY ,将限制每一列的宽度总和以填充表视图的宽度-即使调整了表视图的大小或调整了各个列的大小也是如此.这可能是您想要的行为.

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.

虽然可以通过FXML在tableView上设置CONSTRAINED_RESIZE_POLICY,但我并不知道(尽管我在fxml中的技能很初级-因此也许更多的人可以使用纯fxml完成​​配置).

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

您可以做的是为fxml文件定义一个控制器,然后在该控制器中配置调整大小策略.以下代码演示了一个完整的示例(针对JavaFX 2.2b17预览版编写).

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文件:

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

这篇关于table列在JavaFX中不会采用表格视图的完整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆