1 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
不支持滾動(dòng)條的自定義布局。我最初的評(píng)論是,你需要一個(gè)帶有自定義TableHeaderRow的自定義TableViewSkin:不幸的是,后者負(fù)責(zé)管理......好吧,tableHeader只是故事的一部分。
一個(gè) TableHeaderRow 確實(shí)負(fù)責(zé)布置表頭
但是:TableHeaderRow無(wú)法對(duì)垂直滾動(dòng)條進(jìn)行布局 - 當(dāng)它試圖在被覆蓋中這樣做時(shí),它會(huì)立即重置
layoutChildren()
重置發(fā)生在VirtualFlow(它是滾動(dòng)條的父級(jí))中
因此,在一天結(jié)束時(shí),我們需要
一個(gè)自定義的 TableHeaderRow,表示需要放大和重新定位 scrollBar:下面的示例設(shè)置一個(gè)標(biāo)記,如果 scrollBar 可見(待定:檢查菜單按鈕是否可見),并在 scrollBar 的屬性映射中設(shè)置所需的額外高度
一個(gè)自定義的VirtualFlow,可以處理標(biāo)記并根據(jù)需要實(shí)際進(jìn)行布局:下面的示例檢查標(biāo)記并在需要時(shí)調(diào)整大小/重新定位滾動(dòng)條。
一個(gè)自定義的 TableViewSkin 來(lái)注入兩者(通過(guò)重寫的工廠方法)
該示例是針對(duì) fx11 編寫的,應(yīng)該適用于 fx10,但不適用于 fx9,因?yàn)楹笳卟辉试S提供自定義 VirtualFlow:
public class TableWithoutCorner extends Application {
/**
* Custom TableHeaderRow that requests a larger vbar height
* if needed.
*/
private static class MyTableHeader extends TableHeaderRow {
private Region cornerAlias;
private ScrollBar vBar;
private TableViewSkinBase skin;
public MyTableHeader(TableViewSkinBase skin) {
super(skin);
this.skin = skin;
}
@Override
protected void layoutChildren() {
super.layoutChildren();
adjustCornerLayout();
}
private void adjustCornerLayout() {
checkAlias();
// tbd: check also if corner is visible
if (!vBar.isVisible()) {
vBar.getProperties().remove("DELTA");
} else {
vBar.getProperties().put("DELTA", getHeight());
}
}
private void checkAlias() {
if (cornerAlias == null) {
cornerAlias = (Region) lookup(".show-hide-columns-button");
}
if (vBar == null) {
vBar = (ScrollBar) skin.getSkinnable().lookup(".scroll-bar:vertical");
}
}
}
/**
* Custom VirtualFlow that respects additinal height for its
* vertical ScrollBar.
*/
private static class MyFlow extends VirtualFlow {
private ScrollBar vBar;
private Region clip;
public MyFlow() {
// the scrollbar to adjust
vBar = (ScrollBar) lookup(".scroll-bar:vertical");
// the clipped container to use for accessing viewport dimensions
clip = (Region) lookup(".clipped-container");
}
/**
* Overridden to adjust vertical scrollbar's height and y-location
* after calling super.
*/
@Override
protected void layoutChildren() {
super.layoutChildren();
adjustVBar();
}
/**
* Adjusts vBar height and y-location by the height as
* requested by the table header.
*/
protected void adjustVBar() {
if (vBar.getProperties().get("DELTA") == null) return;
double delta = (double) vBar.getProperties().get("DELTA");
vBar.relocate(clip.getWidth(), - delta);
vBar.resize(vBar.getWidth(), clip.getHeight() + delta);
}
}
/**
* Boilerplate: need custom TableViewSkin to inject a custom TableHeaderRow and
* custom VirtualFlow.
*/
private static class MyTableViewSkin<T> extends TableViewSkin<T> {
public MyTableViewSkin(TableView<T> control) {
super(control);
}
@Override
protected TableHeaderRow createTableHeaderRow() {
return new MyTableHeader(this);
}
@Override
protected VirtualFlow<TableRow<T>> createVirtualFlow() {
return new MyFlow();
}
}
private Parent createContent() {
TableView<Locale> table = new TableView<>(FXCollections.observableArrayList(Locale.getAvailableLocales())) {
@Override
protected Skin<?> createDefaultSkin() {
return new MyTableViewSkin(this);
}
};
TableColumn<Locale, String> col = new TableColumn<>("Name");
col.setCellValueFactory(new PropertyValueFactory<>("displayName"));
table.getColumns().addAll(col);
return table;
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
//stage.setTitle(FXUtils.version());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(TableWithoutCorner.class.getName());
}
添加回答
舉報(bào)