简体   繁体   English

jtree中具有不同行数的jtable

[英]jtable in jtree with different row count

I'm trying to create a JTree that consists of JTables. 我正在尝试创建一个由JTables组成的JTree。 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. 到目前为止,我已经成功地使用Jtables创建了一个Jtree。但是,我无法更改特定树节点表的行数。 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 Jtable作为Jtree节点

I wrote the following code by the recommendation of Trashgod; 我根据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| | node1 |

   |a|b|
   |c|d|

|node2| | node2 |

   |e|f|

|node3| | node3 |

   |g|h|
   |i|j|
   |k|m|

Instead of rendering the tables in the tree, add a TreeSelectionListener and update a single JTable in an adjacent component. 而不是树中呈现表而是添加TreeSelectionListener并更新相邻组件中的单个JTable Let each TreeNode contain a Tablemodel , and use setModel() to update the JTable . 让每个TreeNode包含一个Tablemodel ,并使用setModel()更新JTable Several related examples are cited here . 这里列举几个相关的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM