@warakawaの回答に基づいて、私は解決策を見つけました。私たち皆知っているように、Listviewの実装は多くの頭痛を引き起こしています。このため、 "PREF_SIZE"(高さには400の定数があり、高さには幅が計算されます)の問題を修正する2つのクラスを書いています。私が書いたスキンクラスは、期待どおりにサイズを計算し、新しいプロパティ "fillWidth"が "true"に変更されたときに醜い水平バーを防止します。このプロパティは、セルをできるだけ水平方向に拡大させます。よろしく。
を使用でき
package javafxapplication;
import java.util.Set;
import javafx.beans.Observable;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.IndexedCell;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.Region;
public class ListViewFixedSkin extends com.sun.javafx.scene.control.skin.ListViewSkin
{
// <editor-fold defaultstate="collapsed" desc="Fields">
private ListViewFixed listView;
private ScrollBar scrollBarHorizontal;
private ScrollBar scrollBarVertical;
private boolean fillWidthCache;
private double prefWidthCache;
private Region placeholderRegion;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Constructors">
public ListViewFixedSkin(ListViewFixed listView)
{
super(listView);
this.listView = listView;
registerChangeListener(listView.fillWidthProperty(), "FILL_WIDTH");
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Methods">
private void updateFillWidth()
{
if (scrollBarHorizontal != null && scrollBarVertical != null && fillWidthCache != listView.isFillWidth())
{
if (listView.isFillWidth() && !fillWidthCache)
{
scrollBarHorizontal.visibleProperty().addListener(this::updateCellsPrefWidth);
scrollBarVertical.visibleProperty().addListener(this::updateCellsPrefWidth);
}
else
{
scrollBarHorizontal.visibleProperty().removeListener(this::updateCellsPrefWidth);
scrollBarVertical.visibleProperty().removeListener(this::updateCellsPrefWidth);
}
fillWidthCache = listView.isFillWidth();
}
}
private void updateCellsPrefWidth(Observable o)
{
final Insets insets = getSkinnable().getInsets();
final double prefWidth = getSkinnable().getWidth() + insets.getLeft() + insets.getRight() - scrollBarVertical.getWidth();
if (prefWidth != prefWidthCache)
{
for (int i = 0; i < flow.getCellCount(); i++)
{
final IndexedCell cell = flow.getCell(i);
if (!cell.isEmpty())
{
cell.setPrefWidth(prefWidth);
}
}
prefWidthCache = prefWidth;
}
}
private boolean showingPlaceHolder()
{
checkState();
if (getItemCount() == 0)
{
if (placeholderRegion == null)
{
updatePlaceholderRegionVisibility();
final Object obj = getChildren().get(getChildren().size() - 1);
if (obj instanceof Node && ((Region) obj).getStyleClass().contains("placeholder"))
{
placeholderRegion = (Region) obj;
}
}
if (placeholderRegion != null)
{
return true;
}
}
return false;
}
@Override
protected void handleControlPropertyChanged(String p)
{
super.handleControlPropertyChanged(p);
if ("FILL_WIDTH".equals(p))
{
updateFillWidth();
}
}
@Override
protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset)
{
if (showingPlaceHolder())
{
return super.computePrefHeight(width, topInset, rightInset, bottomInset, leftInset);
}
else
{
double computedHeight = topInset + bottomInset;
for (int i = 0; i < flow.getCellCount(); i++)
{
final IndexedCell cell = flow.getCell(i);
if (!cell.isEmpty())
{
computedHeight += cell.getHeight();
}
}
return computedHeight;
}
}
@Override
protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset)
{
double computedWidth = 0;
if (showingPlaceHolder())
{
computedWidth += placeholderRegion.getLayoutBounds().getWidth();
}
else
{
for (int i = 0; i < flow.getCellCount(); i++)
{
final IndexedCell cell = flow.getCell(i);
if (!cell.isEmpty() && cell.getWidth() > computedWidth)
{
computedWidth = cell.getWidth();
}
}
if (scrollBarVertical != null && scrollBarVertical.isVisible())
{
computedWidth += scrollBarVertical.getWidth();
}
}
if (computedWidth != 0)
{
return computedWidth + leftInset + rightInset;
}
else
{
return super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
}
}
@Override
protected void layoutChildren(double x, double y, double w, double h)
{
super.layoutChildren(x, y, w, h);
if (scrollBarHorizontal == null || scrollBarVertical == null)
{
final Set<Node> nodes = getSkinnable().lookupAll(".scroll-bar");
nodes.stream().forEach((node) ->
{
if (node instanceof ScrollBar)
{
final ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.HORIZONTAL)
{
scrollBarHorizontal = scrollBar;
}
else
{
scrollBarVertical = scrollBar;
}
}
});
updateFillWidth();
}
}
@Override
public void dispose()
{
if (fillWidthCache)
{
scrollBarHorizontal.visibleProperty().removeListener(this::updateCellsPrefWidth);
scrollBarVertical.visibleProperty().removeListener(this::updateCellsPrefWidth);
}
listView = null;
super.dispose();
}
// </editor-fold>
}
ListViewFixed.java
ListViewFixedSkin.java(HTTP [CSSをリストビューに追加の空白行が表示されないように]:// fxexperience .com/2011/11/alternate-row-highlighting-in-empty-tableview-and-listview-rows /)。 ListView自体のサイズをアイテムの高さに合わせるのと同じではありませんが、おそらくそれで十分です。 – jewelsea
@ jewelsea私はそれの例を見ましたが、それは私が必要とするものではありません。 – mentics
ご要望に合わせて、[ScrollPane](http:// docs(英語))で[VBox](http://docs.oracle.com/javafx/2/api/javafx/scene/layout/VBox.html) .oracle.com/javafx/2/api/javafx/scene/control/ScrollPane.html)を使用してください。 – jewelsea