Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about creating a row factory which exposes an observable list of the indexes of table rows which are to be highlighted? That way you can simply update the list with the indexes you need to highlight: for example by calling the getSelectedIndices() on the selection model and passing it to the list's setAll(...) method.</p> <p>This could look something like:</p> <pre><code>import java.util.Collections; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; import javafx.scene.control.TableRow; import javafx.scene.control.TableView; import javafx.util.Callback; public class StyleChangingRowFactory&lt;T&gt; implements Callback&lt;TableView&lt;T&gt;, TableRow&lt;T&gt;&gt; { private final String styleClass ; private final ObservableList&lt;Integer&gt; styledRowIndices ; private final Callback&lt;TableView&lt;T&gt;, TableRow&lt;T&gt;&gt; baseFactory ; public StyleChangingRowFactory(String styleClass, Callback&lt;TableView&lt;T&gt;, TableRow&lt;T&gt;&gt; baseFactory) { this.styleClass = styleClass ; this.baseFactory = baseFactory ; this.styledRowIndices = FXCollections.observableArrayList(); } public StyleChangingRowFactory(String styleClass) { this(styleClass, null); } @Override public TableRow&lt;T&gt; call(TableView&lt;T&gt; tableView) { final TableRow&lt;T&gt; row ; if (baseFactory == null) { row = new TableRow&lt;&gt;(); } else { row = baseFactory.call(tableView); } row.indexProperty().addListener(new ChangeListener&lt;Number&gt;() { @Override public void changed(ObservableValue&lt;? extends Number&gt; obs, Number oldValue, Number newValue) { updateStyleClass(row); } }); styledRowIndices.addListener(new ListChangeListener&lt;Integer&gt;() { @Override public void onChanged(Change&lt;? extends Integer&gt; change) { updateStyleClass(row); } }); return row; } public ObservableList&lt;Integer&gt; getStyledRowIndices() { return styledRowIndices ; } private void updateStyleClass(TableRow&lt;T&gt; row) { final ObservableList&lt;String&gt; rowStyleClasses = row.getStyleClass(); if (styledRowIndices.contains(row.getIndex()) ) { if (! rowStyleClasses.contains(styleClass)) { rowStyleClasses.add(styleClass); } } else { // remove all occurrences of styleClass: rowStyleClasses.removeAll(Collections.singleton(styleClass)); } } } </code></pre> <p>Now you can do</p> <pre><code>final StyleChangingRowFactory&lt;Person&gt; rowFactory = new StyleChangingRowFactory&lt;&gt;("highlightedRow"); table.setRowFactory(rowFactory); </code></pre> <p>And in your button's action handler do </p> <pre><code> rowFactory.getStyledRowIndices().setAll(table.getSelectionModel().getSelectedIndices()); </code></pre> <p>Because StyleChangingRowFactory wraps another row factory, you can still use it if you already have a custom row factory implementation you want to use. For example:</p> <pre><code>final StyleChangingRowFactory&lt;Person&gt; rowFactory = new StyleChangingRowFactory&lt;Person&gt;( "highlightedRow", new Callback&lt;TableView&lt;Person&gt;, TableRow&lt;Person&gt;&gt;() { @Override public TableRow&lt;Person&gt; call(TableView&lt;Person&gt; tableView) { final TableRow&lt;Person&gt; row = new TableRow&lt;Person&gt;(); ContextMenu menu = new ContextMenu(); MenuItem removeMenuItem = new MenuItem("Remove"); removeMenuItem.setOnAction(new EventHandler&lt;ActionEvent&gt;() { @Override public void handle(ActionEvent event) { table.getItems().remove(row.getItem()); } }); menu.getItems().add(removeMenuItem); row.contextMenuProperty().bind( Bindings.when(row.emptyProperty()) .then((ContextMenu) null) .otherwise(menu)); return row; } }); table.setRowFactory(rowFactory); </code></pre> <p><a href="https://gist.github.com/james-d/7813134">Here</a> is a complete code example.</p>
    singulars
    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. 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