참조


- 결과화면


sun 에 기고된 기사를 토대로 따라 하다가 좀더 쉽게.. 사용하기 편한 예제를 만들어 보자는 취지로 만들게 됨.


1. DefaultMutableTreeNode 를 사용해서 트리노드를 만들다.
2. 생성된 노드로 TreeTableModel 을 만든다.
3. 데이터부가 완성되면 JTreeTable 을 생성하고
4. 여기서.. 아무 생각없이 (Netbeans 만 사용하다보니.. 사악한 마법사 땜시 고생함.) add 시켰다가 낭패봄.

    setViewportView() 사용해서 화면에 TreeTable을 보여줌.
// 1번.
DefaultMutableTreeNode root = getTreeNode();

// 2번.
TreeTableModel model = new BookTreeTableModel(root);

// 3번.
JTreeTable treeTable = new JTreeTable(model);

// 4번.
treeTableScrollPane.setViewportView(treeTable);
 
// TreeTable top-level container 배경색 변경.
treeTable.getParent().setBackground(Color.white);

 나머지 잡다한 코드들..
// 1번.

private DefaultMutableTreeNode getTreeNode() {
    DefaultMutableTreeNode root = makeRootNode("BOOK");
    DefaultMutableTreeNode java = makeRootNode("JAVA");
    DefaultMutableTreeNode novel = makeRootNode("NOVEL");

    root.add(java);
    root.add(novel);

    java.add(makeChildNode("concurrency in practice", "35,000원", "2009/12/16", "자바 병렬 프로그래밍"));      
    java.add(makeChildNode("Java FX", "29,000원", "2009/12/16", "자바 GUI 프로그래밍"));
    java.add(makeChildNode("Java Performance Fundamental", "29,000원", "2009/12/16", "JVM 관련 책"));

    novel.add(makeChildNode("람세스", "35,000원", "2009/12/16", "이집트 미친왕 이야기"));
    novel.add(makeChildNode("로마인 이야기", "29,000원", "2009/12/16", "로마사람들 전쟁이야기"));
    novel.add(makeChildNode("영웅문", "29,000원", "2009/12/16", "영웅이 문을 만드는 이야기"));
  
    return root;
}

private DefaultMutableTreeNode makeRootNode(String title) {
    Book book = new Book();
    book.setTitle(title);

    return new DefaultMutableTreeNode(book);
}

private DefaultMutableTreeNode makeChildNode(String title, String cost, String date, String description) {
    Book book = new Book();
    book.setTitle(title);
    book.setCost(cost);
    book.setDate(date);
    book.setDescription(description);
    return new DefaultMutableTreeNode(book);
}

BookTreeTableModel.java
package swing_sample.treetable.samplemodel;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import swing_sample.treetable.sunsource.AbstractTreeTableModel;
import swing_sample.treetable.sunsource.TreeTableModel;

/**
 *
 * @author Administrator
 */
public class BookTreeTableModel extends AbstractTreeTableModel {

    public BookTreeTableModel(Object root)
    {
        super(root);
    }

    /**
     * Error in AbstractTreeTableModel !!!
     * Without overriding this method you can't expand the tree!
     */
    public Class getColumnClass(int column) {
          switch (column)
          {
              case 0:
                    return TreeTableModel.class;
              default:
                    return Object.class;
          }
    }

    public Object getChild(Object parent, int index)
    {
          assert parent instanceof MutableTreeNode;
          MutableTreeNode treenode = (MutableTreeNode) parent;
          return treenode.getChildAt(index);
    }

    public int getChildCount(Object parent)
    {
          assert parent instanceof MutableTreeNode;
          MutableTreeNode treenode = (MutableTreeNode) parent;
          return treenode.getChildCount();
    }

    public int getColumnCount()
    {
        return 4;
    }

    public String getColumnName(int column)
    {
          switch (column)
          {
              case 0:
                    return "title";
              case 1:
                    return "cost";
              case 2:
                    return "date";
              case 3:
                    return "description";

              default:
                    return null;
          }

    }

    public Object getValueAt(Object node, int column)
    {
          assert node instanceof DefaultMutableTreeNode;
          DefaultMutableTreeNode treenode = (DefaultMutableTreeNode) node;
          Book book = (Book) treenode.getUserObject();
        
          switch (column)
          {
              case 0:
                  return book.getTitle();
              case 1:
                return book.getCost();
              case 2:
                  return book.getDate();
              case 3:
                return book.getDescription();
          default:
            return null;
          }

    }
}

Book.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swing_sample.treetable.samplemodel;

/**
 *
 * @author Administrator
 */
public class Book {

    String title;
    String cost;
    String date;
    String description;

    public String getCost() {
        return cost;
    }

    public void setCost(String cost) {
        this.cost = cost;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String toString()
    {
        return title;
    }
}

Posted by 짱가쟁이