Java: How to get the index of file in JTree - java

I have a JTree and I have it displaying everything I need, but i want to add what ever file i choose from the JTree to a JTextArea. I have this been done using a JList which all i do is get the selected index of the file in the JList and then append that to the JTextArea... But looking through the JTree there is no method to get the index of the chosen like in the JList, I have searched online and have had no luck with finding a solution!
The only way I can think of doing this a having a few methods to search through the JTree and at each level return where index of the file is, but that is alot of work so is there any better way to do this? Really what I am asking is what would be the best way to do this?

If you are only allowing single selection, you can use:
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
To get selected node. You could create a Map where the key is going to be the DefaultMutableTreeNode, and the associated value could be the String of the file you want to create.

Related

Select the TreeNode before (java swing, tree)

I am looking for a way to select the TreeNode before the actually selected node (if no Node is selected, the first Node AFTER the root should be selected) on Button click. I am basically having a Hashtable ht and a JTree filled with nodes (all Nodes allowing Children).
Once i click a node, i want to look up in the Hashtable for that Object and the system should write something like "You selected the 'Object.name'". Then the user should be able to click a JButton and select the node before the clicked node. Of course there should also be this line with an updated 'Object.name'. Maybe there is even a way to update the GUI of the tree, so that the other TreeNode is shown as selected.
I have been trying around for quite a while with the treePath-Methods of the selectionModel and the TreeNodes (e.g. saving them in Lists and trying to select the Element before), but nothing seems to work. I am also not finding anything useful on Google or StackOverflow. I was thinking that it might work somehow with the getPreviousSibling-Method from the DefaultMutableTreeNode?
Last thing I tried:
DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode();
treeNode = (DefaultMutableTreeNode) getLastSelectedPathComponent();
setSelectionPath(new TreePath(((DefaultTreeModel) treeModel).getPathToRoot(treeNode.getPreviousSibling())));
Unfortunately I keep getting a NullPointerException:
in line 2 of this snippet (although I selected another TreeNode before, wich has a sibling before).
Can anyone please tell me what I did wrong or tell me what other way might work? :D If you need more information please have no regard in asking me.

How to: JTable with collapsible JTree in first column and data in the other columns?

I am somewhat lost at this point:
I try to create a JTable that is able to hold some kind of a Tree (JTree?) datastructure in the first column followed by data based on the tree Node in the other columns as illustrated in the following image:
That image shows the structure of my JTree. The red lines indicate the table structur that i would need to realize. Each Node in the Tree should be treatet like a single row in that table, etc. I hope it is clear what i try to achieve.
I tried an aproach like it was discussed here: JTable as a JTree Node and here Put JTable in the JTree but that doesn't fit my needs. I also read about the JXTreeTable from the SwingX project but it seems to be very hard to find any usefull examples. Also many links to solutions like this seem to lead to (no longer existing) sun websites and/or beeing redirected to the oracle site...
It would be awesome if someone could lead me into the right direction and/or provide some exmaple code on how to do this (right).
Any help would be appreciated ;).
Create your own TreeTable component like in the following link :
http://www.java2s.com/Code/Java/Swing-Components/JTreeTablecomponent.htm

Jtree navigator

I would like to know how can I implement a navigator tree for my application in swing. I got a long sequence of panel, and I would like to have on left side of the screen a kind of "navigator". It seems easy as concept, but I really do not know where to start.
To get you a picture is something like this:
I would like to realize the "red zone" :)
Actually I'm able to construct the tree iterating through the arraylist provided by the controller, in this way:
Iterator<SectionPanel> sectionIterator = sectionPanelList.iterator();
while (sectionIterator.hasNext()) {
root.add(new DefaultMutableTreeNode(sectionIterator.next()));
}
The question is: if I want to remove a particular object, how can I find it within the tree? Moreover, how can I connect an action with the relative JPanel ? I mean "when I click on Panelx, I would like the scroll to move and let me see that particular panel ".
Thanks for your attention.
Simone
for showing one single JPanel, you can
use JTree and to returns String value from TreeSelectionListener
put JPanels to the CardLayout
selection acme from JTree returns String value as paramanter for CardLayout#show(Container parent, String name)
for multiply the logics could be the same, but there will be x_times number of combinations

JTextarea as nodes of JTree

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.

JTree node Rename

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.

Categories

Resources