splitpane布局问题

q0qdq0h2  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(404)

这是一个交叉柱:http://mail.openjdk.java.net/pipermail/openjfx-dev/2015-january/016437.html
有时,当我以编程方式设置splitpane的分隔符位置时,我的值会再次被一些javafx内部布局代码覆盖。
出于调试的目的,我在divider的position属性中添加了一个监听器,并抛出了一个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有效地“隐藏”第一个和第三个(最后一个)项目。
两个分隔符在布局过程中发生了如下变化:
分隔器位置更改:旧值:0.10857763300760044,新值:0.004343105320304018
分隔线位置已更改:旧值:0.8914223669923995,新值:0.9956894679696
我看过com.sun.javafx.scene.control.skin.splitpaneskin(jdk提供的源代码),但不幸的是,在这种情况下,要看到面积是如何计算的并不容易。
为什么这里要重新布局?为什么它会覆盖我为分隔线位置设置的值?怎么才能阻止这一切?
我试着写一个sscce,但是我还不能用一个简单的例子来重现这个问题(这个问题发生在自定义控件的外观实现中。)
更新
有问题的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\u computed\u 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的项。还请注意,这些项具有不同的resizebleithparent属性。
更新3
这是一个较小的sscce,只有一个splitpane。当你按下“增加宽度”按钮,你会看到同样的效果。

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错误还是我能影响的东西?

bvuwiixz

bvuwiixz1#

它似乎是最近引入javafx的东西。我用java8u20(b26)和java8u40ea(早期采用者)(b23)在windows7上对此进行了测试。
使用您的sscce,我看到在java8u20中,如果我将窗口的高度调整为更小,然后单击growthwidth,那么上下选项卡的高度将保持与以前相同。
如果我用java8u40ea做同样的事情,那么当我将窗口调整为更小并随后单击按钮时,上面和下面的选项卡将自己的高度调整为更小。如果我再次将窗口调整为高,当我单击按钮时,选项卡将再次调整为大(与以前一样)。
听上去像是要和javafx的家伙一起打开的东西;就在几天前,我个人提交了一份sscce,他们的React非常迅速。

yv5phkfx

yv5phkfx2#

我认为堆栈跟踪的重要部分是:
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)
正如您所看到的,javafx默认布局机制(layoutchildren())是为splitpane实现的,其实现方式可能会影响分隔符的位置。setupcontentanddividerforlayout()方法在layoutchildren()方法中的一些内部if语句中调用。因此,我将调试这个方法并检查为什么调用setupcontentanddividerforlayout()。通过这样做,您可以找到splitpane认为必须更改分隔器位置的原因。

vatpfxk5

vatpfxk53#

由于这似乎是一个javafx问题,我在这里提交了一个问题:https://javafx-jira.kenai.com/browse/rt-40031
更新:
这个问题可以复制,但是已经在即将发布的javafx8u40中修复了。

相关问题