【发布时间】:2014-11-19 03:05:52
【问题描述】:
使用表格视图附带的垂直滚动条时会出现此问题。这是我的表格视图的样子:
http://i.stack.imgur.com/w7Wv3.png
当您使用鼠标滚轮滚动时,这很好。当您使用滚动条上的向上/向下箭头滚动时,这很好。但是,当您实际使用鼠标指针拖动滚动条时,复选框中的选择就会变得混乱。例如,如果您看到上面链接的图像,则选择了第三个复选框。滚动后,现在可能会选中第 5 个复选框或第 2 个复选框。甚至可能会选择另外两个复选框。
我认为这是表格视图滚动条的刷新/重绘问题。
解决这个问题的正确方法是拦截垂直滚动事件并在用户滚动时手动刷新网格。为此,我需要能够掌握表格滚动条的句柄。像 table.getScrollBar().setEventHandler(this) 这样的东西。我无法做到这一点。表格的滚动条似乎被隐藏了。如果无法到达滚动条,则无法拦截滚动事件,也无法修复此问题。
这是我的 fxml 的样子:
<TableView fx:id="table" prefWidth="650" prefHeight="200" AnchorPane.topAnchor="50" AnchorPane.leftAnchor="0">
<columns>
<TableColumn fx:id="nameCol" text="Product Type" prefWidth="125" />
<TableColumn fx:id="totalCol" text="Total" prefWidth="65" />
<TableColumn fx:id="entitledCol" text="Entitled" />
<TableColumn fx:id="forPurchaseCol" text="For Purchase" prefWidth="125" />
<TableColumn fx:id="nonImmediate" text="Non Immediate" prefWidth="125" />
</columns>
</TableView>
这是来自控制器的相关java代码:
nameCol.setCellValueFactory(new PropertyValueFactory<Product, String>("productFullName"));
totalCol.setCellValueFactory(new PropertyValueFactory<Product, Integer>("totalCount"));
entitledCol.setCellValueFactory(new PropertyValueFactory<Product, Integer>("entitledImmediateCount"));
forPurchaseCol.setCellValueFactory(new PropertyValueFactory<Product, ObservableMap<String, Number>>(
"forPurchaseImmediateCountAndPrice"));
nonImmediate.setCellValueFactory(new PropertyValueFactory<Product, Integer>("nonImmediateCount"));
forPurchaseCol.setCellFactory(new Callback<TableColumn<Product, ObservableMap<String, Number>>, TableCell<Product, ObservableMap<String, Number>>>() {
@Override
public TableCell<Product, ObservableMap<String, Number>> call(TableColumn<Product, ObservableMap<String, Number>> col) {
return new PriceCell();
}
});
这就是 PriceCell.java 的样子:
public class PriceCell extends TableCell<Product, ObservableMap<String, Number>> {
private CheckBox box = new CheckBox();
@Override
protected void updateItem(ObservableMap<String, Number> countPrice, boolean empty) {
super.updateItem(countPrice, empty);
if(countPrice != null) {
int count = (Integer) countPrice.get("count");
if(count == 0) {
setText("0");
}
else {
setText(countPrice.get("count") + " ($" + countPrice.get("price") + ")");
box.setOnAction(this);
box.setId(countPrice.get("price").toString());
setGraphic(box);
}
}
else {
setText("");
setGraphic(null);
}
}
}
谢谢。任何帮助表示赞赏。
【问题讨论】:
标签: checkbox javafx scroll tableview