Jtable as a Jtree Node
Asked Answered
M

1

8

I know you can create a table and add a JTree as a column. But what I want to do is the complete opposite.

Check the image and tell me if this is possible. Thanks!

enter image description here

UPDATE:

By using MKorbel's code and by randomizing the number of columns with the following code:

@Override
public int getColumnCount() {
    int i = (int) (Math.random( )* 10.0);
    if (i%2 ==0)
        return 2;
    else
        return 3;
}

I was able to get the following image:

enter image description here

Manchu answered 12/1, 2012 at 16:59 Comment(5)
I haven't tested it, but why shouldn't this be possible. The API requests that the renderer returns a component. And a JTable is a component. Editing/selecting in the JTable might be more difficultShiloh
@Shiloh But I mean visually, is it appropriate? Does it present any difficulties?Manchu
Now this is interesting. My 2 cents: I just feel like visually if your tables are big, your tree might look kind of funky. Also, I am curious about whether or not you will actually be able to interact with the table. Would it be just an image of the table?Cheremkhovo
@Cheremkhovo Well in my case, I just use it as a readOnly. but it would interesting to make it editable, ...Manchu
this JTree with JTable in the Node look like very similair as in your picture, put there JPanel --> JScrollPane --> JTable, is JTree will be editable, then you can to edit Cell in the JTable too, +1 nice questionChimp
C
7

1) I didn't solve isLeaf() in Renderer and Editor somehow

2) if I put JScrollPane with JTable to the Node directly, (without using JPanel as parent) then JTree View isn't correctly rendered after start_up for more info please see my question Put JTable in the JTree

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);
            }
        });
    }
}
Chimp answered 12/1, 2012 at 23:5 Comment(2)
Thank you for your code. I modified it a bit to obtain the Updated result in my question. I was just wondering if what I modified with the best way to do it?Manchu
But the Table is not editable.How to edit the contents of the tableHelp

© 2022 - 2024 — McMap. All rights reserved.