0

I'm trying to create a JTree that consists of JTables. So far, i succeeded in creating a Jtree with Jtables.But, I cant change the row count of a table of a specific tree node. Whenever i try to adjust the row count, all of the tree's node's row count changes.

I used the code at the following link:

Jtable as a Jtree Node

I wrote the following code by the recommendation of Trashgod; but it didnt work; could you please give some working code..

package helperPack;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;

public class JTreeTrial extends JFrame {

/**
 * @param args
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode("node");              
            JTree tree = new JTree(parentNode);
            JTable table = new JTable();
            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  ":" + "row" + ":" + column;
                 }
             });
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getModel().getRoot();
            node.setUserObject(table);

            JTreeTrial trial=new JTreeTrial();
            trial.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JScrollPane jsp = new JScrollPane(tree);
            trial.add(jsp, BorderLayout.CENTER);
            trial.pack();
            trial.setVisible(true);
            trial.setLocationRelativeTo(null);

        }
    });

}
}

Example :

|node1|

   |a|b|
   |c|d|

|node2|

   |e|f|

|node3|

   |g|h|
   |i|j|
   |k|m|
Community
  • 1
  • 1
age
  • 1
  • 1

1 Answers1

1

Instead of rendering the tables in the tree, add a TreeSelectionListener and update a single JTable in an adjacent component. Let each TreeNode contain a Tablemodel, and use setModel() to update the JTable. Several related examples are cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • What's not working in the examples cited? Please edit your question to include an [sscce](http://sscce.org/) that shows your current approach. – trashgod Jun 22 '13 at 10:32