This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

ContextMenu - get selected Item from Treeview

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have a treeview which holds some elements. At the moment, those are just strings. I attached a ContextMenu to that Treeview. The ContextMenu has some items like delete, refresh, etc. which should interact on the object in the treeview. My problem is that when the menu appears, i do not know how i can get the selected element(s). And i believe, my design is broken/procedural.

There is my first question: what is best practice to have a treeview (or anything like that) and elements in that list. Guess i should add the complete object to the treeview, register a special renderer for that treeview which holds a reference to the selected elements. When the user rightclicks on one of the elements and selects the delete menuitem, i can get the selected element(s) from the treeview-renderer, which inform the underlying classes that these element(s) should be removed. Is this correct?

I am new to javafx, if someone has any advice, i really appreciate that, thanks!

ps.: there is a tool called jdownloader, which has pretty exactly what i need from the design of the table/tree. Maybe someone can tell me what would be the "best" way to get something like that: https://browse.startpage.com/do/show_picture.pl?l=deutsch&rais=1&oiu=https%3A%2F%2Fi.imgur.com%2F7d26f0u.png&sp=07c549cf7c3207fe4387d521caea089f&t=default but with context menu's when someone right-clicks on one of the listed elements.
 
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really know what you meen by contextmenu, but let me ask, how do you build your gui? Do you create your components in the controller or do you use a fxml?
Anyway the easiest way would be to asign an id. your contextmeny would call a method an that would use the component by it's id...
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way the image you posted uses a border- and a tabpane with a tableview in the center, not a treeview
 
Sheriff
Posts: 6106
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:I don't really know what you meen by contextmenu...


https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ContextMenu.html

It's the menu that pops up when you right-click the mouse on some Node.
 
Knute Snortum
Sheriff
Posts: 6106
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding of Contextmenu is that you make it respond to onMouseClick events, then test for MouseButton.SECONDARY.  This would be the right-click for right-handed people.  So I would think each Node would need to have its own onMouseClick event set.  I don't think you can just check the onMouseClick for a container and then figure out the Node from there.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rereading your question I ask myself is all you want to do remove elements on right click? Why would you then need a contextmenu? A contectmenu is ment to provide you with severak options, in your case that would be a D tour
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment it is 1 menuitem (delete), but when it is finished there will be 5-8 i guess. More or less.
I use scenebuilder to create a .fxml file and then i use the IDs in my code.
Daniel, please re-read my question again. "The ContextMenu has some items like delete, refresh, etc."
Are you absolutely sure that the picture uses a tableview and NOT a treeview? How do you know that? Because it absolutely looks like some kind of tree (treetableview, treeview, ...) and that is what i need - because i have a package which has some files, and each of those files should be able to display a progressbar, a cancel button etc.

Thanks Knute, i think so too. So i have to create for each item in the tree a new ContextMenu which is triggered. This contextmenu knows which element got clicked and then i can execute the given action on that element. Right?

 
Bartender
Posts: 20915
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:My understanding of Contextmenu is that you make it respond to onMouseClick events, then test for MouseButton.SECONDARY.  This would be the right-click for right-handed people.  So I would think each Node would need to have its own onMouseClick event set.  I don't think you can just check the onMouseClick for a container and then figure out the Node from there.



That would be low-level operation. Dealing with the mouse buttons, displaying the context menu, etc. should have already been taken care of by the framework, so all the application programmer needs to deal with are refinements.

I'm not really familiar with JavaFX, but the docs seem to indicate that you implement the ContextMenu at the Tree control level, not per-node. Generally, in fact, the same menu applies more or less to all nodes (or all nodes of a type) for a tree, so that would reduce complexity a lot. I expect that you could inject logic that would customize the details prior to rendering the menu, the same way you can with dialogs, but that's mainly because other, similar frameworks often work that way.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all nodes are suposed to have the same options you don't need to make a contextmenu for every node, you can just show an hide it each time.
the picture you have posted a link to is a tableview inside a tabpane inside a borderpane in a anchorpane. I'm sure of it.
Just look how the fxml is structured. On the top you have a menu and a toolbar, in the center you have the tabpane with a tableview, on the right the viewmenu, that you could do in several different way's (so I don't really know what he used), I would mostlikely use a vbox with a couple of nested hboxes on the bottom your overview, on the botom of you anchorpane you have the rest
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I didn't boughter styling it, but it should illustrate my point
 
Knute Snortum
Sheriff
Posts: 6106
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I think I know how to do this now.  The thing to consider is whether you want to select by row or cell.  If you're doing a delete, is that a delete row?  That's easy.  If not, you can set the selection model to "cell", but it's trickier determining which cell in the context menu.  Here's what I have so far:
 
Tim Holloway
Bartender
Posts: 20915
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That still smells too much of brute force for me.

I'm expecting something more along these lines:


And


The idea being that there are abstractions rather than low-level operations.

And that because GUI events may be fired from many sources (top menu, toolbar, context menu, and so forth) that there should be a single Action event that can be attached to each of the GUI control elements.
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Daniel
The important part is the content, not the stuff around it. Thats the big point. And i still believe, when i take a look at that jdownloader picture, this is some kind of a tree. There is a hidden root node, followed by some child nodes where each of the child nodes has some leafs.

@Knute
I delete a complete row

@Tim
Thanks, this looks very promising to me!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!