把JTable放在JTree中 [英] Put JTable in the JTree

查看:172
本文介绍了把JTable放在JTree中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与线程 Jtable作为Jtree节点有关我把JTable放到JTree,但 JTree 在start_up上无法正确显示视图,我如何 setPreferredSize for JTable ,因为 PreferredScrollableViewportSize 收缩 JTable ,渲染 TableHeader +一个,一个保持隐藏,但在展开Node后,TreeRenderer会更改并重新绘制 setPreferredSize 到预期的维度

in connection with thread Jtable as a Jtree Node I put JTable to JTree, but JTree View isn't rendered correctly on start_up, how can I setPreferredSize for JTable, because PreferredScrollableViewportSize shrinked JTable with rendering TableHeader + one Row, one Row remain hidden, but after expanding Node(s) TreeRenderer change and repaint the setPreferredSize to the expected Dimension

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.*;

public class TreeWithTableRenderer extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTree tree;

    public TreeWithTableRenderer() {
        DefaultMutableTreeNode AA1 = new DefaultMutableTreeNode("AA1");
        DefaultMutableTreeNode AA2 = new DefaultMutableTreeNode("AA2");
        DefaultMutableTreeNode A = new DefaultMutableTreeNode("A");
        A.add(AA1);
        A.add(AA2);
        DefaultMutableTreeNode BB1 = new DefaultMutableTreeNode("BB1");
        DefaultMutableTreeNode BB2 = new DefaultMutableTreeNode("BB2");
        DefaultMutableTreeNode B = new DefaultMutableTreeNode("B");
        B.add(BB1);
        B.add(BB2);
        DefaultMutableTreeNode CC1 = new DefaultMutableTreeNode("CC1");
        DefaultMutableTreeNode CC2 = new DefaultMutableTreeNode("CC2");
        DefaultMutableTreeNode C = new DefaultMutableTreeNode("C");
        C.add(CC1);
        C.add(CC2);
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
        root.add(A);
        root.add(B);
        root.add(C);
        tree = new JTree(root);
        tree.setCellRenderer(new MyTableInTreeCellRenderer());
        tree.setRowHeight(0);
        JScrollPane jsp = new JScrollPane(tree);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(jsp, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
    }

    class MyTableInTreeCellRenderer extends JPanel implements TreeCellRenderer {

        private static final long serialVersionUID = 1L;
        private JTable table;

        public MyTableInTreeCellRenderer() {
            super(new BorderLayout());
            table = new JTable();
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
        }

        public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            final String v = (String) ((DefaultMutableTreeNode) value).getUserObject();
            table.setModel(new DefaultTableModel() {

                private static final long serialVersionUID = 1L;

                @Override
                public int getRowCount() {
                    return 2;
                }

                @Override
                public int getColumnCount() {
                    return 2;
                }

                @Override
                public Object getValueAt(int row, int column) {
                    return v + ":" + row + ":" + column;
                }
            });
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            return this;
        }
    }

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

            public void run() {
                new TreeWithTableRenderer().setVisible(true);
            }
        });
    }
}


推荐答案

获取摆脱scrollPane,无论如何它都是功能失调的(到目前为止我同意Russell :-)并使用适当的LayoutManager将表及其标题添加到面板中:

Get rid of the scrollPane, it's dysfunctional anyway (so far I agree with Russell :-) and add the table and its header to the panel, using an appropriate LayoutManager:

public MyTableInTreeCellRenderer() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    table = new JTable();
    add(table.getTableHeader());
    add(table);
}

你可能需要稍微调整视觉效果,左边和顶部边框线丢失 - 不完全确定哪个组件正常绘制它们,可能是scrollPane

you'll probably need to tweak the visuals a bit, the left and top border lines are missing - not entirely sure which component paints them normally, could be the scrollPane

编辑

忘记:在计算渲染组件所需大小(在ui委托中完成,即VariableHeightLayoutCache)时查询scrollPane的prefSize的原因是scrollPane尚未配置标题。查询在面板添加到rendererPane之前发生,完整配置在表的addNotify中完成,只有在将面板添加到层次结构后才会发生

Forgot: the reason querying the scrollPane's prefSize in calculating the required size (done in the ui delegate, namely the VariableHeightLayoutCache) of the rendering component is that the scrollPane not yet configured with the header. The query happens before the panel is added to the rendererPane, the complete configuration is done in the table's addNotify which happens only after adding the panel to the hierarchy

这篇关于把JTable放在JTree中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆