2017-01-31 59 views
0

我有一個標籤矩陣,我將它添加到GridPane中。我向矩陣中的每個標籤添加了一個事件(setOnMouseClicked)以選擇或取消選擇相同的標籤,因此每次按下鼠標時,都會選擇或取消選擇某個顏色的標籤(單獨)。但是現在我想在Excel中選擇多個標籤:通過短時間按下鼠標選擇多個單元格。不必點擊一個標籤,然後點擊下一個和下一個,等等......(這很慢)。在JavaFX中單獨或一起選擇和取消選擇項目

我以爲它是一個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

回答

0

你需要實現「全壓阻力釋放手勢」,如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 ; 
} 
相關問題