调整列大小时适合文本字段宽度

时间:2015-06-26 15:40:48

标签: dynamic javafx width javafx-8

我有一个treeTableView,在它的标题中我有一个Label和Textbox。我希望通过调整大小来增加列时文本框的宽度也会增加。

怎么做?

如何获得动态列宽?或者如何根据列设置动态宽度到文本框?

代码:

<TableView fx:id="tableView" layoutX="1.0" layoutY="1.0"
           minHeight="580.0" prefWidth="434.0" AnchorPane.bottomAnchor="0.0"
           AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
           AnchorPane.topAnchor="0.0" HBox.hgrow="ALWAYS" VBox.vgrow="ALWAYS">
    <columns>
        <TableColumn style="-fx-font-family: Calibri; -fx-font-size: 11px;" text="MessageId">
            <cellValueFactory>
                <PropertyValueFactory property="messageId" />
            </cellValueFactory>
        </TableColumn>

        <TableColumn prefWidth="110.0" style="-fx-font-family: Calibri; -fx-font-size: 11px;" text="Digest Reference">
            <cellValueFactory>
                <PropertyValueFactory property="digestReference" />
            </cellValueFactory>
        </TableColumn>

        <TableColumn prefWidth="115.0" style="-fx-font-family: Calibri; -fx-font-size: 11px;" text="Message Reference">
            <cellValueFactory>
                <PropertyValueFactory property="messageRef" />
            </cellValueFactory>
        </TableColumn>

        <TableColumn prefWidth="115.0" style="-fx-font-family: Calibri; -fx-font-size: 11px;" text="Deal Number">
            <cellValueFactory>
                <PropertyValueFactory property="dealNo" />
            </cellValueFactory>
        </TableColumn>

        <TableColumn style="-fx-font-family: Calibri; -fx-font-size: 11px;" text="Digest Value">
            <cellValueFactory>
                <PropertyValueFactory property="digestValue" />
            </cellValueFactory>
        </TableColumn>

        <TableColumn prefWidth="100.0" style="-fx-font-family: Calibri; -fx-font-size: 11px;" text="Updated Date Time">
            <cellValueFactory>
                <PropertyValueFactory property="updateTime" />
            </cellValueFactory>
        </TableColumn>

    </columns>

    <columnResizePolicy>
        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
    </columnResizePolicy>
</TableView>

JAVA代码:

for (TableColumn<NonRepudiation, ?> tableColumn : tableView.getColumns()) {

double width = tableColumn.getWidth();

VBox vBox = new VBox();
vBox.autosize();
vBox.setPrefWidth(tableColumn.getMaxWidth());

double width = tableColumn.getWidth();

VBox vBox = new VBox();
vBox.autosize();
vBox.setPrefWidth(tableColumn.getMaxWidth());

HBox lableBox = new HBox();
lableBox.setPrefWidth(width);
lableBox.getStyleClass().add("labelBoxTxt");

StackPane HL    =   new StackPane();
HL.getStyleClass().add("greyBorder");

Label label = new Label(tableColumn.getText());
label.setPrefWidth(width);
label.alignmentProperty().setValue(Pos.TOP_LEFT);

TextField textField = new TextField();
textField.setPrefWidth(width);
textField.textProperty().addListener(filterTable(tableColumn));

HBox textInputBox = new HBox();
textInputBox.getChildren().add(textField);
lableBox.getChildren().add(label);

HBox.setMargin(textField, new Insets(3,3,3,0));

vBox.getChildren().addAll(lableBox, HL, textInputBox);

tableColumn.setGraphic(vBox);
}

2 个答案:

答案 0 :(得分:0)

基本上您需要使用HBox.setHgrow()方法。这里重构了你的代码:

for ( TableColumn tableColumn : tableView.getColumns() ) {

    // build label
    Label label = new Label( tableColumn.getText() );
    HBox lableBox = new HBox( label );
    lableBox.getStyleClass().add( "labelBoxTxt" );
    lableBox.setAlignment( Pos.CENTER );

    // build hor line
    StackPane HL = new StackPane();
    HL.getStyleClass().add( "greyBorder" );

    // build text field
    TextField textField = new TextField();
    HBox.setHgrow( textField, Priority.ALWAYS );
    HBox.setMargin( textField, new Insets( 3 ) );
    HBox textInputBox = new HBox( textField );

    // put together
    VBox vBox = new VBox( lableBox, HL, textInputBox );
    tableColumn.setText( "" );
    tableColumn.setGraphic( vBox );
}

答案 1 :(得分:0)

我找到了解决方案。

将css类设置为talbe视图。

<TableView styleClass="k-table-view"

然后将css类添加到vbox

vBox.getStyleClass().add("k-column-graphic");

添加css规则

    .k-table-view .column-header > .label  {
    -fx-content-display: graphic-only;
}

.k-column-graphic {
    -fx-alignment: center-left;
}

我有图形的动态宽度。