This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implement drag and drop into a ListView that has custom CellFactory

 
Stefan Sel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been banging my head trying to implement a drag n' drop feature into a ListView that has custom controls. I know how it's done when each cell has a default text label, but other than that I don't seem to get how it's done.

The main code:


The custom cell:
 
John Damien Smith
Ranch Hand
Posts: 298
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of drag and drop are you looking for? To drag items in and out of the list or to drag items to re-order the list.

Maybe take a look at the answer to this question:
http://stackoverflow.com/questions/20412445/how-to-create-a-reorder-able-tableview-in-javafx
Which is actually an implementation of images a re-orderable listView using drag and drop.
 
Stefan Sel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to drag items to re-order the list. I already saw that example, and what I don't get is how do you drag/drop the state of the ComboBox, CheckBox and RadioButton from one cell to another...
 
John Damien Smith
Ranch Hand
Posts: 298
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I don't get is how do you drag/drop the state of the ComboBox, CheckBox and RadioButton from one cell to another...

Instead of a List<String> you need a List<YourModelObject> where YourModelObject is some custom class you create.
When the user manipulates the controls in your list cell, update the state in YourModelObject.
When the list is reordered, the updateItem method on the list will fire and the list cells (which are the visual representation of the backing models in the list) will be updated to reflect the new order).
Get the ListView working with a backing model first before you try to get the drag and drop reordering working.
Also putting multiple items in a list cell like this is a little strange. I am sure there are valid use cases for it, but you might be better using a TableView rather than a ListView, so each cell is just modeling a single field of the backing model object rather than the entire backing model object.
 
Stefan Sel
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John, that worked perfectly well.

To be honest the only reason I've chosen to use a ListView instead of a TableView is because I got it easier to work with a ObservableList and because I only need one column with a bunch of controls.

Also here's a working example based on your solution in case somebody stumbles upon the same problem:

Main.java


ElementCell.java


ElementCellState.java
 
John Damien Smith
Ranch Hand
Posts: 298
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the follow-up Stefan, it is always nice to see somebody post follow up specific solutions that they have developed based just on general solution guidelines.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic