java—同时使用两个不同的呈现器显示同一个jtree示例

dw1jzc5e  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(294)

我试图可视化一个diff树,它表示两个树结构之间的差异。通常这种可视化使用组件的两个示例来显示左侧和右侧,并向用户指出它们之间的差异,但在我的例子中,diffing算法的实际结果是一棵树,其中每个节点可能同时承担两个不同的角色(左角色和右角色)。
在用户看来,它必须像有两个 JTree 它挨着彼此,肩并肩。实现如何实现这一点无关紧要。我目前的方法是使用两个同步的 JTree 共享模型的示例。是否有另一种实现方法,即使用单个 JTree 示例是否达到相同的效果?
2 JTree s、 并排,是一种要求。
这不是我可以提供一个可行的代码示例的问题,但这里有一个可切换的渲染器,它模拟了我的树模型:

package org.stackoverflow.example;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class SameTreeNodesRenderedDifferently extends JFrame {

    private JTree tree;
    private JButton button;
    private MyTreeCellRenderer leftRenderer;
    private MyTreeCellRenderer rightRenderer;
    private MyTreeCellRenderer currentRenderer;

    public SameTreeNodesRenderedDifferently() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout()); 
        setTitle("SameTreeNodesRenderedDifferently");

        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new MyNode("root"));
        createTree(root);
        tree = new JTree(root);
        JScrollPane scroll = new JScrollPane(tree);
        add(scroll);

        leftRenderer = new MyTreeCellRenderer(true);
        rightRenderer = new MyTreeCellRenderer(false);
        currentRenderer = leftRenderer;
        tree.setCellRenderer(currentRenderer);
        expandAll();

        button = new JButton("Switch renderer");
        add(button, BorderLayout.PAGE_END);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                currentRenderer = currentRenderer == leftRenderer ? rightRenderer : leftRenderer;
                tree.setCellRenderer(currentRenderer);
                tree.revalidate();
                tree.repaint();
                expandAll();
            }
        });
        setSize(400, 400);
        setLocationRelativeTo(null);
    }

    private void createTree(DefaultMutableTreeNode root) {
        DefaultMutableTreeNode a = new DefaultMutableTreeNode(new MyNode("a"));
        root.add(a);
        DefaultMutableTreeNode b = new DefaultMutableTreeNode(new MyNode("b", "!b"));
        a.add(b);
        DefaultMutableTreeNode c = new DefaultMutableTreeNode(new MyNode("c"));
        a.add(c);
        DefaultMutableTreeNode d = new DefaultMutableTreeNode(new MyNode("d"));
        root.add(d);        
        DefaultMutableTreeNode e = new DefaultMutableTreeNode(new MyNode("e", "!e"));
        d.add(e);       
        DefaultMutableTreeNode f = new DefaultMutableTreeNode(new MyNode("f", "!f"));
        d.add(f);       
        DefaultMutableTreeNode g = new DefaultMutableTreeNode(new MyNode("g", "!g"));
        d.add(g);       
        DefaultMutableTreeNode h = new DefaultMutableTreeNode(new MyNode("h"));
        d.add(h);
    }

    private void expandAll() {
        for (int i = 0; i < tree.getRowCount(); i++) {
            tree.expandRow(i);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SameTreeNodesRenderedDifferently().setVisible(true);
            }
        });
    }

    private static class MyTreeCellRenderer extends DefaultTreeCellRenderer {

        private final boolean left;

        public MyTreeCellRenderer(boolean left) {
            this.left = left;
        }

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (value instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) value;
                MyNode node = (MyNode) dmtn.getUserObject();
                setText(left ? node.leftValue : node.rightValue);
            }
            return this;
        }

    }

    public static class MyNode {
        private final String leftValue;
        private final String rightValue;

        public MyNode(String value) {
            this(value, value);
        }

        public MyNode(String leftValue, String rightValue) {
            this.leftValue = leftValue;
            this.rightValue = rightValue;
        }

        public String getLeftValue() {
            return leftValue;
        }

        public String getRightValue() {
            return rightValue;
        }

        @Override
        public String toString() {
            return "MyNode{" + "leftValue=" + leftValue + ", rightValue=" + rightValue + '}';
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题