2

JXMultiSplitPaneのノードを非表示にすると、ペイントの問題が発生します。次のSSCCEは、この動作を示しています。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Leaf;
import org.jdesktop.swingx.MultiSplitLayout.Node;
import org.jdesktop.swingx.MultiSplitLayout.Split;

public class TestMultiSplitPane {

    public static final String LEFT = "left";
    public static final String CENTER = "center";
    public static final String RIGHT = "right";

    public static final String TOP = "top";
    public static final String MIDDLE = "middle";
    public static final String BOTTOM = "bottom";

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Split horizontal = new Split();
        Split left = getVerticalSplit(LEFT, 0.5, 0.5);
        left.setWeight(0.2);
        Split center = getVerticalSplit(CENTER, 0.8, 0.2);
        center.setWeight(0.6);
        Split right = getVerticalSplit(RIGHT, 0.5, 0.5);
        right.setWeight(0.2);
        horizontal.setChildren(left, new Divider(), center, new Divider(), right);
        MultiSplitLayout layout = new MultiSplitLayout(horizontal);
        JXMultiSplitPane splitPane = new JXMultiSplitPane(layout);
        addButton(LEFT + TOP, splitPane);
        addButton(CENTER + TOP, splitPane);
        addButton(RIGHT + TOP, splitPane);
        addButton(LEFT + BOTTOM, splitPane);
        addButton(CENTER + BOTTOM, splitPane);
        addButton(RIGHT + BOTTOM, splitPane);
        frame.add(splitPane);
        frame.setBounds(50, 50, 1024, 768);
        frame.setVisible(true);
    }

    protected void addButton(final String buttonName, final JXMultiSplitPane splitPane) {
        final JButton button = new JButton(buttonName);
        splitPane.add(buttonName, button);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MultiSplitLayout layout = splitPane.getMultiSplitLayout();
                Node node = layout.getNodeForName(buttonName);
                Split parent = node.getParent();
                parent.hide(node);
                splitPane.invalidate();
                splitPane.revalidate();
                splitPane.repaint();
            }
        });
    }

    public Split getVerticalSplit(String name, double topWeight, double bottomWeight) {
        Split split = new Split();
        split.setRowLayout(false);
        Leaf top = new Leaf(name + TOP);
        top.setWeight(topWeight);
        Leaf bottom = new Leaf(name + BOTTOM);
        bottom.setWeight(bottomWeight);
        split.setChildren(top, new Divider(), bottom);
        return split;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestMultiSplitPane().initUI();

            }
        });
    }

}

表示されたボタンをクリックすると非表示になりますが、マウスを別のボタンの上に移動すると、分割ペインの再描画に問題が発生します。

Mavenを使用する場合、必要な依存関係は次のとおりです。

    <dependency>
        <groupId>org.swinglabs.swingx</groupId>
        <artifactId>swingx-core</artifactId>
        <version>1.6.3</version>
    </dependency>
4

2 に答える 2

1

私は最終的に解決策を見つけました。レイアウトでノードを非表示にするには、メソッドを使用するだけdisplayNode(String, boolean)です。

したがって、で、actionPerformed私が呼び出す必要があるのはこれだけでした:

layout.displayNode(buttonName, false);

完全に機能するコード:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Leaf;
import org.jdesktop.swingx.MultiSplitLayout.Split;

public class TestMultiSplitPane {

    public static final String LEFT = "left";
    public static final String CENTER = "center";
    public static final String RIGHT = "right";

    public static final String TOP = "top";
    public static final String MIDDLE = "middle";
    public static final String BOTTOM = "bottom";

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Split horizontal = new Split();
        Split left = getVerticalSplit(LEFT, 0.5, 0.5);
        left.setWeight(0.2);
        Split center = getVerticalSplit(CENTER, 0.8, 0.2);
        center.setWeight(0.6);
        Split right = getVerticalSplit(RIGHT, 0.5, 0.5);
        right.setWeight(0.2);
        horizontal.setChildren(left, new Divider(), center, new Divider(), right);
        MultiSplitLayout layout = new MultiSplitLayout(horizontal);
        JXMultiSplitPane splitPane = new JXMultiSplitPane(layout);
        addButton(LEFT + TOP, splitPane);
        addButton(CENTER + TOP, splitPane);
        addButton(RIGHT + TOP, splitPane);
        addButton(LEFT + BOTTOM, splitPane);
        addButton(CENTER + BOTTOM, splitPane);
        addButton(RIGHT + BOTTOM, splitPane);
        frame.add(splitPane);
        frame.setBounds(50, 50, 1024, 768);
        frame.setVisible(true);
    }

    protected void addButton(final String buttonName, final JXMultiSplitPane splitPane) {
        final JButton button = new JButton(buttonName);
        splitPane.add(buttonName, button);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MultiSplitLayout layout = splitPane.getMultiSplitLayout();
                layout.displayNode(buttonName, false);
            }
        });
    }

    public Split getVerticalSplit(String name, double topWeight, double bottomWeight) {
        Split split = new Split();
        split.setRowLayout(false);
        Leaf top = new Leaf(name + TOP);
        top.setWeight(topWeight);
        Leaf bottom = new Leaf(name + BOTTOM);
        bottom.setWeight(bottomWeight);
        split.setChildren(top, new Divider(), bottom);
        return split;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestMultiSplitPane().initUI();

            }
        });
    }
}
于 2013-03-14T13:55:49.307 に答える
1

I know it's been a while since you asked the question but I searched a lot until I found a solution (not a great one though) and maybe this will help someone.
I do a lot of add/remove components in the panels I add to the splitPane and if I resized a splitter before adding components it got stuck there and either cut out part of my panel or get to big.

As it says in the documentation:

MultiSplitLayout's "floatingDividers" property, initially true, is set to false by the MultiSplitPane as soon as any Divider is repositioned. When floatingDividers is false, the right/bottom edge of each Leaf (component) is defined by the location of the Divider that follows it. In other words, once the user moves a Divider, the layout no longer depends on the preferred size of any components, it's defined by the current position of the Dividers and the weights.

What I did was call the setFloatingDividers(true) method on each resize I did (resizes related to add/remove components). The problem that arises from this is that the position of the splitters is lost.

于 2013-03-14T13:48:39.000 に答える