Node position in TreeView












0















How can I get the position of a node from a TreeView with onclick event in JavaFX?



JavaFX scene



Let's say when I click on the node (dolor sit amet,) selected with the black circle how can I get the position?



I have tried this with an event when a node is expanded:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
TreeItem item = (TreeItem) event.getSource();
//here I want to know item position
}
});









share|improve this question























  • there is no item position, there's a cell position that has the item - please read a basic tutorial to learn how to implement and configure custom TreeCells

    – kleopatra
    Nov 27 '18 at 10:51











  • What do you want to do with the position(x,y) ? be specific.

    – Calips
    Nov 27 '18 at 12:17













  • @Calips I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05











  • @kleopatra Can you give me some suggestion about that. To let you know I have done some of my CustomCell that extend TreeCell using the HBox widget, not so much success to achieve something out of the TreeItems so a Vertical Line that goes on top of them which should not be connected with TreeItems but only with the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05
















0















How can I get the position of a node from a TreeView with onclick event in JavaFX?



JavaFX scene



Let's say when I click on the node (dolor sit amet,) selected with the black circle how can I get the position?



I have tried this with an event when a node is expanded:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
TreeItem item = (TreeItem) event.getSource();
//here I want to know item position
}
});









share|improve this question























  • there is no item position, there's a cell position that has the item - please read a basic tutorial to learn how to implement and configure custom TreeCells

    – kleopatra
    Nov 27 '18 at 10:51











  • What do you want to do with the position(x,y) ? be specific.

    – Calips
    Nov 27 '18 at 12:17













  • @Calips I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05











  • @kleopatra Can you give me some suggestion about that. To let you know I have done some of my CustomCell that extend TreeCell using the HBox widget, not so much success to achieve something out of the TreeItems so a Vertical Line that goes on top of them which should not be connected with TreeItems but only with the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05














0












0








0








How can I get the position of a node from a TreeView with onclick event in JavaFX?



JavaFX scene



Let's say when I click on the node (dolor sit amet,) selected with the black circle how can I get the position?



I have tried this with an event when a node is expanded:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
TreeItem item = (TreeItem) event.getSource();
//here I want to know item position
}
});









share|improve this question














How can I get the position of a node from a TreeView with onclick event in JavaFX?



JavaFX scene



Let's say when I click on the node (dolor sit amet,) selected with the black circle how can I get the position?



I have tried this with an event when a node is expanded:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
TreeItem item = (TreeItem) event.getSource();
//here I want to know item position
}
});






java javafx






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 10:19









ASTeamASTeam

178211




178211













  • there is no item position, there's a cell position that has the item - please read a basic tutorial to learn how to implement and configure custom TreeCells

    – kleopatra
    Nov 27 '18 at 10:51











  • What do you want to do with the position(x,y) ? be specific.

    – Calips
    Nov 27 '18 at 12:17













  • @Calips I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05











  • @kleopatra Can you give me some suggestion about that. To let you know I have done some of my CustomCell that extend TreeCell using the HBox widget, not so much success to achieve something out of the TreeItems so a Vertical Line that goes on top of them which should not be connected with TreeItems but only with the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05



















  • there is no item position, there's a cell position that has the item - please read a basic tutorial to learn how to implement and configure custom TreeCells

    – kleopatra
    Nov 27 '18 at 10:51











  • What do you want to do with the position(x,y) ? be specific.

    – Calips
    Nov 27 '18 at 12:17













  • @Calips I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05











  • @kleopatra Can you give me some suggestion about that. To let you know I have done some of my CustomCell that extend TreeCell using the HBox widget, not so much success to achieve something out of the TreeItems so a Vertical Line that goes on top of them which should not be connected with TreeItems but only with the TreeView.

    – ASTeam
    Nov 27 '18 at 20:05

















there is no item position, there's a cell position that has the item - please read a basic tutorial to learn how to implement and configure custom TreeCells

– kleopatra
Nov 27 '18 at 10:51





there is no item position, there's a cell position that has the item - please read a basic tutorial to learn how to implement and configure custom TreeCells

– kleopatra
Nov 27 '18 at 10:51













What do you want to do with the position(x,y) ? be specific.

– Calips
Nov 27 '18 at 12:17







What do you want to do with the position(x,y) ? be specific.

– Calips
Nov 27 '18 at 12:17















@Calips I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.

– ASTeam
Nov 27 '18 at 20:05





@Calips I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.

– ASTeam
Nov 27 '18 at 20:05













@kleopatra Can you give me some suggestion about that. To let you know I have done some of my CustomCell that extend TreeCell using the HBox widget, not so much success to achieve something out of the TreeItems so a Vertical Line that goes on top of them which should not be connected with TreeItems but only with the TreeView.

– ASTeam
Nov 27 '18 at 20:05





@kleopatra Can you give me some suggestion about that. To let you know I have done some of my CustomCell that extend TreeCell using the HBox widget, not so much success to achieve something out of the TreeItems so a Vertical Line that goes on top of them which should not be connected with TreeItems but only with the TreeView.

– ASTeam
Nov 27 '18 at 20:05












1 Answer
1






active

oldest

votes


















1














The Node you're talking about is known as the disclosureNode and is accessible via the TreeCell.




The disclosure node is commonly seen represented as a triangle that rotates on screen to indicate whether or not the TreeItem that it is placed beside is expanded or collapsed.




You can get the position of this Node with properties such as Node.boundsInLocal or Node.boundsInParent. There are also methods in Node to help convert between coordinate spaces (e.g. localToScene, sceneToLocal, etc...).





Also, the following code is suspect:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
}
});


You shouldn't need to check the name of the EventType. Instead, register the EventHandler for the EventType you want to listen for—in this case, TreeItem.branchExpandedEvent(). For example, if your TreeItems have values of type String:



// "root" is a TreeItem<String>
// "event" is a TreeItem.TreeModificationEvent<String>
root.addEventHandler(TreeItem.<String>branchExpandedEvent(), event -> {
// do something...
});


If you must check the EventType inside the EventHandler then test that the EventTypes are equal, not their names; it's less error-prone.



root.addEventHandler(Event.ANY, event -> {
if (event.getEventType().equals(TreeItem.branchExpandedEvent()) {
// do something...
}
});





I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.




I haven't looked into it, but this sounds like it should be implemented by the TreeCell. The TreeItem shouldn't know much, if anything, about the view. In other words, handling this inside an event handler added to the root TreeItem seems like the wrong place. Note, you can get the expanded status of a TreeItem from within a TreeCell as it has access to its TreeItem.






share|improve this answer


























  • I have problems accessing the TreeCell from the TreeItem. In the event, I can get easily the TreeItem TreeItem item = (TreeItem) event.getSource();. How to access the TreeCell in order to access the disclosureNode and to use the properties of it.

    – ASTeam
    Nov 28 '18 at 11:43











  • You likely can't from the TreeItem. You can use your own TreeCell implementation by setting the TreeView.cellFactory property. You may need to subclass TreeCellSkin as well.

    – Slaw
    Nov 28 '18 at 21:52













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497433%2fnode-position-in-treeview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The Node you're talking about is known as the disclosureNode and is accessible via the TreeCell.




The disclosure node is commonly seen represented as a triangle that rotates on screen to indicate whether or not the TreeItem that it is placed beside is expanded or collapsed.




You can get the position of this Node with properties such as Node.boundsInLocal or Node.boundsInParent. There are also methods in Node to help convert between coordinate spaces (e.g. localToScene, sceneToLocal, etc...).





Also, the following code is suspect:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
}
});


You shouldn't need to check the name of the EventType. Instead, register the EventHandler for the EventType you want to listen for—in this case, TreeItem.branchExpandedEvent(). For example, if your TreeItems have values of type String:



// "root" is a TreeItem<String>
// "event" is a TreeItem.TreeModificationEvent<String>
root.addEventHandler(TreeItem.<String>branchExpandedEvent(), event -> {
// do something...
});


If you must check the EventType inside the EventHandler then test that the EventTypes are equal, not their names; it's less error-prone.



root.addEventHandler(Event.ANY, event -> {
if (event.getEventType().equals(TreeItem.branchExpandedEvent()) {
// do something...
}
});





I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.




I haven't looked into it, but this sounds like it should be implemented by the TreeCell. The TreeItem shouldn't know much, if anything, about the view. In other words, handling this inside an event handler added to the root TreeItem seems like the wrong place. Note, you can get the expanded status of a TreeItem from within a TreeCell as it has access to its TreeItem.






share|improve this answer


























  • I have problems accessing the TreeCell from the TreeItem. In the event, I can get easily the TreeItem TreeItem item = (TreeItem) event.getSource();. How to access the TreeCell in order to access the disclosureNode and to use the properties of it.

    – ASTeam
    Nov 28 '18 at 11:43











  • You likely can't from the TreeItem. You can use your own TreeCell implementation by setting the TreeView.cellFactory property. You may need to subclass TreeCellSkin as well.

    – Slaw
    Nov 28 '18 at 21:52


















1














The Node you're talking about is known as the disclosureNode and is accessible via the TreeCell.




The disclosure node is commonly seen represented as a triangle that rotates on screen to indicate whether or not the TreeItem that it is placed beside is expanded or collapsed.




You can get the position of this Node with properties such as Node.boundsInLocal or Node.boundsInParent. There are also methods in Node to help convert between coordinate spaces (e.g. localToScene, sceneToLocal, etc...).





Also, the following code is suspect:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
}
});


You shouldn't need to check the name of the EventType. Instead, register the EventHandler for the EventType you want to listen for—in this case, TreeItem.branchExpandedEvent(). For example, if your TreeItems have values of type String:



// "root" is a TreeItem<String>
// "event" is a TreeItem.TreeModificationEvent<String>
root.addEventHandler(TreeItem.<String>branchExpandedEvent(), event -> {
// do something...
});


If you must check the EventType inside the EventHandler then test that the EventTypes are equal, not their names; it's less error-prone.



root.addEventHandler(Event.ANY, event -> {
if (event.getEventType().equals(TreeItem.branchExpandedEvent()) {
// do something...
}
});





I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.




I haven't looked into it, but this sounds like it should be implemented by the TreeCell. The TreeItem shouldn't know much, if anything, about the view. In other words, handling this inside an event handler added to the root TreeItem seems like the wrong place. Note, you can get the expanded status of a TreeItem from within a TreeCell as it has access to its TreeItem.






share|improve this answer


























  • I have problems accessing the TreeCell from the TreeItem. In the event, I can get easily the TreeItem TreeItem item = (TreeItem) event.getSource();. How to access the TreeCell in order to access the disclosureNode and to use the properties of it.

    – ASTeam
    Nov 28 '18 at 11:43











  • You likely can't from the TreeItem. You can use your own TreeCell implementation by setting the TreeView.cellFactory property. You may need to subclass TreeCellSkin as well.

    – Slaw
    Nov 28 '18 at 21:52
















1












1








1







The Node you're talking about is known as the disclosureNode and is accessible via the TreeCell.




The disclosure node is commonly seen represented as a triangle that rotates on screen to indicate whether or not the TreeItem that it is placed beside is expanded or collapsed.




You can get the position of this Node with properties such as Node.boundsInLocal or Node.boundsInParent. There are also methods in Node to help convert between coordinate spaces (e.g. localToScene, sceneToLocal, etc...).





Also, the following code is suspect:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
}
});


You shouldn't need to check the name of the EventType. Instead, register the EventHandler for the EventType you want to listen for—in this case, TreeItem.branchExpandedEvent(). For example, if your TreeItems have values of type String:



// "root" is a TreeItem<String>
// "event" is a TreeItem.TreeModificationEvent<String>
root.addEventHandler(TreeItem.<String>branchExpandedEvent(), event -> {
// do something...
});


If you must check the EventType inside the EventHandler then test that the EventTypes are equal, not their names; it's less error-prone.



root.addEventHandler(Event.ANY, event -> {
if (event.getEventType().equals(TreeItem.branchExpandedEvent()) {
// do something...
}
});





I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.




I haven't looked into it, but this sounds like it should be implemented by the TreeCell. The TreeItem shouldn't know much, if anything, about the view. In other words, handling this inside an event handler added to the root TreeItem seems like the wrong place. Note, you can get the expanded status of a TreeItem from within a TreeCell as it has access to its TreeItem.






share|improve this answer















The Node you're talking about is known as the disclosureNode and is accessible via the TreeCell.




The disclosure node is commonly seen represented as a triangle that rotates on screen to indicate whether or not the TreeItem that it is placed beside is expanded or collapsed.




You can get the position of this Node with properties such as Node.boundsInLocal or Node.boundsInParent. There are also methods in Node to help convert between coordinate spaces (e.g. localToScene, sceneToLocal, etc...).





Also, the following code is suspect:



root.addEventHandler(EventType.ROOT, (Event event) -> {
if (event.getEventType().getName().equals("BranchExpandedEvent")) {
}
});


You shouldn't need to check the name of the EventType. Instead, register the EventHandler for the EventType you want to listen for—in this case, TreeItem.branchExpandedEvent(). For example, if your TreeItems have values of type String:



// "root" is a TreeItem<String>
// "event" is a TreeItem.TreeModificationEvent<String>
root.addEventHandler(TreeItem.<String>branchExpandedEvent(), event -> {
// do something...
});


If you must check the EventType inside the EventHandler then test that the EventTypes are equal, not their names; it's less error-prone.



root.addEventHandler(Event.ANY, event -> {
if (event.getEventType().equals(TreeItem.branchExpandedEvent()) {
// do something...
}
});





I want to have this position and to add some other Nodes (Vertical line in my case to connect the treeitems at the same level) in the TreeView.




I haven't looked into it, but this sounds like it should be implemented by the TreeCell. The TreeItem shouldn't know much, if anything, about the view. In other words, handling this inside an event handler added to the root TreeItem seems like the wrong place. Note, you can get the expanded status of a TreeItem from within a TreeCell as it has access to its TreeItem.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 0:39

























answered Nov 28 '18 at 0:16









SlawSlaw

8,75631234




8,75631234













  • I have problems accessing the TreeCell from the TreeItem. In the event, I can get easily the TreeItem TreeItem item = (TreeItem) event.getSource();. How to access the TreeCell in order to access the disclosureNode and to use the properties of it.

    – ASTeam
    Nov 28 '18 at 11:43











  • You likely can't from the TreeItem. You can use your own TreeCell implementation by setting the TreeView.cellFactory property. You may need to subclass TreeCellSkin as well.

    – Slaw
    Nov 28 '18 at 21:52





















  • I have problems accessing the TreeCell from the TreeItem. In the event, I can get easily the TreeItem TreeItem item = (TreeItem) event.getSource();. How to access the TreeCell in order to access the disclosureNode and to use the properties of it.

    – ASTeam
    Nov 28 '18 at 11:43











  • You likely can't from the TreeItem. You can use your own TreeCell implementation by setting the TreeView.cellFactory property. You may need to subclass TreeCellSkin as well.

    – Slaw
    Nov 28 '18 at 21:52



















I have problems accessing the TreeCell from the TreeItem. In the event, I can get easily the TreeItem TreeItem item = (TreeItem) event.getSource();. How to access the TreeCell in order to access the disclosureNode and to use the properties of it.

– ASTeam
Nov 28 '18 at 11:43





I have problems accessing the TreeCell from the TreeItem. In the event, I can get easily the TreeItem TreeItem item = (TreeItem) event.getSource();. How to access the TreeCell in order to access the disclosureNode and to use the properties of it.

– ASTeam
Nov 28 '18 at 11:43













You likely can't from the TreeItem. You can use your own TreeCell implementation by setting the TreeView.cellFactory property. You may need to subclass TreeCellSkin as well.

– Slaw
Nov 28 '18 at 21:52







You likely can't from the TreeItem. You can use your own TreeCell implementation by setting the TreeView.cellFactory property. You may need to subclass TreeCellSkin as well.

– Slaw
Nov 28 '18 at 21:52






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497433%2fnode-position-in-treeview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)