Adding a Button node inside a ListView in Java FX 2 is pretty easy. Create a custom class that extends ListCell and integrate your object within it. See my sample class code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class ButtonListCell extends ListCell<MyObject> { @Override public void updateItem(MyObject obj, boolean empty) { super.updateItem(obj, empty); if (empty) { setText(null); setGraphic(null); } else { setText(obj.toString()); Button butt = new Button(); butt.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("clicked"); } }); setGraphic(butt); } } } |
Then use the class in the ListView object by doing it like this:
|
1 2 3 4 5 6 7 |
listview.setCellFactory(new Callback<ListView<MyObject>, ListCell>() { @Override public ListCell call(ListView<MyObject> param) { return new ButtonListCell(); } }); |
That’s it! Easy, right?