在JavaFX中单独或一起选择和取消选择项目

时间:2017-01-31 00:23:22

标签: java javafx mouseevent

我有一个标签矩阵,我将其添加到GridPane中。我已经为矩阵中的每个标签添加了一个事件(setOnMouseClicked)来选择或取消选择相同的标签,因此每次按下鼠标时,都会选择或取消选择颜色标签(单独)。但现在我想在Excel中选择多个标签:通过短按鼠标选择多个单元格。不必点击标签,然后点击下一个,下一个和下一个,等等......(它非常慢)。

我认为它是MouseEntered和MouseClicked的组合,但我不知道如何或不知道是否有更简单的方法。这是我逐个选择标签的代码:

for (int i = 0; i < matrix.length; i++) 
        for (int j = 0; j < matrix[i].length; j++) {

            matrix[i][j] = new Label();
            matrix[i][j].setAccessibleHelp(i+","+j);
            matrix[i][j].getStyleClass().add("classic-label");
            matrix[i][j].setStyle("-fx-background-color: "+colorDeath+";");
            matrix[i][j].setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    String[] coords = ((Label)event.getSource()).getAccessibleHelp().split(",");
                    //Function that defines the color. I have no problem in this part
                    int x = defineColor(coords);
                    ((Label)event.getSource()).setStyle("-fx-background-color: "+((x == 1) ? colorLife : colorDeath)+";");
                }
            });

            gridPaneMatrix.add(matrix[i][j], i, j);

        }

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要实施&#34;完全按下 - 拖动 - 释放手势&#34;,如MouseEvent documentation中所述。

这是一个快速演示(虽然更新标签&#39;状态效率不高,你可以通过一些工作来优化它。)

import javafx.application.Application;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class DragToSelect extends Application {

    private class Location { int x; int y ;}

    private PseudoClass selectedClass = PseudoClass.getPseudoClass("selected");

    @Override
    public void start(Stage primaryStage) {

        Label[][] matrix = new Label[40][40];
        boolean[][] selected = new boolean[40][40];

        GridPane gridPaneMatrix = new GridPane();

        Location dragAnchor = new Location();



        for (int i = 0; i < matrix.length; i++) 
            for (int j = 0; j < matrix[i].length; j++) {

                Location loc = new Location();
                loc.x = i ;
                loc.y = j ;

                matrix[i][j] = new Label();
                matrix[i][j].setAccessibleHelp(i+","+j);
                matrix[i][j].getStyleClass().add("classic-label");
                matrix[i][j].setOnMousePressed(event -> {
                    dragAnchor.x = loc.x ;
                    dragAnchor.y = loc.y ;
                    selected[loc.x][loc.y] = ! selected[loc.x][loc.y];
                    matrix[loc.x][loc.y].pseudoClassStateChanged(selectedClass, selected[loc.x][loc.y]);
                });

                matrix[i][j].setOnDragDetected(e -> {
                    dragAnchor.x = loc.x ;
                    dragAnchor.y = loc.y ;
                    updateState(selected, matrix, dragAnchor, loc);
                    matrix[loc.x][loc.y].startFullDrag();
                });

                matrix[i][j].setOnMouseDragEntered(e -> {
                    System.out.printf("Dragged [%d, %d]%n", loc.x, loc.y);
                    updateState(selected, matrix, dragAnchor, loc);
                });

                gridPaneMatrix.add(matrix[i][j], i, j);

            }

        Scene scene = new Scene(gridPaneMatrix);
        scene.getStylesheets().add("style.css");

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void updateState(boolean[][] selected, Label[][] matrix, Location anchor, Location target) {
        for (int x = 0 ; x < selected.length ; x++) {
            for (int y = 0 ; y < selected[x].length; y++) {
                selected[x][y] = x >= Math.min(anchor.x, target.x)
                        && x <= Math.max(anchor.x, target.x)
                        && y >= Math.min(anchor.y, target.y)
                        && y <= Math.max(anchor.y, target.y) ;
                matrix[x][y].pseudoClassStateChanged(selectedClass, selected[x][y]);
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

的style.css:

.classic-label {
    -fx-pref-width: 10 ;
    -fx-pref-height: 15 ;
    -fx-background: black ;
    -fx-background-color: orange, -fx-background ;
    -fx-background-insets: 0, 1 1 0 0 ;
}
.classic-label:selected {
    -fx-background: limegreen ;
}