Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import javafx.scene.Node;
- import java.util.ArrayList;
- import javafx.geometry.HPos;
- import javafx.geometry.VPos;
- import javafx.scene.layout.Pane;
- import javafx.beans.property.DoubleProperty;
- import javafx.beans.property.SimpleDoubleProperty;
- /**
- *
- * @author Aamir Khan
- * A Pane to put your Nodes in a Sorted Order....
- */
- public class SortedPane extends Pane {
- public SortedPane() {
- super();
- spacingProperty().addListener(e -> requestLayout());
- }
- public SortedPane(double spacing) {
- this();
- setSpacing(spacing);
- }
- @Override
- protected void layoutChildren() {
- List<Node> sortedChildren = new ArrayList<>(getChildren());
- sortedChildren.sort((node1, node2) -> new Double(node1.prefWidth(-1)).compareTo(new Double(node2.prefWidth(-1))));
- double currentX = getInsets().getLeft();
- for (Node node : sortedChildren) {
- double width = node.prefWidth(-1);
- double height = node.prefHeight(-1);
- layoutInArea(node, currentX, getInsets().getTop(), width, height, 0, HPos.CENTER, VPos.CENTER);
- currentX = currentX + width + getSpacing();
- }
- }
- @Override
- protected double computePrefHeight(double width) {
- double maxH = 0;
- for (Node node : getChildren()) {
- maxH = Math.max(node.prefHeight(-1), maxH);
- }
- return getInsets().getTop() + getInsets().getBottom() + maxH;
- }
- @Override
- protected double computePrefWidth(double height) {
- double width = 0;
- for (Node node : getChildren()) {
- width = width + node.prefWidth(-1);
- }
- return getInsets().getLeft() + getInsets().getRight() + width + getSpacing();
- }
- @Override
- protected double computeMaxHeight(double width) {
- return computePrefHeight(width);
- }
- @Override
- protected double computeMaxWidth(double height) {
- return computePrefWidth(height);
- }
- @Override
- protected double computeMinHeight(double width) {
- return computePrefHeight(width);
- }
- @Override
- protected double computeMinWidth(double height) {
- return computePrefWidth(height);
- }
- public final void setSpacing(double value) {
- spacingProperty().set(value);
- }
- private DoubleProperty spacing;
- public final DoubleProperty spacingProperty() {
- if (spacing == null) {
- spacing = new SimpleDoubleProperty(0){
- @Override
- protected void invalidated(){
- super.invalidated();
- requestLayout();
- }
- };
- }
- return spacing;
- }
- public double getSpacing() {
- return spacing == null ? 0 : spacing.get();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement