SplitPane布局问题

时间:2015-01-14 22:37:59

标签: java javafx javafx-8 splitpane

这是一个交叉帖子:http://mail.openjdk.java.net/pipermail/openjfx-dev/2015-January/016437.html

有时当我设置SplitPane的分隔符位置时 以编程方式,我的值被一些JavaFX内部布局再次覆盖 代码。

出于调试目的,我已经添加了一个监听器的position属性 divider并抛出一个RuntimeException来查看堆栈跟踪。它看起来 像这样:

at 
com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
        at 
com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
        at 
javafx.beans.property.DoublePropertyBase.fireValueChangedEvent(DoublePropertyBase.java:106)
        at 
javafx.beans.property.DoublePropertyBase.markInvalid(DoublePropertyBase.java:113)
        at 
javafx.beans.property.DoublePropertyBase.set(DoublePropertyBase.java:146)
        at javafx.scene.control.SplitPane$Divider.setPosition(SplitPane.java:486)
        at 
com.sun.javafx.scene.control.skin.SplitPaneSkin.setAbsoluteDividerPos(SplitPaneSkin.java:310)
        at 
com.sun.javafx.scene.control.skin.SplitPaneSkin.setupContentAndDividerForLayout(SplitPaneSkin.java:502)
        at 
com.sun.javafx.scene.control.skin.SplitPaneSkin.layoutChildren(SplitPaneSkin.java:817)
        at javafx.scene.control.Control.layoutChildren(Control.java:589)
        at javafx.scene.Parent.layout(Parent.java:1074)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Parent.layout(Parent.java:1080)
        at javafx.scene.Scene.doLayoutPass(Scene.java:532)
        at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2363)
        at com.sun.javafx.tk.Toolkit.lambda$runPulse$28(Toolkit.java:314)
        at com.sun.javafx.tk.Toolkit$$Lambda$230/25595560.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:313)
        at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:340)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:451)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:431)
        at 
com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$363(QuantumToolkit.java:298)
        at 
com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$59/174792896.run(Unknown 
Source)
        at 
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at 
com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126)
        at com.sun.glass.ui.gtk.GtkApplication$$Lambda$55/1472148546.run(Unknown 
Source)
        at java.lang.Thread.run(Thread.java:745)

它似乎没有被我的一些代码触发(至少不是直接触发) 根据堆栈跟踪)。

我尝试了几件事,包括设置prefWidth / prefHeight 项目达到预期值,但我无法有效地停止SplitPane 在重新布局期间“隐藏”第一个和第三个(最后一个)项目。

在layoutChildren调用期间,两个分隔符发生了变化,如下所示:

分频器位置已更改:旧值:0.10857763300760044,新值: 0.004343105320304018

分频器位置已更改:旧值:0.8914223669923995,新值: 0.995656894679696

我看了一下com.sun.javafx.scene.control.skin.SplitPaneSkin(来源) 随JDK提供的代码),但不幸的是,它看起来并不容易 在这种情况下计算面积。

为什么这种重新布局在这里讨价还价? 为什么它会覆盖我为分频器位置设置的值? 怎么能阻止这个?

我曾尝试编写SSCCE,但我还是无法使用简单的示例重现该问题。 (问题发生在自定义控件的Skin实现中。)

更新

有问题的SplitPane的3个项目(控件的子类)(方向:垂直)具有以下最小和最大高度:

property: height min: -1.0 ; max: -1.0
property: height min: -1.0 ; max: -1.0
property: height min: -1.0 ; max: -1.0

AFAIK这是Region.USE_COMPUTED_SIZE的价值。

我将最大值更改为Double.MAX_VALUE

property: height min: -1.0 ; max: 1.7976931348623157E308 
property: height min: -1.0 ; max: 1.7976931348623157E308 
property: height min: -1.0 ; max: 1.7976931348623157E308

但我仍然遇到同样的问题。

更新2

我终于设法创建了一个SSCCE:

package splitpanelayoutissue;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SplitPaneLayoutIssue extends Application {

    private static final double DIVIDER_POSITION_0 = 0.10834236186348863;
    private static final double DIVIDER_POSITION_1 = 0.8916576381365114;

    @Override
    public void start(Stage primaryStage) {
        SplitPane outerSplitPane = createOuterSplitPane();

        StackPane root = new StackPane();
        root.getChildren().add(outerSplitPane);

        Scene scene = new Scene(root, 1500, 1000);

        primaryStage.setTitle("SplitPane Layout Issue");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private SplitPane createOuterSplitPane() {
        SplitPane outerSplitPane = createSplitPane("outerSplitPane");

        TabPane leftTabPane = addTabPane(outerSplitPane, "Left");

        SplitPane innerSplitPane = createInnerSplitPane();
        outerSplitPane.getItems().add(innerSplitPane);

        TabPane rightTabPane = addTabPane(outerSplitPane, "Right");

        SplitPane.setResizableWithParent(leftTabPane, Boolean.FALSE);
        SplitPane.setResizableWithParent(rightTabPane, Boolean.FALSE);
        SplitPane.setResizableWithParent(innerSplitPane, Boolean.TRUE);

        setDividerPositions(outerSplitPane, innerSplitPane);

        leftTabPane.getTabs().get(0).setOnClosed(event -> {
            // the following line causes com.sun.javafx.scene.control.skin.SplitPaneSkin.Content.getArea() 
            // of all 3 items return 0, even though setDividerPositions is called before the next SplitPane.layoutChildren call
            innerSplitPane.getItems().setAll(innerSplitPane.getItems().get(0),innerSplitPane.getItems().get(1),innerSplitPane.getItems().get(2));

            outerSplitPane.getItems().setAll(innerSplitPane, rightTabPane);
            setDividerPositions(outerSplitPane, innerSplitPane);
        });

        return outerSplitPane;
    }

    private SplitPane createSplitPane(String name) {
        SplitPane splitPane = new SplitPane() {

            @Override
            protected void layoutChildren() {
                System.out.println("Calling relayout on " + name + "...");
                com.sun.javafx.scene.control.skin.SplitPaneSkin skin = (com.sun.javafx.scene.control.skin.SplitPaneSkin) getSkin();
                super.layoutChildren();
            }

        };
        return splitPane;
    }

    private void setDividerPositions(SplitPane outerSplitPane, SplitPane innerSplitPane) {
        System.out.println("Set divider positions...");
        System.out.println("Set divider position 0 of outerSplitPane...");
        outerSplitPane.setDividerPosition(0, outerSplitPane.getItems().size() == 3 ? DIVIDER_POSITION_0
                : DIVIDER_POSITION_1);
        if (outerSplitPane.getItems().size() == 3) {
            System.out.println("Set divider position 1 of outerSplitPane...");
            outerSplitPane.setDividerPosition(1, DIVIDER_POSITION_1);
        }
        System.out.println("Set divider position 0 of innerSplitPane...");
        innerSplitPane.setDividerPosition(0, DIVIDER_POSITION_0);
        System.out.println("Set divider position 1 of innerSplitPane...");
        innerSplitPane.setDividerPosition(1, DIVIDER_POSITION_1);
    }

    private TabPane addTabPane(SplitPane splitPane, String label) {
        TabPane tabPane = new TabPane();
//        tabPane.setFocusTraversable(false);
        Tab tab = new Tab(label);
        tab.setContent(new Label(label));
        tabPane.getTabs().add(tab);
        splitPane.getItems().add(tabPane);
        return tabPane;
    }

    private SplitPane createInnerSplitPane() {
        SplitPane innerSplitPane = createSplitPane("innerSplitPane");
        innerSplitPane.setOrientation(Orientation.VERTICAL);

        TabPane topTabPane = addTabPane(innerSplitPane, "Top");
        TabPane centerTabPane = addTabPane(innerSplitPane, "Center");
        TabPane bottomTabPane = addTabPane(innerSplitPane, "Bottom");

        SplitPane.setResizableWithParent(topTabPane, Boolean.FALSE);
        SplitPane.setResizableWithParent(bottomTabPane, Boolean.FALSE);

        SplitPane.setResizableWithParent(centerTabPane, Boolean.TRUE);

        return innerSplitPane;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

如果关闭左侧选项卡,则顶部和底部选项卡将隐藏(大小为0),尽管在layoutChildren()调用之前设置了分隔符位置(请参阅输出)。

我遇到这种情况,因为我需要在代码中重置splitPane的项目。另请注意,这些项具有不同的resizableWithParent属性。

更新3

这是一个只有一个SplitPane的小型SSCCE。当您按“增长宽度”按钮时,您将看到相同的效果。

package splitpanelayoutissue;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SplitPaneLayoutIssue2 extends Application {

    private static final double DIVIDER_POSITION_0 = 0.10834236186348863;
    private static final double DIVIDER_POSITION_1 = 0.8916576381365114;

    @Override
    public void start(Stage primaryStage) {
        SplitPane splitPane = createSplitPane();

        StackPane root = new StackPane();
        root.getChildren().add(splitPane);

        Scene scene = new Scene(root, 1500, 1000);

        primaryStage.setTitle("SplitPane Layout Issue");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private SplitPane createSplitPane(String name) {
        SplitPane splitPane = new SplitPane() {

            @Override
            protected void layoutChildren() {
                System.out.println("Calling relayout on " + name + "...");
                com.sun.javafx.scene.control.skin.SplitPaneSkin skin = (com.sun.javafx.scene.control.skin.SplitPaneSkin) getSkin();
                super.layoutChildren();
            }

        };
        return splitPane;
    }

    private void setDividerPositions(SplitPane innerSplitPane) {
        System.out.println("Set divider positions...");
        System.out.println("Set divider position 0 of innerSplitPane...");
        innerSplitPane.setDividerPosition(0, DIVIDER_POSITION_0);
        System.out.println("Set divider position 1 of innerSplitPane...");
        innerSplitPane.setDividerPosition(1, DIVIDER_POSITION_1);
    }

    private TabPane addTabPane(SplitPane splitPane, String label) {
        TabPane tabPane = new TabPane();
//        tabPane.setFocusTraversable(false);
        Tab tab = new Tab(label);
        tab.setContent(new Label(label));
        tabPane.getTabs().add(tab);
        splitPane.getItems().add(tabPane);
        return tabPane;
    }

    private SplitPane createSplitPane() {
        SplitPane splitPane = createSplitPane("splitPane");
        splitPane.setOrientation(Orientation.VERTICAL);

        TabPane topTabPane = addTabPane(splitPane, "Top");

        TabPane centerTabPane = new TabPane();
//        tabPane.setFocusTraversable(false);
        Tab tab = new Tab("Center");
        Button button = new Button("Grow width");
        tab.setContent(button);
        button.setOnAction(event -> {
            System.out.println("Growing window width...");
            // the following line causes com.sun.javafx.scene.control.skin.SplitPaneSkin.Content.getArea() 
            // of all 3 items return 0, even though setDividerPositions is called before the next SplitPane.layoutChildren call
            // Without this line the code works fine.
            splitPane.getItems().setAll(splitPane.getItems().get(0), splitPane.getItems().get(1), splitPane.getItems().get(2));

            // This line is needed to trigger the execution path in question
            splitPane.getScene().getWindow().setWidth(splitPane.getScene().getWindow().getWidth() + 20.0);
            setDividerPositions(splitPane);
        });
        centerTabPane.getTabs().add(tab);

        splitPane.getItems().add(centerTabPane);

        TabPane bottomTabPane = addTabPane(splitPane, "Bottom");

        setDividerPositions(splitPane);
        SplitPane.setResizableWithParent(topTabPane, Boolean.FALSE);
        SplitPane.setResizableWithParent(bottomTabPane, Boolean.FALSE);

        SplitPane.setResizableWithParent(centerTabPane, Boolean.TRUE);

        return splitPane;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

这是一个JavaFX错误还是我可以以某种方式影响的东西?

3 个答案:

答案 0 :(得分:2)

我认为堆栈跟踪的重要部分是:

  

com.sun.javafx.scene.control.skin.SplitPaneSkin.setAbsoluteDividerPos(SplitPaneSkin.java:310)           在   com.sun.javafx.scene.control.skin.SplitPaneSkin.setupContentAndDividerForLayout(SplitPaneSkin.java:502)           在   com.sun.javafx.scene.control.skin.SplitPaneSkin.layoutChildren(SplitPaneSkin.java:817)

正如您所看到的,SplitFane的JavaFX默认布局机制(layoutChildren())以一种可能影响分隔符位置的方式实现。在layoutChildren()方法的一些内部if语句中调用setupContentAndDividerForLayout()方法。因此,我将调试此方法并检查调用setupContentAndDividerForLayout()的原因。通过这样做,您可以找到SplitPane认为必须更改分隔符位置的原因。

答案 1 :(得分:1)

这似乎是最近在JavaFX中引入的内容。我在Windows 7上使用Java 8u20(b26)和Java 8u40ea(早期采用者)(b23)进行了测试。

使用你的SSCCE,我看到使用Java 8u20,如果我将窗口的高度调整为更小,然后单击Grow Width,则上下标签保持与之前相同的高度。

如果我使用Java 8u40ea做同样的事情,当我将窗口调整为较小并随后单击按钮时,上部和下部选项卡会将其自身的高度调整为较小。如果我再次调整窗口大小,当我单击按钮时,标签会再次调整为大(如前所述)。

听起来像JavaFX家伙打开的东西;我个人在几天前亲自提交了一份SSCCE,他们反应灵敏。

答案 2 :(得分:0)

由于它似乎是一个JavaFX问题,我在此处提出了一个问题:https://javafx-jira.kenai.com/browse/RT-40031

<强>更新

该问题可以重现,但已在即将发布的JavaFX 8u40版本中得到修复。