我有一个包含5列和4行的表格视图。如果我从其中一行中选择说两个单元格,如何找到该值(即连续选择的单元格数)。
我可以使用
找出关注的细胞table.getFocusModel().getFocusedCell()
所选行数
table.getSelectionModel().getSelectedItems().size()
其中
table = new TableView(...)
答案 0 :(得分:0)
您可以使用table.getSelectionModel().getSelectedCells()
获取所有选定TablePosition
的列表。对于每个TablePosition
,getRow()
会告诉您它在哪一行,因此您可以按行计算它们。
例如:
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TableViewCountSelectedCellsByRow extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Item> table = new TableView<>();
table.getColumns().addAll(Arrays.asList(
createCol("Name", Item::nameProperty),
createCol("Value", Item::valueProperty)
));
final int numItems = 100 ;
final Random rng = new Random();
IntStream.rangeClosed(1, numItems)
.mapToObj(i -> new Item("Item "+i, rng.nextInt(20)))
.forEach(table.getItems()::add);
table.getSelectionModel().setCellSelectionEnabled(true);
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
table.getSelectionModel().getSelectedCells().addListener((ListChangeListener.Change<? extends TablePosition> c) -> {
// table.getSelectionModel().getSelectedCells() returns a list of all selected TablePositions
// Just iterate through it and count up cells by row
Map<Integer, Integer> selectedCellCountByRow = new HashMap<>();
table.getSelectionModel().getSelectedCells().forEach( p ->
selectedCellCountByRow.merge(p.getRow(), 1, Integer::sum));
// Display all row counts:
selectedCellCountByRow.keySet().stream().sorted(Integer::compare).forEach( r ->
System.out.println("Row " + r + " has "+selectedCellCountByRow.get(r)+" cells selected"));
System.out.println();
});
BorderPane root = new BorderPane(table);
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
public static <S,T> TableColumn<S,T> createCol(String title, Function<S, ObservableValue<T>> property) {
TableColumn<S,T> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
return col ;
}
public static class Item {
private final StringProperty name = new SimpleStringProperty();
private final IntegerProperty value = new SimpleIntegerProperty();
public Item(String name, int value) {
setName(name);
setValue(value);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
public final IntegerProperty valueProperty() {
return this.value;
}
public final int getValue() {
return this.valueProperty().get();
}
public final void setValue(final int value) {
this.valueProperty().set(value);
}
@Override
public String toString() {
return getName();
}
}
public static void main(String[] args) {
launch(args);
}
}