2012-01-12 105 views
8

我知道你可以創建一個table and add a JTree as a column。但我想要做的是完全相反。Jtable作爲Jtree節點

檢查圖像,並告訴我這是否可能。謝謝!

enter image description here

UPDATE:

通過使用MKorbel的代碼,並通過隨機列數用下面的代碼:

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

我能夠獲得下面的圖片:

enter image description here

+1

我還沒有測試過它,但爲什麼不應該這樣做呢。 API請求呈示器返回組件。而JTable是一個組件。在JTable編輯/選擇可能會更困難 – Robin 2012-01-12 17:01:06

+0

@Robin但我的意思是視覺上,它是否合適?它是否有任何困難? – 2012-01-12 17:10:43

+0

現在這很有趣。我的2美分:我覺得如果你的桌子很大,你的樹可能看起來很時髦。另外,我很好奇你是否真的能夠與桌子互動。它只是一張桌子的圖像? – user489041 2012-01-12 17:21:43

回答

7

1)I沒有解決在RendererisLeaf()Editor不知何故

2)如果我把JScrollPaneJTableNode直接(不使用JPanel作爲parent)然後JTree View未正確START_UP用於後呈現更多信息請看我的問題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); 
      } 
     }); 
    } 
} 
+0

謝謝你的代碼。我修改了一下,以獲得我的問題中的更新結果。我只是想知道我用最好的方法修改了什麼? – 2012-01-13 09:04:31

+0

但表格不可編輯。如何編輯表格的內容 – Madhan 2015-10-23 07:45:38