Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. public class NodeType {
  2.  
  3. private String nodeType ;
  4.  
  5. public NodeType (String nodeType ) {
  6. this.nodeType = nodeType ;
  7. }
  8.  
  9. public String getNodeType() {
  10. return this.nodeType ;
  11. }
  12. public void setNodeType (String nodeType) {
  13. this.nodeType = nodeType ;
  14. }
  15.  
  16. }
  17.  
  18. btnSave = new JButton("Save");
  19. btnSave.addActionListener(new ActionListener() {
  20. public void actionPerformed(ActionEvent e) {
  21. DefaultMutableTreeNode node = (DefaultMutableTreeNode)
  22. DynamicTree.tree.getLastSelectedPathComponent();
  23.  
  24. String txt = desc.getTxt() ;
  25. Object nodeInfo = node.getUserObject();
  26. if (node.isLeaf()) {
  27. ((NodeType) nodeInfo).setNodeType(txt );
  28.  
  29. JButton change = new JButton("Change");
  30. change.addActionListener(new ActionListener() {
  31. @Override
  32. public void actionPerformed(ActionEvent e) {
  33. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
  34.  
  35. if (node == null) {
  36. return;
  37. }
  38.  
  39. Object nodeInfo = node.getUserObject();
  40. if (node.isLeaf()) {
  41. BookInfo book = (BookInfo) nodeInfo;
  42. book.bookName = "New Name";
  43. ((DefaultTreeModel)tree.getModel()).nodeChanged(node);
  44. }
  45. if (DEBUG) {
  46. System.out.println(nodeInfo.toString());
  47. }
  48. }
  49. });
  50.  
  51. import java.awt.BorderLayout;
  52. import java.awt.event.ActionEvent;
  53. import java.awt.event.ActionListener;
  54. import javax.swing.JFrame;
  55. import javax.swing.JPanel;
  56. import javax.swing.JScrollPane;
  57. import javax.swing.UIManager;
  58.  
  59. import javax.swing.JTree;
  60. import javax.swing.tree.DefaultMutableTreeNode;
  61. import javax.swing.tree.TreeSelectionModel;
  62. import javax.swing.event.TreeSelectionEvent;
  63. import javax.swing.event.TreeSelectionListener;
  64.  
  65. import java.net.URL;
  66. import javax.swing.JButton;
  67. import javax.swing.tree.DefaultTreeModel;
  68.  
  69. public class TreeDemo extends JPanel {
  70.  
  71. private JTree tree;
  72. private static boolean DEBUG = false;
  73.  
  74. //Optionally play with line styles. Possible values are
  75. //"Angled" (the default), "Horizontal", and "None".
  76. private static boolean playWithLineStyle = false;
  77. private static String lineStyle = "Horizontal";
  78.  
  79. //Optionally set the look and feel.
  80. private static boolean useSystemLookAndFeel = false;
  81.  
  82. public TreeDemo() {
  83. super(new BorderLayout());
  84.  
  85. //Create the nodes.
  86. DefaultMutableTreeNode top
  87. = new DefaultMutableTreeNode("The Java Series");
  88. createNodes(top);
  89.  
  90. //Create a tree that allows one selection at a time.
  91. tree = new JTree();
  92. tree.setModel(new DefaultTreeModel(top));
  93. tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  94.  
  95. if (playWithLineStyle) {
  96. System.out.println("line style = " + lineStyle);
  97. tree.putClientProperty("JTree.lineStyle", lineStyle);
  98. }
  99.  
  100. //Create the scroll pane and add the tree to it.
  101. JScrollPane treeView = new JScrollPane(tree);
  102.  
  103. add(treeView);
  104.  
  105. JButton change = new JButton("Change");
  106. change.addActionListener(new ActionListener() {
  107. @Override
  108. public void actionPerformed(ActionEvent e) {
  109. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
  110.  
  111. if (node == null) {
  112. return;
  113. }
  114.  
  115. Object nodeInfo = node.getUserObject();
  116. if (node.isLeaf()) {
  117. BookInfo book = (BookInfo) nodeInfo;
  118. book.bookName = "New Name";
  119. ((DefaultTreeModel)tree.getModel()).nodeChanged(node);
  120. }
  121. if (DEBUG) {
  122. System.out.println(nodeInfo.toString());
  123. }
  124. }
  125. });
  126. add(change, BorderLayout.SOUTH);
  127. }
  128.  
  129. private class BookInfo {
  130.  
  131. public String bookName;
  132.  
  133. public BookInfo(String book) {
  134. bookName = book;
  135. }
  136.  
  137. public String toString() {
  138. return bookName;
  139. }
  140. }
  141.  
  142. private void createNodes(DefaultMutableTreeNode top) {
  143. DefaultMutableTreeNode category = null;
  144. DefaultMutableTreeNode book = null;
  145.  
  146. category = new DefaultMutableTreeNode("Books for Java Programmers");
  147. top.add(category);
  148.  
  149. //original Tutorial
  150. book = new DefaultMutableTreeNode(new BookInfo("The Java Tutorial: A Short Course on the Basics"));
  151. category.add(book);
  152.  
  153. //Tutorial Continued
  154. book = new DefaultMutableTreeNode(new BookInfo("The Java Tutorial Continued: The Rest of the JDK"));
  155. category.add(book);
  156.  
  157. //JFC Swing Tutorial
  158. book = new DefaultMutableTreeNode(new BookInfo("The JFC Swing Tutorial: A Guide to Constructing GUIs"));
  159. category.add(book);
  160.  
  161. //Bloch
  162. book = new DefaultMutableTreeNode(new BookInfo("Effective Java Programming Language Guide"));
  163. category.add(book);
  164.  
  165. //Arnold/Gosling
  166. book = new DefaultMutableTreeNode(new BookInfo("The Java Programming Language"));
  167. category.add(book);
  168.  
  169. //Chan
  170. book = new DefaultMutableTreeNode(new BookInfo("The Java Developers Almanac"));
  171. category.add(book);
  172.  
  173. category = new DefaultMutableTreeNode("Books for Java Implementers");
  174. top.add(category);
  175.  
  176. //VM
  177. book = new DefaultMutableTreeNode(new BookInfo("The Java Virtual Machine Specification"));
  178. category.add(book);
  179.  
  180. //Language Spec
  181. book = new DefaultMutableTreeNode(new BookInfo("The Java Language Specification"));
  182. category.add(book);
  183. }
  184.  
  185. /**
  186. * Create the GUI and show it. For thread safety, this method should be
  187. * invoked from the event dispatch thread.
  188. */
  189. private static void createAndShowGUI() {
  190. if (useSystemLookAndFeel) {
  191. try {
  192. UIManager.setLookAndFeel(
  193. UIManager.getSystemLookAndFeelClassName());
  194. } catch (Exception e) {
  195. System.err.println("Couldn't use system look and feel.");
  196. }
  197. }
  198.  
  199. //Create and set up the window.
  200. JFrame frame = new JFrame("TreeDemo");
  201. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  202.  
  203. //Add content to the window.
  204. frame.add(new TreeDemo());
  205.  
  206. //Display the window.
  207. frame.pack();
  208. frame.setLocationByPlatform(true);
  209. frame.setVisible(true);
  210. }
  211.  
  212. public static void main(String[] args) {
  213. //Schedule a job for the event dispatch thread:
  214. //creating and showing this application's GUI.
  215. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  216. public void run() {
  217. createAndShowGUI();
  218. }
  219. });
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement