问题
I currently have a ListView that is resizable in Width and I use a custom CellFactory that returns my custom ListCell objects.
I already read this: Customize ListView in JavaFX with FXML and I use a similar construct like the one mentioned:
public class ListViewCell extends ListCell<String>{
@Override
public void updateItem(String string, boolean empty){
super.updateItem(string,empty);
if(string != null) {
...
setGraphic(myComponent);
}
}
I want myComponent to take the full size that is available inside the ListCell, however it seems that the setGraphic method limits the size of the given Node.
I can provide all my code if it is necessary, however I am not sure which parts are relevant and I do not want to post a big wall of code.
回答1:
Try setting prefWidth property of your control to Infinity either via CSS or via API so as to provide its maximal expansion whithin container:
myComponent.setStyle("-fx-pref-width: Infinity");
回答2:
I solved my Problem now, here is how:
class MyListCell extends ListCell<MyObject>
{
private final AnchorPane _myComponent;
public MyListCell()
{
...
this.setPrefWidth(0);
myComponent.prefWidthProperty().bind(this.widthProperty());
}
...
@Override
protected void updateItem(MyObject item, boolean empty)
{
...
setGraphic(_myComponent);
...
}
}
If this.setPrefWdith(0) is ommited, myComponent will grow inside the ListCell, but the Cell won't shrink if I shrink my ListView.
来源:https://stackoverflow.com/questions/24108466/javafx-listcell-enable-horizontal-growth