简体   繁体   中英

Jtable with expandable jtable for each row

I'm trying to obtain a table where you can collapse rows using a + or whatever symbol, like in this image from Access:

在此处输入图片说明

Actually, I would like to have the same behaviour, with the title of the columns like that. I will use only 2 levels: the one that have the + and the ones that do not have (different parents with different childs but childs won't be parents). At the moment I am trying Jtable inside Jtree but I'm quite far from the objective and the results are not near... I cannot edit table cells neither I can have the column names like this case (If I put the title of columns each parent will have the title). Do you know some java swing component with a behaviour like the one that I want to obtain or some code that could do this? Any help with the code I wrote?

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;
  }
}

For those looking for something similar, I found this:

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

If you want to use swingX, every link I found was broken (java.net does not exist anymore) and I couldn't find an official repository. But still, you can find the last upload from the SwingX project at several github personal projects who had replicated it like this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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