java swing的樹操作(增刪改)
import java.awt.Dimension; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath;public class MyTree extends JFrame {
//增加帶滾動條容器 private JScrollPane jScrollPane1 = new JScrollPane(); private JTree tree; private JPopupMenu popMenu; public JScrollPane getjScrollPane1() { return jScrollPane1; } public void setjScrollPane1(JScrollPane jScrollPane1) { this.jScrollPane1 = jScrollPane1; } public JPopupMenu getPopMenu() { return popMenu; } public void setPopMenu(JPopupMenu popMenu) { this.popMenu = popMenu; } public MyTree() { try { init(); treeInit(); popMenuInit(); } catch (Exception exception) { exception.printStackTrace(); } this.setSize(800, 600); this.setResizable(true); this.setMinimumSize(new Dimension(800, 600)); this.setLocationRelativeTo(null); this.setVisible(true); //退出時需要終止當前jvm this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //設置當前窗口的信息 private void init() { getContentPane().setLayout(null); setTitle("樹操作"); } //初始化景點分類樹 public void treeInit() { if (jScrollPane1 != null) { this.remove(jScrollPane1); } jScrollPane1.setBounds(new Rectangle(0, 0, 800, 600)); jScrollPane1.setAutoscrolls(true); this.getContentPane().add(jScrollPane1); expandTree(); tree.addMouseListener(new TreePopMenuEvent(this)); this.repaint(); } //右鍵點擊分類導航樹的菜單 private void popMenuInit() { popMenu = new JPopupMenu(); JMenuItem addItem = new JMenuItem("添加"); addItem.addActionListener(new TreeAddViewMenuEvent(this)); JMenuItem delItem = new JMenuItem("刪除"); delItem.addActionListener(new TreeDeleteViewMenuEvent(this)); JMenuItem modifyItem = new JMenuItem("修改"); modifyItem.addActionListener(new TreeModifyViewMenuEvent(this)); popMenu.add(addItem); popMenu.add(delItem); popMenu.add(modifyItem); } /** * 完全展開一個JTree * * @param tree JTree */ public void expandTree() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("根節點"); tree = new JTree(root); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { //選中菜單節點的事件 DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); } }); tree.updateUI(); jScrollPane1.getViewport().add(tree); } /** * 獲取圖片文件內容 * * @param fileName * @return */ public Image getImage(String fileName) { FileInputStream fis = null; try { fis = new FileInputStream(fileName); BufferedInputStream bis = new BufferedInputStream(fis); ByteBuffer bb = ByteBuffer.allocate(1024 * 1024); byte[] buffer = new byte[1]; while (bis.read(buffer) > 0) { bb.put(buffer); } ImageIcon icon = new ImageIcon(bb.array()); return icon.getImage(); } catch (IOException ex) { Logger.getLogger(MyTree.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fis.close(); } catch (IOException ex) { Logger.getLogger(MyTree.class.getName()).log(Level.SEVERE, null, ex); } } return null; } public JTree getTree() { return tree; } /** * popmenu點擊右鍵的增加處理 */ class TreeAddViewMenuEvent implements ActionListener { private MyTree adaptee; public TreeAddViewMenuEvent(MyTree adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { String name = JOptionPane.showInputDialog("請輸入分類節點名稱:"); DefaultMutableTreeNode treenode = new DefaultMutableTreeNode(name); ((DefaultMutableTreeNode) this.adaptee.getTree().getLastSelectedPathComponent()).add(treenode); this.adaptee.getTree().expandPath(new TreePath(((DefaultMutableTreeNode) this.adaptee.getTree().getLastSelectedPathComponent()).getPath())); this.adaptee.getTree().updateUI(); } } /** * popmenu點擊右鍵的刪除處理 */ class TreeDeleteViewMenuEvent implements ActionListener { private MyTree adaptee; public TreeDeleteViewMenuEvent(MyTree adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { int conform = JOptionPane.showConfirmDialog(null, "是否確認刪除?", "刪除景點確認", JOptionPane.YES_NO_OPTION); if (conform == JOptionPane.YES_OPTION) { DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) (((DefaultMutableTreeNode) this.adaptee.getTree().getLastSelectedPathComponent()).getParent()); ((DefaultMutableTreeNode) this.adaptee.getTree().getLastSelectedPathComponent()).removeFromParent(); this.adaptee.getTree().updateUI(); } } }
}
/**
popmenu點擊右鍵的修改處理 */ class TreeModifyViewMenuEvent implements ActionListener {
private MyTree adaptee;
public TreeModifyViewMenuEvent(MyTree adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog("請輸入新分類節點名稱:"); DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.adaptee.getTree().getSelectionPath().getLastPathComponent(); //改名 node.setUserObject(name); //刷新 this.adaptee.getTree().updateUI();
} }
/**
菜單點擊右鍵的事件處理 */ class TreePopMenuEvent implements MouseListener {
private MyTree adaptee;
public TreePopMenuEvent(MyTree adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
TreePath path = adaptee.getTree().getPathForLocation(e.getX(), e.getY()); // 關鍵是這個方法的使用 if (path == null) { return; } adaptee.getTree().setSelectionPath(path); if (e.getButton() == 3) { adaptee.getPopMenu().show(adaptee.getTree(), e.getX(), e.getY()); }
}
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public static void main(String[] args) {
try { JFrame.setDefaultLookAndFeelDecorated(true); UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); MyTree userframe = new MyTree(); } catch (ClassNotFoundException ex) { Logger.getLogger(MyTree.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(MyTree.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(MyTree.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(MyTree.class.getName()).log(Level.SEVERE, null, ex); }
} }</pre>
轉自:http://blog.csdn.net/zhongweijian/article/details/7668909
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!