Javafx选择多行

Tol*_*mer 14 java javafx tableview

我试图在javafx中选择多行,但我不想使用键盘(SHIFT).应该在我点击它之后选择,当我点击其他列时它也应该被选中.

我在这里检查了一些其他的答案,但我找不到简短而方便的东西.有更短的方法吗?

@FXML 

private static Logger logger = Logger.getLogger(MainFrameControl.class);


public TableView<Box> boxTable;
protected final ObservableList<Box> boxData = FXCollections.observableArrayList();

Service service = new ServiceImpl();
private Stage mainStage;

public MainFrameControl(Stage mainStage) {



    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainFrame.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);
    this.mainStage = mainStage;


    Stage stage = new Stage();



    try {
        fxmlLoader.load();  

    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }


    ArrayList<Box> list = service.findAllBoxen();

    TableColumn<Box, String> boxnameColumn = (TableColumn<Box, String>) boxTable.getColumns().get(0);
    boxnameColumn.setCellValueFactory(new PropertyValueFactory<Box, String>("boxName"));


    TableColumn<Box, String> sizeColumn = (TableColumn<Box, String>) boxTable.getColumns().get(1);
    sizeColumn.setCellValueFactory(new PropertyValueFactory<Box, String>("size"));

    TableColumn<Box, String> windowColumn = (TableColumn<Box, String>) boxTable.getColumns().get(2);
    windowColumn.setCellValueFactory(new PropertyValueFactory<Box, String>("window"));

    TableColumn<Box, String> costColumn = (TableColumn<Box, String>) boxTable.getColumns().get(3);
    costColumn.setCellValueFactory(new PropertyValueFactory<Box, String>("cost"));

    TableColumn<Box, String> locationColumn = (TableColumn<Box, String>) boxTable.getColumns().get(4);
    locationColumn.setCellValueFactory(new PropertyValueFactory<Box, String>("location"));

    TableColumn<Box, Boolean> beddingColumn = (TableColumn<Box, Boolean>) boxTable.getColumns().get(5);
    beddingColumn.setCellValueFactory(new PropertyValueFactory<Box, Boolean>("bedding"));

    boxTable.setItems(boxData);
Run Code Online (Sandbox Code Playgroud)

jew*_*sea 31

tableView.getSelectionModel().setSelectionMode(
    SelectionMode.MULTIPLE
);
Run Code Online (Sandbox Code Playgroud)

  • @jewelsea ,在按照你的建议我们仍然需要用键盘(Ctrl+单击)选择多行之后,有没有办法只用鼠标点击? (2认同)

alc*_*lis 5

自定义RowFactory是解决这个问题的一个很好的解决方案:

boxTable.setRowFactory(new Callback<TableView<Box>, TableRow<Box>>() {
    @Override
    public TableRow<Box> call(TableView<Box> tableView2)
    {
        final TableRow<Box> row = new TableRow<>();

        row.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event)
            {
                final int index = row.getIndex();

                if (index >= 0 && index < boxTable.getItems().size())
                {
                    if(boxTable.getSelectionModel().isSelected(index))
                        boxTable.getSelectionModel().clearSelection(index);
                    else
                        boxTable.getSelectionModel().select(index);

                    event.consume();
                }
            }
        });
        return row;
    }
});
Run Code Online (Sandbox Code Playgroud)