简体   繁体   English

JavaFX可编辑表格视图:如何更改TableCell内TextField的大小和CSS?

[英]JavaFX editable tableview: How to change size and css of the TextField inside of TableCell?

I have changed height of the TableRows in css and it is working well, but how can I change the size of the TextField in side of TableCell which should be editable. 我已经在CSS中更改了TableRows的高度,并且运行良好,但是如何更改TableCell侧面的TextField的大小,该大小应该是可编辑的。 Hopefully the picture can explain my problem: 希望图片可以解释我的问题: 在此处输入图片说明

You can change the css of the text field just after it's created by adding css. 您可以在添加文本字段后立即更改文本字段的css。 You can also change some things in code. 您还可以更改代码中的某些内容。

    private void createTextField() {
        textField = new TextField(getString());
        textField.setStyle("-fx-padding 1px 1px 1px 1px;-fx-font-size: 10pt;");
        //or another way
        textField.setPadding(new Insets(0,0,0,5));
        textField.setFont(new Font(8));

You could also set the style class using textField.getStyleClass().add("my-textfield"); 您还可以使用textField.getStyleClass().add("my-textfield");设置样式类textField.getStyleClass().add("my-textfield"); and then create styles in your stylesheet for the program using .my-textfield{css stuff...} 然后使用.my-textfield{css stuff...}在程序的样式表中创建样式

I think the best idea is to not change the row size but to change the font. 我认为最好的办法是不更改行大小,而是更改字体。 The row height then changes automatically. 然后,行高会自动更改。 You can even change the font in your whole application by adding a stylesheet with .root{ -fx-font-size:12pt;} to the scene. 您甚至可以通过在场景中添加带有.root{ -fx-font-size:12pt;}的样式表来更改整个应用程序中.root{ -fx-font-size:12pt;}

The Oracle sample has a custom editing cell, example 12-11. Oracle示例具有一个自定义编辑单元,例如12-11。 That is where the TextField gets created. 那就是创建TextField的地方。

If you use TextFieldTableCell.forTableColumn you can style it using .text-field-table-cell 如果使用TextFieldTableCell.forTableColumn ,则可以使用.text-field-table-cell设置样式

.text-field-table-cell{
    -fx-font-size:15pt;
    -fx-background-color: pink;
}

That will style all cells even before they are being edited. 这将在所有单元格被编辑之前设置样式。 If you only want styles in the edit state use .text-field-table-cell .text-field{ . 如果只希望样式处于编辑状态,请使用.text-field-table-cell .text-field{ And they don't have to be pink:) 而且它们不必是粉红色的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM