2017-08-16 83 views
0

我期待在JavaFX 8中實現基本的可擴展自定義控件,其中包含添加了其他控件的窗格。如何實現基於窗格的自定義JavaFX控件

因此,例如,它可以包含GridPane,其持有TextField,ButtonCheckBox

我不想子類PaneGridPane,因爲我不想將這些API公開給用戶。所以,「一個由網格窗格組成的節點」與「一個擴展網格窗格的節點」相對。

我看到延伸RegionControl是可能的,這是推薦?將窗體大小和佈局委派給窗格需要什麼?

public class BasePaneControl extends Control { 
    private final Pane pane; 

    public BasePaneControl(Pane pane) { 
     this.pane = pane; 
     getChildren().add(pane); 
    } 

    // What do I need to delegate here to the pane to get sizing 
    // to affect and be calculated by the pane? 
} 

public class MyControl extends BasePaneControl { 
    private final GridPane gp = new GridPane(); 
    public MyControl() { 
     super(gp); 
     gp.add(new TextField(), 0, 0); 
     gp.add(new CheckBox(), 0, 1); 
     gp.add(new Button("Whatever"), 0, 2); 
    } 

    // some methods to manage how the control works. 
} 

我需要幫忙實施BasePaneControl以上請。

回答

1

擴展區域,並覆蓋layoutChildren方法。

您可以使用Region.snappedTopInset()方法(以及底部,左側和右側)來獲取BasePaneControl的位置。然後根據可能是BasePaneControl的一部分的其他組件計算出您想要的窗格。

一旦您知道該窗格的位置,請致電resizeRelocate

/** 
* Invoked during the layout pass to layout this node and all its content. 
*/ 
@Override protected void layoutChildren() { 
    // dimensions of this region 
    final double width = getWidth(); 
    final double height = getHeight(); 

    // coordinates for placing pane 
    double top = snappedTopInset(); 
    double left = snappedLeftInset(); 
    double bottom = snappedBottomInset(); 
    double right = snappedRightInset(); 

    // adjust dimensions for pane based on any nodes that are part of BasePaneControl 
    top += titleLabel.getHeight(); 
    left += someOtherNode.getWidth(); 

    // layout pane 
    pane.resizeRelocate(left,top,width-left-right,height-top-bottom); 
} 
相關問題