【问题标题】:Selecting and deselecting items individually or together in JavaFX在 JavaFX 中单独或一起选择和取消选择项目
【发布时间】:2017-01-31 00:23:21
【问题描述】:

我有一个标签矩阵,我将其添加到 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);

        }

【问题讨论】:

    标签: java javafx mouseevent


    【解决方案1】:

    您需要实现“完全按下-拖动-释放手势”,如MouseEvent documentation 中所述。

    这是一个快速演示(虽然标签状态的更新效率不是很高,但您可以通过一些工作对其进行优化)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 2021-10-29
      相关资源
      最近更新 更多