I am using a JTree and in this to raname a JTree node I am using right click(Rename through popup) or F2 key or double click. But problem is : when I rename a name and hit Enter key, node successfully renamed and when I rename a name and click anywhere on the frame(windows explorer style), node name does not rename.
Please give me a solution for this problem and Thanks in advance.
It's probably a little late, but the correct solution is to call JTree.setInvokesStopCellEditing(true). This will cause the tree to execute the rename when the user hits enter, or when the cell editor loses focus.
The ability to save the current edit by clicking elsewhere in the tree is not something that comes standard as part of the default JTree implementation. You're going to have to get into the details of how to use trees and do some special customization in order to achieve this functionality. This sounds like it would involve something with the focus of Swing components, i.e., when the tree gains focus, if the node is being edited, then save the node rename.
Related
I am making a program in Java and I have a SWT tree widget that the user is supposed to toy with via drag and drop.
Now the problem is that when the user drags and drops a branch, I want to change the branch item's parent to whichever treeItem it was dropped on, but there doesn't appear to be any way to do so. I could simply create a new treeItem, but there is no easy way to transfer the children, so I need to redefine all the children, and their grandchildren and so on recursively. It seems pretty klunky and inefficient to me that I need to remake the entire branch just to change the parent.
Is there any clean way to do this?
Well, the clean way is to separate view/widgets from model (as in MVC). In case of SWT, you should use TreeViewer and implement ITreeContentProvider. jFace will take care of creating tree nodes for you and you only need to update your model and refresh the viewer.
I need help on JavaHelp. The context-sensitive help offers, window-level, field-level (as in passing the component).
But i need to activate the JavaHelp when user press the HelpKey F1 or JavaHelp button from the tree node (the application built-in : Eclipse).
Since the tree node is not a component, so it cannot directly call the enableHelpKey or enableHelpOnButton. Furthermore, i need to open the selected JavaHelp content based on selected tree node (example there 3 node courseware : Science, Math, English)
Really appreciate for ideas and help on this.
Thanks.
The tree nodes are not JComponents but they are hosted in a JTree or other JComponent (JPanel). I would try to add a selection listener to the JTree and adjust the help topic for the JTree or JPanel.
I am new to eclipse RCP, well as a matter of fact to java also. Have a very basic question.
I have a JFace TreeViewer. I want to expand a particular node in that. The catch is I only have the name of the node. and no information apart from that.
I tried using treeItem, compared its string with that of the node name that I have, thus I got the node. I tried expanding it in the contentprovider of the tree. But i am not getting the desired output. When I check it in the log i get that it is expanded but it doesnt show in the viewer. I am performing this in display.asyncExec method in the contentprovider.
I hope the question is clear.
JFace viewers were created so that developers wouldn't have to mess around with SWT widgets and could use higher level API instead. When using JFace's viewer/content provider/label provider you should be in control of what nodes are in your tree (thus when you say you only know the name, I assume you are using SWT Tree directly). You can read about JFace viewers from Eclipse help.
To expand a tree node use expandToLevel(Object elementOrTreePath, int level) method of TreeViewer (the level is relative to the node that is expanded, not the root of the tree).
I want to create a JTree, and each node of the JTree has a name, and when you click on the node, a textarea will be expanded, I did some research online but didn't find anything useful. Could anyone tell me if this possible to achieve? if yes, could you please post a simple code sample?
You dont need JTextAreas, JTree has the ability built-in to make Nodes editable. Take a look at this example:
http://www.roseindia.net/java/example/java/swing/JTreeEditable.shtml
I just tried it out to make sure it works. Compile & Run. Click on one of the Nodes then press F2 to edit it. Please comment if you have questions or if this is not what you are looking for.
I have a JTree which displays a JPopupMenu when I right click nodes in the JTree. What is the best way to hide/show or enable/disable certain JMenuItems in my JPopupMenu based on the node selected in the JTree?
The first solution that came to mind was to add a TreeSelectionListener to the JTree for the 'value changed' event. In the event handling code I would use the TreeSelectionEvent's getNewLeadSelectionPath() method to get the path of the most recent selection, and use the resulting TreePath object's getLastPathComponent() to get the selected node. From here I would have a series of IF statements that access my JPopupMenu object and perform the modifications necessary to hide/show specific JMenuItems.
However, something feels off about this, and so I decided I would ask SO if there was a better approach.
The way that I chose to tackle this within my own app was to use the "userObject" property of the DefaultMutableTreeNode class which allows you to just store any data you want along with your node. I have a variety of types of things that extend from an abstract base class which defines a "createPopupMenu()" method. Then, in the selection listener (just as you described in your question) I get the user object and ask it to create a popup menu appropriate for the selected object and display that.
Getting the selected tree node is straight forward and should work as you described it. To modify the popup menu I would recommend using Actions. This way you wouldn’t have to modify your live menu and could also add e.g. a JToolBar that contains the same actions that react the same way the items in your menu do.