在我的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<T></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");
我的桌子上有两个弹出菜单,它们是隐藏和取消隐藏。
在我的控制器中,我使用了以下方法来实现这些菜单。
private void hidePressed() {
rowFactory.getHiddenRowIndices().addAll(cfiTableView.getSelectionModel().getSelectedIndices());
}
private void unHidePressed() {
rowFactory.getHiddenRowIndices().removeAll(cfiTableView.getSelectionModel().getSelectedIndices());
}
问题是当我隐藏选定的行时。它还隐藏了表视图中的其他一些行。请帮我解决这个问题。