Представляю вам дерево стран на Java. Просто скопируйте нижний код и вставьте его в ваш IDE Eclipse или NetBeans.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; public class DerevoTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new DerevoFrameEdit(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class DerevoFrameEdit extends JFrame { public DerevoFrameEdit() { setTitle("Дерево"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); TreeNode root = makeSampleDerevo(); model = new DefaultTreeModel(root); tree = new JTree(model); tree.setEditable(true); JScrollPane scrollPane = new JScrollPane(tree); add(scrollPane, BorderLayout.CENTER); makeButtons(); } public TreeNode makeSampleDerevo() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("Мир"); DefaultMutableTreeNode country = new DefaultMutableTreeNode("Россия"); root.add(country); DefaultMutableTreeNode city = new DefaultMutableTreeNode("Москва"); country.add(city); country = new DefaultMutableTreeNode("Молдова"); root.add(country); city = new DefaultMutableTreeNode("Кишинев"); country.add(city); country = new DefaultMutableTreeNode("Англия"); root.add(country); city = new DefaultMutableTreeNode("Лондод"); country.add(city); return root; } public void makeButtons() { } { JPanel panel = new JPanel(); JButton addSiblingButton = new JButton("Добавить страну"); addSiblingButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(selectedNode == null) return; DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent(); if(parent == null) return; DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Новый"); int selectedIndex = parent.getIndex(selectedNode); model.insertNodeInto(newNode, parent, selectedIndex + 1); TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addSiblingButton); JButton addChildButton = new JButton("Добавить наследника"); addChildButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(selectedNode == null) return; DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Новый"); model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount()); TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addChildButton); JButton deleteButton = new JButton("Удалить"); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(selectedNode != null && selectedNode.getParent() != null) model.removeNodeFromParent(selectedNode); } }); panel.add(deleteButton); add(panel, BorderLayout.SOUTH); } private DefaultTreeModel model; private JTree tree; private static final int DEFAULT_WIDTH = 500; private static final int DEFAULT_HEIGHT = 500; } |