Jtable,每行都有可扩展的jtable

时间:2018-04-14 15:02:44

标签: java jtable jtree jtreetable

我试图获取一个表,您可以使用+或任何符号折叠行,例如来自Access的此图像:

enter image description here

实际上,我希望有相同的行为,列的标题就是这样。我将只使用2个级别:具有+和不具有的级别(不同的父母具有不同的孩子,但孩子不会成为父母)。目前我在Jtree中尝试Jtable,但我离目标很远,结果并不接近......我无法编辑表格单元格我也不能像这种情况那样拥有列名称(如果我把标题列的每个父母都有标题)。你知道一些java swing组件有一个像我想要获得的行为或一些代码可以做到这一点吗?对我写的代码有任何帮助吗?

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.EventObject;

//  www .ja va  2s  .  c  o m
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

public class main extends JFrame {

  private JTree tree;

  public main() {
    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.setRootVisible(false);
    tree.setShowsRootHandles(true);
    for (int i = 0; i<tree.getRowCount(); i++){
        tree.expandRow(i);
    }
    tree.setCellRenderer(new MyTableInTreeCellRenderer());
    tree.setRowHeight(0);
    JScrollPane jsp = new JScrollPane(tree);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(jsp, BorderLayout.CENTER);
    pack();
  }

  public static void main(String[] args) throws Exception {

    new main().setVisible(true);

  }
}

class MyTableInTreeCellRenderer extends JPanel implements TreeCellRenderer {

  private JTable table;

  public MyTableInTreeCellRenderer() {
    super(new BorderLayout());
    table = new JTable();    
    JScrollPane scrollPane = new JScrollPane(table);
    table.setTableHeader(null);
    table.setAutoCreateRowSorter(true);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    table.setCellSelectionEnabled(true);
    table.setColumnSelectionAllowed(true);

    //No column ordering. Must be before setModel
    table.setColumnModel(new DefaultTableColumnModel() {
        /**
         * 
         */
        private static final long serialVersionUID = 5;

        public void moveColumn(int columnIndex, int newIndex) {
                super.moveColumn(columnIndex, columnIndex);
        }
    });
    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() {
      @Override
      public int getRowCount() {
        return 1;
      }
      @Override
      public int getColumnCount() {
        return 3;
      }
      @Override
      public Object getValueAt(int row, int column) {
        return v + ":" + row + ":" + column;
      }
    });
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    return this;
  }
}

1 个答案:

答案 0 :(得分:0)

对于那些寻找类似东西的人,我发现了这个:

http://www.hameister.org/JavaSwingTreeTable.html

如果你想使用swingX,我发现的每个链接都被破坏了(java.net不再存在),我找不到官方存储库。但是,您仍然可以在几个github个人项目中找到SwingX项目的最后一次上传,这些项目像这样复制了它:

https://github.com/arotenberg/swingx/releases