Tree Popup problem

Thread Solved

Join Date: Jun 2009
Posts: 133
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Tree Popup problem

 
0
  #1
Jul 24th, 2009
I donno why I am not able to get the popup for this JTree. This tree is found almost in all tutorial ?

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.text.SimpleDateFormat;
  6.  
  7. import javax.swing.*;
  8. import javax.swing.tree.*;
  9. import javax.swing.event.*;
  10.  
  11. public class FileTree2
  12. extends JFrame
  13. {
  14. public static final ImageIcon ICON_COMPUTER =
  15. new ImageIcon("computer.gif");
  16. public static final ImageIcon ICON_DISK =
  17. new ImageIcon("disk.gif");
  18. public static final ImageIcon ICON_FOLDER =
  19. new ImageIcon("folder.gif");
  20. public static final ImageIcon ICON_EXPANDEDFOLDER =
  21. new ImageIcon("expandedfolder.gif");
  22.  
  23. protected JTree m_tree;
  24. protected DefaultTreeModel m_model;
  25. protected JTextField m_display;
  26.  
  27. // NEW
  28. protected JPopupMenu m_popup;
  29. protected Action m_action;
  30. protected TreePath m_clickedPath;
  31.  
  32. public FileTree2()
  33. {
  34. super("Directories Tree [Popup Menus]");
  35. setSize(400, 300);
  36.  
  37. DefaultMutableTreeNode top = new DefaultMutableTreeNode(
  38. new IconData(ICON_COMPUTER, null, "Computer"));
  39.  
  40. DefaultMutableTreeNode node;
  41. File[] roots = File.listRoots();
  42. for (int k=0; k<roots.length; k++)
  43. {
  44. node = new DefaultMutableTreeNode(new IconData(ICON_DISK,
  45. null, new FileNode(roots[k])));
  46. top.add(node);
  47. node.add(new DefaultMutableTreeNode( new Boolean(true) ));
  48. }
  49.  
  50. m_model = new DefaultTreeModel(top);
  51. m_tree = new JTree(m_model);
  52.  
  53. m_tree.putClientProperty("JTree.lineStyle", "Angled");
  54.  
  55. TreeCellRenderer renderer = new
  56. IconCellRenderer();
  57. m_tree.setCellRenderer(renderer);
  58.  
  59. m_tree.addTreeExpansionListener(new
  60. DirExpansionListener());
  61.  
  62. m_tree.addTreeSelectionListener(new
  63. DirSelectionListener());
  64.  
  65. m_tree.getSelectionModel().setSelectionMode(
  66. TreeSelectionModel.SINGLE_TREE_SELECTION);
  67. m_tree.setShowsRootHandles(true);
  68. m_tree.setEditable(false);
  69.  
  70. JScrollPane s = new JScrollPane();
  71. s.getViewport().add(m_tree);
  72. getContentPane().add(s, BorderLayout.CENTER);
  73.  
  74. m_display = new JTextField();
  75. m_display.setEditable(false);
  76. getContentPane().add(m_display, BorderLayout.NORTH);
  77.  
  78. // NEW
  79. m_popup = new JPopupMenu();
  80. m_action = new AbstractAction()
  81. {
  82. public void actionPerformed(ActionEvent e)
  83. {
  84. if (m_clickedPath==null)
  85. return;
  86. if (m_tree.isExpanded(m_clickedPath))
  87. m_tree.collapsePath(m_clickedPath);
  88. else
  89. m_tree.expandPath(m_clickedPath);
  90. }
  91. };
  92. m_popup.add(m_action);
  93. m_popup.addSeparator();
  94.  
  95. Action a1 = new AbstractAction("Delete")
  96. {
  97. public void actionPerformed(ActionEvent e)
  98. {
  99. m_tree.repaint();
  100. JOptionPane.showMessageDialog(FileTree2.this,
  101. "Delete option is not implemented",
  102. "Info", JOptionPane.INFORMATION_MESSAGE);
  103. }
  104. };
  105. m_popup.add(a1);
  106.  
  107. Action a2 = new AbstractAction("Rename")
  108. {
  109. public void actionPerformed(ActionEvent e)
  110. {
  111. m_tree.repaint();
  112. JOptionPane.showMessageDialog(FileTree2.this,
  113. "Rename option is not implemented",
  114. "Info", JOptionPane.INFORMATION_MESSAGE);
  115. }
  116. };
  117. m_popup.add(a2);
  118. m_tree.add(m_popup);
  119. m_tree.addMouseListener(new PopupTrigger());
  120.  
  121. WindowListener wndCloser = new WindowAdapter()
  122. {
  123. public void windowClosing(WindowEvent e)
  124. {
  125. System.exit(0);
  126. }
  127. };
  128. addWindowListener(wndCloser);
  129.  
  130. setVisible(true);
  131. }
  132.  
  133. DefaultMutableTreeNode getTreeNode(TreePath path)
  134. {
  135. return (DefaultMutableTreeNode)(path.getLastPathComponent());
  136. }
  137.  
  138. FileNode getFileNode(DefaultMutableTreeNode node)
  139. {
  140. if (node == null)
  141. return null;
  142. Object obj = node.getUserObject();
  143. if (obj instanceof IconData)
  144. obj = ((IconData)obj).getObject();
  145. if (obj instanceof FileNode)
  146. return (FileNode)obj;
  147. else
  148. return null;
  149. }
  150.  
  151. // NEW
  152. class PopupTrigger extends MouseAdapter
  153. {
  154. public void mouseReleased(MouseEvent e)
  155. {
  156. if (e.isPopupTrigger())
  157. {
  158. int x = e.getX();
  159. int y = e.getY();
  160. TreePath path = m_tree.getPathForLocation(x, y);
  161. if (path != null)
  162. {
  163. if (m_tree.isExpanded(path))
  164. m_action.putValue(Action.NAME, "Collapse");
  165. else
  166. m_action.putValue(Action.NAME, "Expand");
  167. m_popup.show(m_tree, x, y);
  168. m_clickedPath = path;
  169. }
  170. }
  171. }
  172. }
  173.  
  174. // Make sure expansion is threaded and updating the tree model
  175. // only occurs within the event dispatching thread.
  176. class DirExpansionListener implements TreeExpansionListener
  177. {
  178. public void treeExpanded(TreeExpansionEvent event)
  179. {
  180. final DefaultMutableTreeNode node = getTreeNode(
  181. event.getPath());
  182. final FileNode fnode = getFileNode(node);
  183.  
  184. Thread runner = new Thread()
  185. {
  186. public void run()
  187. {
  188. if (fnode != null && fnode.expand(node))
  189. {
  190. Runnable runnable = new Runnable()
  191. {
  192. public void run()
  193. {
  194. m_model.reload(node);
  195. }
  196. };
  197. SwingUtilities.invokeLater(runnable);
  198. }
  199. }
  200. };
  201. runner.start();
  202. }
  203.  
  204. public void treeCollapsed(TreeExpansionEvent event) {}
  205. }
  206.  
  207. class DirSelectionListener
  208. implements TreeSelectionListener
  209. {
  210. public void valueChanged(TreeSelectionEvent event)
  211. {
  212. DefaultMutableTreeNode node = getTreeNode(
  213. event.getPath());
  214. FileNode fnode = getFileNode(node);
  215. if (fnode != null)
  216. m_display.setText(fnode.getFile().
  217. getAbsolutePath());
  218. else
  219. m_display.setText("");
  220. }
  221. }
  222.  
  223. public static void main(String argv[])
  224. {
  225. new FileTree2();
  226. }
  227. }
  228.  
  229. class IconCellRenderer
  230. extends JLabel
  231. implements TreeCellRenderer
  232. {
  233. protected Color m_textSelectionColor;
  234. protected Color m_textNonSelectionColor;
  235. protected Color m_bkSelectionColor;
  236. protected Color m_bkNonSelectionColor;
  237. protected Color m_borderSelectionColor;
  238.  
  239. protected boolean m_selected;
  240.  
  241. public IconCellRenderer()
  242. {
  243. super();
  244. m_textSelectionColor = UIManager.getColor(
  245. "Tree.selectionForeground");
  246. m_textNonSelectionColor = UIManager.getColor(
  247. "Tree.textForeground");
  248. m_bkSelectionColor = UIManager.getColor(
  249. "Tree.selectionBackground");
  250. m_bkNonSelectionColor = UIManager.getColor(
  251. "Tree.textBackground");
  252. m_borderSelectionColor = UIManager.getColor(
  253. "Tree.selectionBorderColor");
  254. setOpaque(false);
  255. }
  256.  
  257. public Component getTreeCellRendererComponent(JTree tree,
  258. Object value, boolean sel, boolean expanded, boolean leaf,
  259. int row, boolean hasFocus)
  260.  
  261. {
  262. DefaultMutableTreeNode node =
  263. (DefaultMutableTreeNode)value;
  264. Object obj = node.getUserObject();
  265. setText(obj.toString());
  266.  
  267. if (obj instanceof Boolean)
  268. setText("Retrieving data...");
  269.  
  270. if (obj instanceof IconData)
  271. {
  272. IconData idata = (IconData)obj;
  273. if (expanded)
  274. setIcon(idata.getExpandedIcon());
  275. else
  276. setIcon(idata.getIcon());
  277. }
  278. else
  279. setIcon(null);
  280.  
  281. setFont(tree.getFont());
  282. setForeground(sel ? m_textSelectionColor :
  283. m_textNonSelectionColor);
  284. setBackground(sel ? m_bkSelectionColor :
  285. m_bkNonSelectionColor);
  286. m_selected = sel;
  287. return this;
  288. }
  289.  
  290. public void paintComponent(Graphics g)
  291. {
  292. Color bColor = getBackground();
  293. Icon icon = getIcon();
  294.  
  295. g.setColor(bColor);
  296. int offset = 0;
  297. if(icon != null && getText() != null)
  298. offset = (icon.getIconWidth() + getIconTextGap());
  299. g.fillRect(offset, 0, getWidth() - 1 - offset,
  300. getHeight() - 1);
  301.  
  302. if (m_selected)
  303. {
  304. g.setColor(m_borderSelectionColor);
  305. g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
  306. }
  307.  
  308. super.paintComponent(g);
  309. }
  310. }
  311.  
  312. class IconData
  313. {
  314. protected Icon m_icon;
  315. protected Icon m_expandedIcon;
  316. protected Object m_data;
  317.  
  318. public IconData(Icon icon, Object data)
  319. {
  320. m_icon = icon;
  321. m_expandedIcon = null;
  322. m_data = data;
  323. }
  324.  
  325. public IconData(Icon icon, Icon expandedIcon, Object data)
  326. {
  327. m_icon = icon;
  328. m_expandedIcon = expandedIcon;
  329. m_data = data;
  330. }
  331.  
  332. public Icon getIcon()
  333. {
  334. return m_icon;
  335. }
  336.  
  337. public Icon getExpandedIcon()
  338. {
  339. return m_expandedIcon!=null ? m_expandedIcon : m_icon;
  340. }
  341.  
  342. public Object getObject()
  343. {
  344. return m_data;
  345. }
  346.  
  347. public String toString()
  348. {
  349. return m_data.toString();
  350. }
  351. }
  352.  
  353. class FileNode
  354. {
  355. protected File m_file;
  356.  
  357. public FileNode(File file)
  358. {
  359. m_file = file;
  360. }
  361.  
  362. public File getFile()
  363. {
  364. return m_file;
  365. }
  366.  
  367. public String toString()
  368. {
  369. return m_file.getName().length() > 0 ? m_file.getName() :
  370. m_file.getPath();
  371. }
  372.  
  373. public boolean expand(DefaultMutableTreeNode parent)
  374. {
  375. DefaultMutableTreeNode flag =
  376. (DefaultMutableTreeNode)parent.getFirstChild();
  377. if (flag==null) // No flag
  378. return false;
  379. Object obj = flag.getUserObject();
  380. if (!(obj instanceof Boolean))
  381. return false; // Already expanded
  382.  
  383. parent.removeAllChildren(); // Remove Flag
  384.  
  385. File[] files = listFiles();
  386. if (files == null)
  387. return true;
  388.  
  389. Vector v = new Vector();
  390.  
  391. for (int k=0; k<files.length; k++)
  392. {
  393. File f = files[k];
  394. if (!(f.isDirectory()))
  395. continue;
  396.  
  397. FileNode newNode = new FileNode(f);
  398.  
  399. boolean isAdded = false;
  400. for (int i=0; i<v.size(); i++)
  401. {
  402. FileNode nd = (FileNode)v.elementAt(i);
  403. if (newNode.compareTo(nd) < 0)
  404. {
  405. v.insertElementAt(newNode, i);
  406. isAdded = true;
  407. break;
  408. }
  409. }
  410. if (!isAdded)
  411. v.addElement(newNode);
  412. }
  413.  
  414. for (int i=0; i<v.size(); i++)
  415. {
  416. FileNode nd = (FileNode)v.elementAt(i);
  417. IconData idata = new IconData(FileTree2.ICON_FOLDER,
  418. FileTree2.ICON_EXPANDEDFOLDER, nd);
  419. DefaultMutableTreeNode node = new
  420. DefaultMutableTreeNode(idata);
  421. parent.add(node);
  422.  
  423. if (nd.hasSubDirs())
  424. node.add(new DefaultMutableTreeNode(
  425. new Boolean(true) ));
  426. }
  427.  
  428. return true;
  429. }
  430.  
  431. public boolean hasSubDirs()
  432. {
  433. File[] files = listFiles();
  434. if (files == null)
  435. return false;
  436. for (int k=0; k<files.length; k++)
  437. {
  438. if (files[k].isDirectory())
  439. return true;
  440. }
  441. return false;
  442. }
  443.  
  444. public int compareTo(FileNode toCompare)
  445. {
  446. return m_file.getName().compareToIgnoreCase(
  447. toCompare.m_file.getName() );
  448. }
  449.  
  450. protected File[] listFiles()
  451. {
  452. if (!m_file.isDirectory())
  453. return null;
  454. try
  455. {
  456. return m_file.listFiles();
  457. }
  458. catch (Exception ex)
  459. {
  460. JOptionPane.showMessageDialog(null,
  461. "Error reading directory "+m_file.getAbsolutePath(),
  462. "Warning", JOptionPane.WARNING_MESSAGE);
  463. return null;
  464. }
  465. }
  466. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Tree Popup problem

 
1
  #2
Jul 24th, 2009
I working on Windows XP platform, and I am able to get the popup for this JTree.
From api doc java:
  1. public boolean isPopupTrigger()
  2. Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality.
Check Bruce Eckel example
http://www.java2s.com/Code/Java/Swin...swithSwing.htm
quuba
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 133
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Re: Tree Popup problem

 
0
  #3
Jul 25th, 2009
Great !! Ya it solved the problem.

Thanks a lot for your kind guidance.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC