Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx select multiple rows

I'm trying to select multiple rows in JavaFX but I don't want to use keyboard (i.e. SHIFT key) for that. It should be selected after I click on it and when I click on another column it should be selected as well.

I checked some other answers here but I couldn't find something short and handy. Is there a shorter way to do it?

@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);
like image 849
Tolga Tamer Avatar asked Mar 22 '14 19:03

Tolga Tamer


People also ask

How to select row in JavaFX?

It is possible to select rows programmatically in a JavaFX TableView. You do so via the TableViewSelectionModel object's many selection methods. To select a row with a specific index you can use the select(int) method.

How do you select multiple rows in Java?

To select more than one row in a JTable, use the setRowSelectionInterval() method. Here, set the indexes as interval for one end as well as other end.

What is TableView in JavaFX?

The TableView class provides built-in capabilities to sort data in columns. Users can alter the order of data by clicking column headers. The first click enables the ascending sorting order, the second click enables descending sorting order, and the third click disables sorting. By default, no sorting is applied.


2 Answers

tableView.getSelectionModel().setSelectionMode(
    SelectionMode.MULTIPLE
);
like image 145
jewelsea Avatar answered Sep 19 '22 03:09

jewelsea


This is just a basic idea of what you may be looking for. Could use a bit of improvement but I'm unclear on the intended use.

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

}
like image 38
brian Avatar answered Sep 21 '22 03:09

brian