更新 附上显示问题的sample code。我注意到它发生在:
我正在尝试使用TableView显示标题和TextField对,使用以下代码(Java8u66)
FXML
<TableView fx:id="AttributeValueTableView" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" GridPane.columnSpan="2" GridPane.rowIndex="1">
<columns>
<TableColumn fx:id="AttributeTableColumn" editable="false" minWidth="50.0" prefWidth="200.0" text="Attribute"/>
<TableColumn fx:id="ValueTableColumn" maxWidth="1.7976931348623157E308" minWidth="50.0" prefWidth="400.0" text="Value"/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
控制器
public class Controller implements Initializable {
@FXML
private TableView<AttributeValuePair> AttributeValueTableView;
@FXML
private TableColumn<AttributeValuePair, String> AttributeTableColumn;
@FXML
private TableColumn<AttributeValuePair, Object> ValueTableColumn;
private ObservableList<AttributeValuePair> valuePairs = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
AttributeTableColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
ValueTableColumn.setCellFactory(param -> new AttributeValueCell());
valuePairs.add(new AttributeValuePair("Attribute1"));
valuePairs.add(new AttributeValuePair("Attribute2"));
valuePairs.add(new AttributeValuePair("Attribute3"));
valuePairs.add(new AttributeValuePair("Attribute4"));
valuePairs.add(new AttributeValuePair("Attribute5"));
valuePairs.add(new AttributeValuePair("Attribute6"));
AttributeValueTableView.setItems(valuePairs);
}
private class AttributeValueCell extends javafx.scene.control.TableCell<AttributeValuePair, Object> {
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
AttributeValuePair valuePair = (AttributeValuePair) this.getTableRow().getItem();
if (empty || valuePair == null) {
setGraphic(null);
setText(null);
} else {
TextField t = new TextField();
setGraphic(t);
}
}
}
}
但是,如果使用所有可见行,则它看起来非常扭曲(有时甚至与标题不对齐),如下面的两个图所示。 我不确定这是一个错误还是我做错了什么。
顺便说一句,如果我开始调整其中一个列的大小,标题会快速回到行