tableview调整大小以适合窗口

tableview调整大小以适合窗口

本文介绍了JavaFX tableview调整大小以适合窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试使用JavaFX并且正在强行进入它,因为它被认为是未来。我正在尝试创建一个包含4列的表。列和表应调整大小以填充父窗格。我不能为我的生活让这个工作。我现在已经尝试了4个多小时,桌面视图没有调整大小。



这是我的FXML文件。

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

<?import java.lang。*?>
<?import java.util。*?>
<?import javafx.scene。*?>
<?import javafx.scene.control。*?>
<?import javafx.scene.layout。*?>
<?import java.net。*?>
<?import javafx.geometry。*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control。*?>
<?import javafx.scene.control.cell。*?>
<?import javafx.scene.layout。*?>
<?import javafx.scene.text。*?>
<?import javafx.collections。*?>
<?import t.cubed.fxml。*?>


< BorderPane styleClass =rootxmlns:fx =http://javafx.com/fxml/1xmlns =http://javafx.com/javafx/ 8fx:controller =t.cubed.fxml.FXMLDocumentController>
< top>
< MenuBar fx:id =menuBarstyleClass =menu-bar>
< menus>
< Menu text =File>
< items>
< MenuItem onAction =#handleOpenActiontext =Open/>
< MenuItem onAction =#handleExitActiontext =Exit/>
< / items>
< /菜单>
< Menu text =Edit>
< items>
< MenuItem onAction =#handleWeightActiontext =Edit Weights/>
< MenuItem onAction =#handleFilterActiontext =编辑过滤器/>
< MenuItem onAction =#handleOptionsActiontext =Options/>
< / items>
< /菜单>
< / menus>
< / MenuBar>
< / top>
< center>
< GridPane>
<! -
< padding>
< Insets bottom =10.0left =10.0right =10.0top =10.0/>
< / padding>
- >
< TableView fx:id =testTableGridPane.columnIndex =0GridPane.rowIndex =0GridPane.columnSpan =1>
< columnResizePolicy>
< TableView fx:constant =CONSTRAINED_RESIZE_POLICY/>
< / columnResizePolicy>
< columns>
< TableColumn text =TEST NUMBER>
< cellValueFactory>
< PropertyValueFactory property =testNumber/>
< / cellValueFactory>
< / TableColumn>
< TableColumn text =TEST NAME>
< cellValueFactory>
< PropertyValueFactory property =testName/>
< / cellValueFactory>
< / TableColumn>
< TableColumn text =TEST TIME(ms)>
< cellValueFactory>
< PropertyValueFactory property =testTime/>
< / cellValueFactory>
< / TableColumn>
< TableColumn text =BEST MATCH>
< cellValueFactory>
< PropertyValueFactory property =bestMatch/>
< / cellValueFactory>
< / TableColumn>
< / columns>
< items>
<! -
< FXCollections fx:factory =observableArrayList>
< TestTableModel testNumber =100testName =Test1234testTime =0.34bestMatch =99/>
< / FXCollections>
- >
< / items>
< / TableView>
< / GridPane>
< / center>
< stylesheets>
< URL value =@ t-cubed.css/>
< / stylesheets>
< / BorderPane>


解决方案

可调整大小的布局如何工作



JavaFX中可调整大小的项目的大小由放置项目的布局管理器管理。



您需要做什么



针对您的特定问题,您需要设置GridPane大小调整约束来管理放置在网格中的TableView 。



参见 GridPane.hgrow GridPane.vgrow 我在TableView上添加的约束:

 < TableView fx:id =testTable
GridPane.columnIndex = 0
GridPane.columnSpan =1
GridPane.hgrow =ALWAYS
GridPane.vgrow =ALWAYS
GridPane.rowIndex =0>

FXML只反映了Java API,所以你也可以在Java源代码中做同样的事情。即中加载了你的FXML,设置适当的约束,当我使用Scene Builder的预览功能时,它似乎会自动调整大小。

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

<?import java.lang。*?>
<?import java.util。*?>
<?import javafx.scene。*?>
<?import javafx.scene.control。*?>
<?import javafx.scene.layout。*?>
<?import java.net。*?>
<?import javafx.geometry。*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control。*?>
<?import javafx.scene.control.cell。*?>
<?import javafx.scene.layout。*?>
<?import javafx.scene.text。*?>
<?import javafx.collections。*?>
<?import t.cubed.fxml。*?>

< BorderPane styleClass =rootxmlns =http://javafx.com/javafx/8xmlns:fx =http://javafx.com/fxml/1fx:控制器= t.cubed.fxml.FXMLDocumentController >
< top>
< MenuBar fx:id =menuBarstyleClass =menu-bar>
< menus>
< Menu text =File>
< items>
< MenuItem onAction =#handleOpenActiontext =Open/>
< MenuItem onAction =#handleExitActiontext =Exit/>
< / items>
< /菜单>
< Menu text =Edit>
< items>
< MenuItem onAction =#handleWeightActiontext =Edit Weights/>
< MenuItem onAction =#handleFilterActiontext =编辑过滤器/>
< MenuItem onAction =#handleOptionsActiontext =Options/>
< / items>
< /菜单>
< / menus>
< / MenuBar>
< / top>
< center>
< GridPane>
< children>
<! -
< padding>
< Insets bottom =10.0left =10.0right =10.0top =10.0/>
< / padding>
- >
< TableView fx:id =testTableGridPane.columnIndex =0GridPane.columnSpan =1GridPane.hgrow =ALWAYSGridPane.rowIndex =0GridPane.vgrow =ALWAYS> ;
< columnResizePolicy>
< TableView fx:constant =CONSTRAINED_RESIZE_POLICY/>
< / columnResizePolicy>
< columns>
< TableColumn text =TEST NUMBER>
< cellValueFactory>
< PropertyValueFactory property =testNumber/>
< / cellValueFactory>
< / TableColumn>
< TableColumn text =TEST NAME>
< cellValueFactory>
< PropertyValueFactory property =testName/>
< / cellValueFactory>
< / TableColumn>
< TableColumn text =TEST TIME(ms)>
< cellValueFactory>
< PropertyValueFactory property =testTime/>
< / cellValueFactory>
< / TableColumn>
< TableColumn text =BEST MATCH>
< cellValueFactory>
< PropertyValueFactory property =bestMatch/>
< / cellValueFactory>
< / TableColumn>
< / columns>
< items>
<! -
< FXCollections fx:factory =observableArrayList>
< TestTableModel testNumber =100testName =Test1234testTime =0.34bestMatch =99/>
< / FXCollections>
- >
< / items>
< / TableView>
< / children>
< columnConstraints>
< ColumnConstraints />
< / columnConstraints>
< rowConstraints>
< RowConstraints />
< / rowConstraints>
< / GridPane>
< / center>
< stylesheets>
< URL value =@ t-cubed.css/>
< / stylesheets>
< / BorderPane>

一些建议



您将TableView放在GridPane中。在你的情况下使用GridPane有点奇怪,因为GridPane只在GridPane中放置一个节点。只需将TableView直接放在BorderPane的中心或使用更简单的布局父级(如StackPane)就可以正常工作。但也许这只是一个更复杂的用户界面的缩减版本,所以也许这就是你使用GridPane的原因。



进一步阅读



如果你有时间,请在,,和 javadoc并尝试这个小小的。



其他评论问题

StackPanes不需要Hgrow或Vgrow约束。这些限制设置为,这些是定义为:

使用StackPane,所有孩子都是彼此重叠,它们不竞争空间,因此子节点的增长优先级设置不适用。


I am just trying out JavaFX and am forcing my way into it because it is suppose to be the future. I am trying to create a table with 4 columns. The columns AND THE TABLE should resize to fill the parent pane. I cannot for the life of me get this to work. I have been trying for over 4 hours now and the tableview does not resize.

Here is my FXML file.

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.collections.*?>
<?import t.cubed.fxml.*?>


<BorderPane styleClass="root" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="t.cubed.fxml.FXMLDocumentController">
    <top>
        <MenuBar fx:id="menuBar" styleClass="menu-bar">
        <menus>
            <Menu text="File">
                <items>
                    <MenuItem onAction="#handleOpenAction" text="Open" />
                    <MenuItem onAction="#handleExitAction" text="Exit" />
                </items>
            </Menu>
            <Menu text="Edit">
                <items>
                    <MenuItem onAction="#handleWeightAction" text="Edit Weights" />
                    <MenuItem onAction="#handleFilterAction" text="Edit Filters" />
                    <MenuItem onAction="#handleOptionsAction" text="Options" />
                </items>
            </Menu>
        </menus>
    </MenuBar>
    </top>
    <center>
        <GridPane>
               <!--
               <padding>
                   <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
               </padding>
               -->
               <TableView fx:id="testTable" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="1">
                   <columnResizePolicy>
                       <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                   </columnResizePolicy>
                   <columns>
                       <TableColumn text="TEST NUMBER">
                       <cellValueFactory>
                           <PropertyValueFactory property="testNumber" />
                       </cellValueFactory>
                       </TableColumn>
                       <TableColumn text="TEST NAME">
                           <cellValueFactory>
                           <PropertyValueFactory property="testName" />
                       </cellValueFactory>
                       </TableColumn>
                       <TableColumn text="TEST TIME(ms)">
                           <cellValueFactory>
                           <PropertyValueFactory property="testTime" />
                       </cellValueFactory>
                       </TableColumn>
                       <TableColumn text="BEST MATCH">
                           <cellValueFactory>
                           <PropertyValueFactory property="bestMatch" />
                       </cellValueFactory>
                       </TableColumn>
                   </columns>
                   <items>
                    <!--
                    <FXCollections fx:factory="observableArrayList">
                        <TestTableModel testNumber="100" testName="Test1234" testTime="0.34" bestMatch="99"/>
                    </FXCollections>
                    -->
                </items>
               </TableView>
        </GridPane>
    </center>
<stylesheets>
    <URL value="@t-cubed.css" />
</stylesheets>
</BorderPane>
解决方案

How Resizable Layout Works

The size of resizable items in JavaFX is managed by the layout managers in which the items are placed.

What you need to do

For your particular issue, you need to set the GridPane sizing constraints to manage the TableView that you placed in the grid.

See the GridPane.hgrow and GridPane.vgrow constraints I added on the TableView:

<TableView fx:id="testTable"
           GridPane.columnIndex="0"
           GridPane.columnSpan="1"
           GridPane.hgrow="ALWAYS"
           GridPane.vgrow="ALWAYS"
           GridPane.rowIndex="0">

FXML just reflects the Java API, so you could also do the same in Java source as well; i.e. GridPane.setHGrow(node, priority). Though, if you are using FXML for your layout, defining the layout constraints in FXML is recommended.

Refactored Sample

I loaded your FXML up in Scene Builder 2 and set the appropriate constraints and it appears to resize automatically fine when I use the preview functionality of Scene Builder.

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.collections.*?>
<?import t.cubed.fxml.*?>

<BorderPane styleClass="root" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="t.cubed.fxml.FXMLDocumentController">
  <top>
    <MenuBar fx:id="menuBar" styleClass="menu-bar">
      <menus>
        <Menu text="File">
          <items>
            <MenuItem onAction="#handleOpenAction" text="Open" />
            <MenuItem onAction="#handleExitAction" text="Exit" />
          </items>
        </Menu>
        <Menu text="Edit">
          <items>
            <MenuItem onAction="#handleWeightAction" text="Edit Weights" />
            <MenuItem onAction="#handleFilterAction" text="Edit Filters" />
            <MenuItem onAction="#handleOptionsAction" text="Options" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
  </top>
  <center>
    <GridPane>
       <children>
         <!--
         <padding>
             <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
         </padding>
         -->
         <TableView fx:id="testTable" GridPane.columnIndex="0" GridPane.columnSpan="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="0" GridPane.vgrow="ALWAYS">
           <columnResizePolicy>
             <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
           </columnResizePolicy>
           <columns>
             <TableColumn text="TEST NUMBER">
               <cellValueFactory>
                 <PropertyValueFactory property="testNumber" />
               </cellValueFactory>
             </TableColumn>
             <TableColumn text="TEST NAME">
               <cellValueFactory>
                 <PropertyValueFactory property="testName" />
               </cellValueFactory>
             </TableColumn>
             <TableColumn text="TEST TIME(ms)">
               <cellValueFactory>
                 <PropertyValueFactory property="testTime" />
               </cellValueFactory>
             </TableColumn>
             <TableColumn text="BEST MATCH">
               <cellValueFactory>
                 <PropertyValueFactory property="bestMatch" />
               </cellValueFactory>
             </TableColumn>
           </columns>
           <items>
          <!--
          <FXCollections fx:factory="observableArrayList">
              <TestTableModel testNumber="100" testName="Test1234" testTime="0.34" bestMatch="99"/>
          </FXCollections>
          -->
        </items>
         </TableView>
       </children>
         <columnConstraints>
            <ColumnConstraints />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints />
         </rowConstraints>
    </GridPane>
  </center>
  <stylesheets>
    <URL value="@t-cubed.css" />
  </stylesheets>
</BorderPane>

Some Advice

You are placing the TableView inside a GridPane. Using a GridPane in your case is a little strange as the GridPane you only place one node in the GridPane. Just placing the TableView directly in the center of your BorderPane or using a simpler layout parent such as a StackPane would have worked just fine. But perhaps this is just some cut down version of a more complex UI, so perhaps that is why you have used a GridPane.

Further Reading

If you have time, read up on layout in JavaFX in the Node, Pane, Group and GridPane javadoc and try this little layout bounds demo too.

Additional Questions for Comments

StackPanes don't need a Hgrow or Vgrow constraints. Those constraints set Priorities, which are defined as:

With a StackPane, all children are layered on top of each other, they don't compete for space, so such growth priority settings for child nodes are not applicable.

这篇关于JavaFX tableview调整大小以适合窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:23