JavaFX tableview隐藏选定行

JavaFX tableview隐藏选定行,javafx,tableview,Javafx,Tableview,在我的JavaFX应用程序中,我试图在tableview中隐藏选定的行。我使用CSS如下 .hideRow { -fx-cell-size: 0px; -fx-border-width: 0; } 我使用下面的一行工厂来实现同样的目标 import java.util.Collections; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import

在我的JavaFX应用程序中,我试图在tableview中隐藏选定的行。我使用CSS如下

.hideRow
{
     -fx-cell-size: 0px;
     -fx-border-width: 0;
}
我使用下面的一行工厂来实现同样的目标

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.event.EventHandler;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.util.Callback;

/

**
 * This is the table view row style factory class. * This class is responsible
 * for performing style changes for enable,disable,hide and un hide of the rows.
 *
 * @param <T>
 */

public class StyleChangingRowFactory<T> implements
    Callback<TableView<T>, TableRow<T>> {
    private TableRow rowSelected;
    private final String styleClass;
    private final ObservableList<Integer> styledRowIndices;
    private final Callback<TableView<T>, TableRow<T>> baseFactory;

    /**
     * Construct a <code>StyleChangingRowFactory</code>, specifying the name of
     * the style class that will be applied to rows determined by
     * <code>getStyledRowIndices</code> and a base factory to create the
     * <code>TableRow</code>. If <code>baseFactory</code> is <code>null</code>,
     * default table rows will be created.
     *
     * @param styleClass The name of the style class that will be applied to
     * specified rows.
     * @param baseFactory A factory for creating the rows. If null, default
     * <code>TableRow&lt;T&gt;</code>s will be created using the default
     * <code>TableRow</code> constructor.
     */
    public StyleChangingRowFactory(String styleClass, Callback<TableView<T>, TableRow<T>> baseFactory) {
        this.styleClass = styleClass;
        this.baseFactory = baseFactory;
        this.styledRowIndices = FXCollections.observableArrayList();
    }

    /**
     * Construct a <code>StyleChangingRowFactory</code>, which applies
     * <code>styleClass</code> to the rows determined by
     * <code>getStyledRowIndices</code>, and using default
     * <code>TableRow</code>s.
     *
     * @param styleClass
     */
    public StyleChangingRowFactory(String styleClass) {
        this(styleClass, null);
    }

    @Override
    public TableRow<T> call(final TableView<T> tableView) {
        final TableRow<T> row;
        if (baseFactory == null) {
            row = new TableRow<>();
        } else {
            row = baseFactory.call(tableView);
        }
         row.setOnDragEntered(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent t) {

                tableView.getSelectionModel().clearSelection();
                tableView.getSelectionModel().selectRange(rowSelected.getIndex(), row.getIndex());
            }
        });

        row.setOnDragDetected(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                rowSelected = row;
                Dragboard db = row.getTableView().startDragAndDrop(TransferMode.LINK);
                ClipboardContent content = new ClipboardContent();
                content.put(DataFormat.PLAIN_TEXT, "XData");
                db.setContent(content);
                tableView.getSelectionModel().clearSelection();
                t.consume();
            }
        });
        row.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                rowSelected = row;
                final int index = row.getIndex();
                if (event.getButton() == MouseButton.SECONDARY && index >= 0 && !tableView.getSelectionModel().isSelected(index)) {
                    tableView.getSelectionModel().clearSelection();
                }
            }
        });
        row.indexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> obs,
                    Number oldValue, Number newValue) {
                updateStyleClass(row);
            }
        });
        styledRowIndices.addListener(new ListChangeListener<Integer>() {
            @Override
            public void onChanged(Change<? extends Integer> change) {
                updateStyleClass(row);
                tableView.getColumns().get(0).setVisible(false);
                tableView.getColumns().get(0).setVisible(true);
            }
        });

        return row;
    }

    /**
     *
     * @return The list of indices of the rows to which <code>styleClass</code>
     * will be applied. Changes to the content of this list will result in the
     * style class being immediately updated on rows whose indices are either
     * added to or removed from this list.
     */
    public ObservableList<Integer> getStyledRowIndices() {
        return styledRowIndices;
    }

    private void updateStyleClass(TableRow<T> row) {
        final ObservableList<String> rowStyleClasses = row.getStyleClass();
        if (styledRowIndices.contains(row.getIndex())) {
            if (!rowStyleClasses.contains(styleClass)) {
                rowStyleClasses.add(styleClass);

            }
        } else {
// remove all occurrences of styleClass:
            rowStyleClasses.removeAll(Collections.singletonList(styleClass));
        }
    }

}
我宣布该行为工厂

StyleChangingRowFactory  rowFactory = new StyleChangingRowFactory<>("hideRow");

问题是当我隐藏所选行时。它还在表视图中隐藏一些其他行。请帮我解决这个问题。

你能说明你是如何使用行工厂的,以及你是如何操作它的
styledrovindices
列表的吗?@James_D在我的控制器中,我声明行工厂为StyleChangingRowFactory rowFactory=new StyleChangingRowFactory(“hideRow”);私有void hidePressed(){rowFactory.getStyledRowIndices().addAll(cfiTableView.getSelectionModel().GetSelectedDices());}从该列表中从何处删除值?你能编辑这个问题并添加更多细节吗?@James_D我现在更新了这个问题。@user2971296,你能创建一个并用它更新你的问题吗?
private void hidePressed() {
        rowFactory.getHiddenRowIndices().addAll(cfiTableView.getSelectionModel().getSelectedIndices());
    }

    private void unHidePressed() {
        rowFactory.getHiddenRowIndices().removeAll(cfiTableView.getSelectionModel().getSelectedIndices());
    }