Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was testing this recently. My solution:</p> <pre><code>import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.Priority; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.util.Callback; public class SO extends Application { static class XCell extends ListCell&lt;String&gt; { HBox hbox = new HBox(); Label label = new Label("(empty)"); Pane pane = new Pane(); Button button = new Button("(&gt;)"); String lastItem; public XCell() { super(); hbox.getChildren().addAll(label, pane, button); HBox.setHgrow(pane, Priority.ALWAYS); button.setOnAction(new EventHandler&lt;ActionEvent&gt;() { @Override public void handle(ActionEvent event) { System.out.println(lastItem + " : " + event); } }); } @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); setText(null); // No text in label of super class if (empty) { lastItem = null; setGraphic(null); } else { lastItem = item; label.setText(item!=null ? item : "&lt;null&gt;"); setGraphic(hbox); } } } @Override public void start(Stage primaryStage) throws Exception { StackPane pane = new StackPane(); Scene scene = new Scene(pane, 300, 150); primaryStage.setScene(scene); ObservableList&lt;String&gt; list = FXCollections.observableArrayList( "Item 1", "Item 2", "Item 3", "Item 4"); ListView&lt;String&gt; lv = new ListView&lt;&gt;(list); lv.setCellFactory(new Callback&lt;ListView&lt;String&gt;, ListCell&lt;String&gt;&gt;() { @Override public ListCell&lt;String&gt; call(ListView&lt;String&gt; param) { return new XCell(); } }); pane.getChildren().add(lv); primaryStage.show(); } public static void main(String[] args) { launch(args); } } </code></pre> <p>The cells will look like this:</p> <p><img src="https://i.stack.imgur.com/QIT9V.png" alt="Custom List Cell with Button"></p> <p>The relevant part is the <code>XCell.updateItem</code> method and the <code>setGraphic</code> call. With <code>setGraphic()</code> usually an icon shall be set for a label, but it works equally well to set a complex Node - in this case the HBox with label and button.</p> <p>You need to make sure, that the Button's event handler refers to the correct item in the list. In the first link below, it is mentioned, that the currently selected item might be sufficient for now. So fetch the currently selected index in the list when handling the button's event.</p> <p>You might like to look at these:</p> <ul> <li><a href="http://fxexperience.com/2012/05/listview-custom-cell-factories-and-context-menus/" rel="noreferrer">http://fxexperience.com/2012/05/listview-custom-cell-factories-and-context-menus/</a></li> <li><a href="http://docs.oracle.com/javafx/2/ui_controls/list-view.htm" rel="noreferrer">http://docs.oracle.com/javafx/2/ui_controls/list-view.htm</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload