Javafx: Tableview header is not aligned with rows

狂风中的少年 提交于 2019-12-11 23:20:28

问题


Update Attached a sample code that shows the problem. I noticed that it happens when:

  1. The tableview inside a grid pane with another control (say Combobox), so the tableview headers is aligned with the grid
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!