한 번 봐 이렇게하면 첫 번째 열에서 렌더링 나무를 줄 것이다 이전 JTreeTable 코드를 볼 수있는 좋은 시간, 각 열의 셀을 렌더링 할 수있는 자유 수 있습니다 귀하가 원하는 경우 트리 노드의 오른쪽, 확인란 및 레이블을 넣고 JTable에서 TableCellEditors를 사용하게 할 수 있습니다. 경고는 링크의 코드가 작동하지만 약간 뒤얽힌 것입니다.
다른 대안이 있습니다. NetBeans에서 제공하는 Outline
이라는 더 나은 것으로 간주되는 트리 테이블 구현을 데모했습니다 (NetBeans IDE로 개발할 필요는 없지만 jar 만 필요함). This article은 시작하는 것이 얼마나 쉬운지를 나타냅니다.
약 30 분 만에 Eclipse의 개요 트리 테이블 (프로젝트에 가져온 org-netbeans-swing-outline.jar 포함)의 간단한 예제를 실습 할 수있었습니다 :
private void buildFrame() {
frame = new JFrame("Demo");
frame.setSize(300, 300);
addStuffToFrame();
frame.setVisible(true);
}
private void addStuffToFrame() {
MyTreeNode top = new MyTreeNode("top");
createNodes(top);
DefaultTreeModel model = new DefaultTreeModel(top);
//here are the netBeans tree table classes
OutlineModel outlineModel =
DefaultOutlineModel.createOutlineModel(model, new MyRowModel());
Outline outline = new Outline();
outline.setRootVisible(true);
outline.setModel(outlineModel);
frame.getContentPane().add(new JScrollPane(outline));
}
private void createNodes(MyTreeNode top) {
MyTreeNode child = new MyTreeNode("child 2");
top.add(new MyTreeNode("child 1"));
child.add(new MyTreeNode("g-child1"));
child.add(new MyTreeNode("g-child2"));
child.add(new MyTreeNode("g-child3"));
top.add(child);
top.add(new MyTreeNode("child3"));
top.add(new MyTreeNode("child4"));
}
은 내가 JTable
's에 내장 된 체크 박스 렌더링 mechnanism과 잘 연동됩니다 Boolean
의를 개최하는 TreeNode를을 만들 수 있습니다.
public class MyTreeNode extends DefaultMutableTreeNode {
Boolean data1 = null;
Boolean data2 = null;
String name = null;
MyTreeNode (String name) {
this.name=name;
}
void setData1(Boolean val) {data1=val;}
void setData2(Boolean val) {data2=val;}
Boolean getData1() {return data1;}
Boolean getData2() {return data2;}
String getName() {return name;}
}
넷빈즈 RowModel 대신 간단한의 JTree의이 테이블을 만드는 열쇠입니다 :
alt text http://img17.imageshack.us/img17/6643/picture1hz.png
: 여기
public class MyRowModel implements RowModel {
public Class getColumnClass(int col) {
switch (col) {
case 0: return String.class;
case 1: return Boolean.class; //these return class definitions will
case 2: return Boolean.class; //trigger the checkbox rendering
default:return null;
}
}
public int getColumnCount() {
return 3;
}
public String getColumnName(int col) {
return "";
}
public Object getValueFor(Object node, int col) {
MyTreeNode n = (MyTreeNode)node;
switch (col) {
case 0: return n.getName();
case 1: return n.getData1();
case 2: return n.getData2();
default:return null;
}
}
public boolean isCellEditable(Object node, int col) {
return col > 0;
}
public void setValueFor(Object node, int col, Object val) {
MyTreeNode n = (MyTreeNode)node;
if (col == 1) {n.setData1((Boolean)val);}
else if (col == 2) {n.setData2((Boolean)val);}
//EDIT: here is a recursive method to set all children
// selected for one of the two checkboxes as it is
// checked by the parent
for (Enumeration children = n.children();
children.hasMoreElements();) {
MyTreeNode child = (MyTreeNode) children.nextElement();
setValueFor(child, col, val);
}
}
}
단순한이기는하지만, 완성 된 제품입니다 setValueFor
메소드를 업데이트하여 노드의 하위를 반복하고 부모가 수정되었을 때 선택되거나 선택 해제 된 체크 박스를 설정합니다. 디.
출처
2009-09-17 02:43:16
akf
여전히'addStuffToFrame()'메소드를 호출해야합니다. 그것은'Outline'을 설정하고'JFrame'에 표시를 추가하는 것입니다. 내가 거기에 넣은 방법은 데모 응용 프로그램의 목적을위한 것임을 유의하십시오. – akf