Javafx TableColumn.setGraphic位于列标签下方

Javafx TableColumn.setGraphic位于列标签下方,javafx,tableview,Javafx,Tableview,当我使用以下代码向每列添加文本字段时: tableColumn.setGraphic(textField); textfield被放置在标签的左侧,使列如下所示: 如果我拖出列宽,我会看到标签仍然存在 有没有办法让专栏使用它的原始标签,并将文本字段放在下面? 我曾尝试将自己的标签与文本字段一起添加到VBox中,但随后您失去了自动宽度功能 在James_D的回答之后,标签是正确的,但是专栏风格还是有点奇怪 MVCE: 公共类TableViewTEST扩展应用程序{ List-l

当我使用以下代码向每列添加文本字段时:

      tableColumn.setGraphic(textField);
textfield被放置在标签的左侧,使列如下所示:

如果我拖出列宽,我会看到标签仍然存在

有没有办法让专栏使用它的原始标签,并将文本字段放在下面? 我曾尝试将自己的标签与文本字段一起添加到VBox中,但随后您失去了自动宽度功能

在James_D的回答之后,标签是正确的,但是专栏风格还是有点奇怪

MVCE:

公共类TableViewTEST扩展应用程序{
List-listocolumns=new-ArrayList();
TableView TableView=新建TableView();
公共静态void main(字符串[]args){
发射(args);
}
public void buildData(){
TableColumn myColumn=新的TableColumn(“asd”);
TableColumn myColumn2=新的TableColumn(“qq”);
添加(myColumn);
添加(myColumn2);
//动态添加列
int计数器=0;
for(表列列列:列表列){
最终int j=计数器;
col.setCellValueFactory(新回调(){
公共observeValue调用(TableColumn.CellDataFeatures参数){
返回新的SimpleStringProperty(param.getValue().rowData.get(j));
}
});
计数器++;
TextField txtField=新建TextField();
VBox VBox=新的VBox();
vbox.getChildren().add(txtField);
col.setGraphic(vbox);
tableView.getColumns().add(col);
}
tableView.setRowFactory(电视->{
MyTableRow rowz=新建MyTableRow();
rowz.setOnMouseClicked(事件->{
if(event.getClickCount()==2&&(!rowz.isEmpty()){
}
});
返回rowz;
});
}
@凌驾
public void start(Stage)引发异常{
FlowPane FlowPane=新的FlowPane();
flowpane.getChildren().add(tableView);
buildData();
场景=新场景(流程窗格);
scene.getStylesheets().add(TableViewTEST.class.getResource(“css.css”).toExternalForm());
舞台场景;
stage.show();
}
}

使用具有以下内容的外部css文件:

.table-column .label {
    -fx-content-display: bottom ;
}
下面是一个完整的示例:

import java.util.List;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableWithTextFieldColumnHeaders extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<List<String>> table = new TableView<>();

        table.getColumns().add( createColumn("A", 0) );
        table.getColumns().add( createColumn("B", 1) );

        BorderPane root = new BorderPane(table);
        Scene scene = new Scene(root, 600, 400);
        scene.getStylesheets().add("table-text-field.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private TableColumn<List<String>, String> createColumn(String title, int index) {

        TableColumn<List<String>, String> col = new TableColumn<>(title);

        col.setCellValueFactory(cellData -> 
            new ReadOnlyStringWrapper(cellData.getValue().get(index)));

        TextField textField = new TextField();
        col.setGraphic(textField);

        double textFieldPadding = 8 ;
        textField.prefWidthProperty().bind(
                col.widthProperty().subtract(textFieldPadding));

        return col ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

谢谢@james_D,但是这个专栏的格式看起来还是很奇怪?我会更新我的问题,我无法复制。也许可以尝试文本字段和列的min/prefWidth属性,但它们总是会根据用户数据而变化,我想我是在设想使用绑定的。你能在你的截图中创建一个演示行为的工具吗?那不是MCVE,我无法编译它。
import java.util.List;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableWithTextFieldColumnHeaders extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<List<String>> table = new TableView<>();

        table.getColumns().add( createColumn("A", 0) );
        table.getColumns().add( createColumn("B", 1) );

        BorderPane root = new BorderPane(table);
        Scene scene = new Scene(root, 600, 400);
        scene.getStylesheets().add("table-text-field.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private TableColumn<List<String>, String> createColumn(String title, int index) {

        TableColumn<List<String>, String> col = new TableColumn<>(title);

        col.setCellValueFactory(cellData -> 
            new ReadOnlyStringWrapper(cellData.getValue().get(index)));

        TextField textField = new TextField();
        col.setGraphic(textField);

        double textFieldPadding = 8 ;
        textField.prefWidthProperty().bind(
                col.widthProperty().subtract(textFieldPadding));

        return col ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
.table-column .label {
    -fx-content-display: bottom ;
}