//************************************************************* // 木構造表示の動作確認 // 「TreeTest」 // //          作 成 者:ルート高菜    //          作成開始月:2007/8 //          最終更新月:2007/11 [TN115/J105] //************************************************************* import java.awt.Color; import java.awt.FileDialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultCellEditor; import javax.swing.ImageIcon; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeExpansionListener; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.event.TreeWillExpandListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.ExpandVetoException; import javax.swing.tree.TreePath; public class TreeTest extends JFrame implements TreeSelectionListener, TreeWillExpandListener, TreeExpansionListener, ActionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; public static void main(String[] args) { TreeTest frame=new TreeTest(); frame.setTitle("TreeTest"); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setSize(300,500); frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } // JTree tree; DefaultMutableTreeNode root; DefaultTreeCellRenderer renderer; // JMenuBar menu=new JMenuBar(); JMenu add=new JMenu("add"); JMenuItem addNonnameChild=new JMenuItem("non-named child"); JMenuItem addNameChild=new JMenuItem("named child..."); JMenuItem addNonnameUp=new JMenuItem("non-named upper"); JMenuItem addNameUp=new JMenuItem("named upper..."); JMenu remove=new JMenu("remove"); JMenuItem removeSelf=new JMenuItem("self"); JMenuItem removeChildren=new JMenuItem("all children"); JMenu move=new JMenu("move"); JMenuItem moveUp=new JMenuItem("up"); JMenuItem moveDown=new JMenuItem("down"); JMenu color=new JMenu("color"); JMenuItem colorSelectText=new JMenuItem("selected text..."); JMenuItem colorSelectBack=new JMenuItem("selected back..."); JMenuItem colorNonselectText=new JMenuItem("non-selected text..."); JMenuItem colorNonselectBack=new JMenuItem("non-selected back..."); JMenu icon=new JMenu("icon"); JMenuItem iconClosed=new JMenuItem("closed..."); JMenuItem iconOpen=new JMenuItem("open..."); JMenuItem iconLeaf=new JMenuItem("leaf..."); JMenu others=new JMenu("others"); JMenuItem format=new JMenuItem("foramt"); JMenuItem rename=new JMenuItem("rename..."); // public TreeTest(){ //******************************************** add.add(addNonnameChild); add.add(addNameChild); add.addSeparator(); add.add(addNonnameUp); add.add(addNameUp); remove.add(removeSelf); remove.add(removeChildren); move.add(moveUp); move.add(moveDown); color.add(colorSelectText); color.add(colorSelectBack); color.addSeparator(); color.add(colorNonselectText); color.add(colorNonselectBack); others.add(format); others.addSeparator(); others.add(rename); icon.add(iconClosed); icon.add(iconOpen); icon.addSeparator(); icon.add(iconLeaf); menu.add(add); menu.add(remove); menu.add(move); menu.add(color); menu.add(icon); menu.add(others); setJMenuBar(menu); // addNonnameChild.addActionListener(this); addNameChild.addActionListener(this); addNonnameUp.addActionListener(this); addNameUp.addActionListener(this); removeSelf.addActionListener(this); removeChildren.addActionListener(this); moveUp.addActionListener(this); moveDown.addActionListener(this); colorSelectText.addActionListener(this); colorSelectBack.addActionListener(this); colorNonselectText.addActionListener(this); colorNonselectBack.addActionListener(this); iconClosed.addActionListener(this); iconOpen.addActionListener(this); iconLeaf.addActionListener(this); format.addActionListener(this); rename.addActionListener(this); //******************************************** // formatTree(); // tree.addTreeSelectionListener(this); tree.addTreeWillExpandListener(this); tree.addTreeExpansionListener(this); } private void formatTree(){ DefaultMutableTreeNode kitamura=new DefaultMutableTreeNode("北村薫"); DefaultMutableTreeNode kitamura01=new DefaultMutableTreeNode("空飛ぶ馬"); DefaultMutableTreeNode kitamura02=new DefaultMutableTreeNode("スキップ"); DefaultMutableTreeNode kitamura03=new DefaultMutableTreeNode("覆面作家は二人いる"); kitamura.add(kitamura01); kitamura.add(kitamura02); kitamura.add(kitamura03); DefaultMutableTreeNode kanou=new DefaultMutableTreeNode("加納朋子"); DefaultMutableTreeNode kanou01=new DefaultMutableTreeNode("ななつのこ"); DefaultMutableTreeNode kanou02=new DefaultMutableTreeNode("いちばん初めにあった海"); kanou.add(kanou01); kanou.add(kanou02); DefaultMutableTreeNode wakatake=new DefaultMutableTreeNode("若竹七海"); DefaultMutableTreeNode sakaki=new DefaultMutableTreeNode("坂木司"); root=new DefaultMutableTreeNode("mystery"); root.add(kitamura); root.add(kanou); root.add(wakatake); root.add(sakaki); // tree=new JTree(root); //色やアイコンを変更可能に renderer=new DefaultTreeCellRenderer(); tree.setCellRenderer(renderer); //直接テキストフィールドで入力可能に tree.setEditable(true); DefaultCellEditor editor=new DefaultCellEditor(new JTextField(10)); tree.setCellEditor(editor); // JScrollPane scPanel=new JScrollPane(tree); getContentPane().add(scPanel); } //------------------------------------------------------------------- //ツリーイベント //------------------------------------------------------------------- //選択範囲が変更された public void valueChanged(TreeSelectionEvent e) { TreePath[] paths=tree.getSelectionPaths(); if(paths==null){ setTitle("TreeTest"); return; } //最後の選択から全てのパスを取得しタイトルに表示 Object[] nodes=paths[paths.length-1].getPath(); StringBuffer buf=new StringBuffer("TreeTest "); for(int n=0;n0){ //一番前でなければ1個前にずらす parent.insert(node,pos-1); //変更を通知 DefaultTreeModel model=(DefaultTreeModel)tree.getModel(); model.reload(parent); //移動したノードを選択 tree.addSelectionPath(new TreePath(node.getPath())); } }else{ JOptionPane.showMessageDialog(this,"ルートは移動できません","エラー",JOptionPane.ERROR_MESSAGE); } }else if(object==moveDown){ //***自分を後に移動 if(tree.getSelectionCount()!=1){ JOptionPane.showMessageDialog(this,"1個、選択してください","エラー",JOptionPane.ERROR_MESSAGE); return; } // DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if(!node.isRoot()){ DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent(); int pos=parent.getIndex(node); //現在の位置 if(pos!=-1){ if(pos