2014-03-30 45 views
0

這裏是我的代碼我的數據在它JXTreeTable沒有顯示

DemoJFrame.java 

package com.suru; 

import javax.swing.UIManager; 
import org.jdesktop.swingx.treetable.TreeTableModel; 

public class DemoJFrame extends javax.swing.JFrame { 

    private javax.swing.JScrollPane jScrollPane1; 
    private org.jdesktop.swingx.JXTreeTable treeTable; 
    private final TreeTableModel gen; 

    public DemoJFrame() { 
     gen = new TreeTableGen(); 
     initComponents(); 
     setSize(600, 500); 
     setLocationRelativeTo(this); 
    } 

    private void initComponents() { 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     treeTable = new org.jdesktop.swingx.JXTreeTable(); 
     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setTitle("JXTreeTable Demo"); 
     treeTable.setTreeTableModel(gen); 
     jScrollPane1.setViewportView(treeTable); 
     getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); 
     pack(); 
    } 

    public static void main(String args[]) throws Exception { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new DemoJFrame().setVisible(true); 
      } 
     }); 
    } 
} 

public class DataBean { 
    int id; 
    String data; 
    int amount; 
} 

public class MainDataBean { 
    int id; 
    String name; 
    int total; 
    ArrayList<DataBean> dataBeans; 
} 


//MyTableModelGenerator 

package com.suru; 

import java.util.ArrayList; 
import org.jdesktop.swingx.treetable.DefaultTreeTableModel; 

public class TreeTableGen extends DefaultTreeTableModel { 

    private static final String[] COLUMNS = new String[]{ 
     "ID", "Name", "Amount" 
    }; 
    ArrayList<MainDataBean> arrayList; 

    public TreeTableGen() { 
     // data source class 
     // printing data below 
     DataSource dataSource = new DataSource(); 
     arrayList = dataSource.getMainDataBeans(); 

     for (MainDataBean bean : arrayList) { 
      System.out.println(bean); 
      ArrayList<DataBean> dataBeans = bean.getDataBeans(); 
      for (DataBean dataBean : dataBeans) { 
       System.out.println(dataBean); 
      } 
      System.out.println("=========================================="); 
     } 
    } 

    @Override 
    public boolean isCellEditable(Object node, int column) { 
     return false; 
    } 

    @Override 
    public boolean isLeaf(Object node) { 
     return node instanceof DataBean; 
    } 

    @Override 
    public String getColumnName(int columnIndex) { 
     return COLUMNS[columnIndex]; 
    } 

    @Override 
    public int getColumnCount() { 
     return COLUMNS.length; 
    } 

    @Override 
    public Object getValueAt(Object node, int columnIndex) { 
     if (node instanceof MainDataBean) { 
      MainDataBean data = (MainDataBean) node; 
      if (columnIndex == 0) { 
       return data.getId() + ""; 
      } else if (columnIndex == 1) { 
       return data.getName(); 
      } else if (columnIndex == 2) { 
       return data.getTotal() + ""; 
      } else { 
       return "data not found! at 1"; 
      } 
     } else if (node instanceof DataBean) { 
      DataBean data = (DataBean) node; 
      if (columnIndex == 0) { 
       return data.getId() + ""; 
      } else if (columnIndex == 1) { 
       return data.getData(); 
      } else if (columnIndex == 2) { 
       return data.getAmount() + ""; 
      } else { 
       return "data not found! at 2"; 
      } 
     } else { 
      return null; 
     } 
    } 

    @Override 
    public Object getChild(Object parent, int index) { 
     return ((MainDataBean) parent).getDataBeans().get(index); 
    } 

    @Override 
    public int getChildCount(Object parent) { 
     if (parent instanceof MainDataBean) { 
      return ((MainDataBean) parent).getDataBeans().size(); 
     } else { 
      return arrayList.size(); 
     } 
    } 

    @Override 
    public int getIndexOfChild(Object parent, Object child) { 
     MainDataBean pa = ((MainDataBean) parent); 
     DataBean ch = ((DataBean) child); 
     ArrayList<DataBean> dataBeans = pa.getDataBeans(); 
     for (int i = 0; i < dataBeans.size(); i++) { 
      DataBean dataBean = dataBeans.get(i); 
      if (dataBean.getId() == ch.getId()) { 
       return i; 
      } 
     } 
     return -1; 
    } 

} 


my output in console as follows: 

MainDataBean{id=1, name=bean1, total=2974} 
DataBean{id=10, data=val (1, 0), amount=900} 
DataBean{id=11, data=val (1, 1), amount=104} 
DataBean{id=12, data=val (1, 2), amount=861} 
DataBean{id=13, data=val (1, 3), amount=947} 
DataBean{id=14, data=val (1, 4), amount=162} 
========================================== 
MainDataBean{id=2, name=bean2, total=2215} 
DataBean{id=20, data=val (2, 0), amount=141} 
DataBean{id=21, data=val (2, 1), amount=196} 
DataBean{id=22, data=val (2, 2), amount=769} 
DataBean{id=23, data=val (2, 3), amount=146} 
DataBean{id=24, data=val (2, 4), amount=963} 
========================================== 
MainDataBean{id=3, name=bean3, total=2816} 
DataBean{id=30, data=val (3, 0), amount=810} 
DataBean{id=31, data=val (3, 1), amount=675} 
DataBean{id=32, data=val (3, 2), amount=895} 
DataBean{id=33, data=val (3, 3), amount=290} 
DataBean{id=34, data=val (3, 4), amount=146} 
========================================== 
MainDataBean{id=4, name=bean4, total=1537} 
DataBean{id=40, data=val (4, 0), amount=38} 
..... 
..... 
but it is not reflecting in my frame 

我的輸出屏幕截圖自己做錯了什麼:

Screen Shot

回答

0

你不及格DefaultTreeTableModel根節點。

因此,DefaultTreeTableModel正在創建自己的空根節點,然後你不會添加任何東西。

你可以添加一些東西到根節點或添加你自己的根節點。