问题
Update Attached a sample code that shows the problem. I noticed that it happens when:
- The tableview inside a grid pane with another control (say Combobox), so the tableview headers is aligned with the grid
- The first row of the tableview is empty
I am trying to use TableView to display a title and TextField pair, using the following code (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>
Controller
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);
}
}
}
}
I noticed that it works fine as long as there is empty rows as in the following figure
However, it looks very distorted (even sometimes not aligned with the header) if all visible rows are used as in the following two figures. I am not sure if it is a bug or I am doing something wrong.
BTW, if I started to resize one of the columns, the header will snap back to the rows
来源:https://stackoverflow.com/questions/34439890/javafx-tableview-header-is-not-aligned-with-rows