サンプルコード
import java.awt.* import javax.swing.* import javax.swing.tree.* import groovy.swing.* class MyNode extends DefaultMutableTreeNode { MyNode(params) { super(params) } public String toString() { return userObject?.name } } class MyTreeRenderer implements TreeCellRenderer { public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { JLabel label = new JLabel(value.toString()) if( value instanceof MyNode ){ if( value.getUserObject().containsKey("icon") ){ label.setIcon(value.userObject.icon) } } return label } } sb = new SwingBuilder() sb.edt { frame( title: "tree with images example", show: true, resizable: true, size: [300, 200], defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE ){ scrollPane(){ tree(id:"tree", rootVisible: false, cellRenderer:new MyTreeRenderer()) tree.model.root.removeAllChildren() def node1 = new MyNode([name:"Node1", icon:imageIcon(file:"e:/icons/home.png")]) node1.add(new MyNode([name:"Leaf1", icon:imageIcon(file:"e:/icons/plane.png")])) node1.add(new MyNode([name:"Leaf2", icon:imageIcon(file:"e:/icons/mobile2.png")])) tree.model.root.add(node1) tree.model.root.add(new MyNode([name:"Node2", icon:imageIcon(file:"e:/icons/chat-.png")])) tree.model.reload(tree.model.root) tree.expandRow(0) } } }実行時画面
動作環境
groovy 2.2.2, JDK 1.7 update55
0 件のコメント:
コメントを投稿