Javafx选择多行

时间:2014-03-22 19:49:50

标签: java javafx tableview

我试图在javafx中选择多行,但我不想使用键盘(SHIFT)。我应该在点击它之后选择它,当我点击另一列时它也应该被选中。< / p>

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

@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);

3 个答案:

答案 0 :(得分:29)

tableView.getSelectionModel().setSelectionMode(
    SelectionMode.MULTIPLE
);

答案 1 :(得分:4)

这只是您可能正在寻找的基本概念。可以使用一些改进,但我不清楚预期的用途。

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        ObservableList<LineItem> items = FXCollections.observableArrayList();
        items.addAll(new LineItem("hello",123.45),
                     new LineItem("paid in full",0.01),
                     new LineItem("paid",0.01),
                     new LineItem("due",0.01),
                     new LineItem("paid",0.01));

        ObservableList<LineItem> filteredItems = FXCollections.observableArrayList(items);
        TableView<LineItem> tableView = new TableView<>(filteredItems);
        ObservableList<TablePosition> selectedCells = FXCollections.observableArrayList();

        Button multi = new Button("Multi-select");
        multi.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                multi.setText((multi.getText().equals("Multi-select"))?"Single-select":"Multi-select");
                selectedCells.clear();
            }
        });

        TableColumn<LineItem,String> descCol = new TableColumn<>("desc");
        descCol.setCellValueFactory(new PropertyValueFactory<>("desc"));

        TableColumn<LineItem, Double> amountCol = new TableColumn<>("amount");
        amountCol.setCellValueFactory(new PropertyValueFactory<>("amount"));

        tableView.getColumns().addAll(descCol,amountCol);
        tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        tableView.getSelectionModel().setCellSelectionEnabled(true);

        //maybe you want onTouchPressed here for tablet
        tableView.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (multi.getText().equals("Single-select")) return;
                selectedCells.add(
                    tableView.getSelectionModel().getSelectedCells().get(
                        tableView.getSelectionModel().getSelectedCells().size()-1
                    )
                );
                for (TablePosition tp : selectedCells){
                    tableView.getSelectionModel().select(tp.getRow(), tp.getTableColumn());
                }
            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(tableView, multi);
        Scene scene = new Scene(root, 300, 300);

        primaryStage.setTitle("multi select table test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public class LineItem {

        private final StringProperty desc = new SimpleStringProperty();
        private final DoubleProperty amount = new SimpleDoubleProperty();

        public StringProperty descProperty() {return desc;}
        public DoubleProperty amountProperty() {return amount;}

        public LineItem(String dsc, double amt) {
            desc.set(dsc); amount.set(amt);
        }
    }

}

答案 2 :(得分:2)

自定义RowFactory是解决此问题的好方法:

Project tool window