2017-01-31 9 views
0

私はラベルのマトリックスをグリッドパネルに追加しています。私はイベント(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 ; 
} 
関連する問題