2012-12-13 53 views
0

我想將一個事件處理程序添加到多個節點,這是onDragDetected事件。但是當我把它分配給一個循環時,它似乎沒有註冊它。我使用的代碼如下所示:在一個循環中添加事件處理程序

import java.util.ArrayList; 

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.input.ClipboardContent; 
import javafx.scene.input.DragEvent; 
import javafx.scene.input.Dragboard; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.input.TransferMode; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.text.Text; 
import javafx.stage.Screen; 
import javafx.stage.Stage; 

public class DragAndDrop extends Application { 
    Group page1,page2; 
    ArrayList<Group> pages; 
    FlowPane bottom; 
    public static void main(String[] args) { 
     launch(); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     pages = new ArrayList<Group>(); 
     stage.setTitle("scale test"); 
     BorderPane border = new BorderPane(); 
     Group root = new Group(); 
     bottom = new FlowPane(); 
     bottom.setMinSize(Screen.getPrimary().getVisualBounds().getWidth(), 200); 
     //bottom.setStyle("-fx-background-color: grey"); 
     page1 = new Group(); 
     page2 = new Group(); 
     Rectangle r = new Rectangle(0,0,200, 200); 
     Rectangle r2 = new Rectangle(205,0,200,200); 
     Text t = new Text(
       "asodjoijdjiasjdoijaisjd\nijasijdiajsidjoias\njiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs"); 
     t.setFill(Color.FLORALWHITE); 
     t.setWrappingWidth(180); 
     t.setLayoutX(10); 
     t.setLayoutY(20); 
     Text t2 = new Text(
       "asodjoijdjiasjdoijaisjdijasijdiajsidjoiasjiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs"); 
     t2.setFill(Color.FLORALWHITE); 
     t2.setWrappingWidth(180); 
     t2.setX(r2.getX()+10); 
     t2.setY(r2.getY()+20); 
     page1.getChildren().addAll(r, t); 
     page2.getChildren().addAll(r2,t2); 
     setDrag(); 
     root.getChildren().addAll(page1,page2); 
     pages.add(page1); 
     pages.add(page2); 
     border.setCenter(root); 
     border.setBottom(bottom); 
     Scene scene = new Scene(border, 800, 700); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    private void createPage(String s){ 
     Group page = new Group(); 
     Rectangle rect = new Rectangle(200,200); 
     Text text = new Text(
       "asodjoijdjiasjdoijaisjdijasijdiajsidjoiasjiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs"); 
     text.setFill(Color.FLORALWHITE); 
     text.setText(s); 
     text.setWrappingWidth(180); 
     text.setX(rect.getX()+10); 
     text.setY(rect.getY()+20); 
     page.getChildren().addAll(rect,text); 
     bottom.getChildren().add(page); 
    } 

    private void setDrag(){ 
     int pagenum = 0; 
     while(pagenum<pages.size()){ 
      final Group page = pages.get(pagenum); 
      page.setOnDragDetected(new EventHandler <MouseEvent>() { 
       public void handle(MouseEvent event) { 
        Dragboard drag = page.startDragAndDrop(TransferMode.ANY); 
        ClipboardContent content = new ClipboardContent(); 
        content.putString(((Text) page.getChildren().get(1)).getText()); 
        drag.setContent(content); 

        event.consume(); 
       } 
      }); 
      pagenum++; 
     }/* 
     page1.setOnDragDetected(new EventHandler <MouseEvent>() { 
      public void handle(MouseEvent event) { 
       Dragboard drag = page1.startDragAndDrop(TransferMode.ANY); 
       ClipboardContent content = new ClipboardContent(); 
       content.putString(((Text) page1.getChildren().get(1)).getText()); 
       drag.setContent(content); 

       event.consume(); 
      } 
     }); 

     page2.setOnDragDetected(new EventHandler <MouseEvent>() { 
      public void handle(MouseEvent event) { 
       Dragboard drag = page2.startDragAndDrop(TransferMode.ANY); 
       ClipboardContent content = new ClipboardContent(); 
       content.putString(((Text) page2.getChildren().get(1)).getText()); 
       drag.setContent(content); 

       event.consume(); 
      } 
     });*/ 

     bottom.setOnDragOver(new EventHandler <DragEvent>() { 
      public void handle(DragEvent event) { 
       /* data is dragged over the target */ 

       /* accept it only if it is not dragged from the same node 
       * and if it has a string data */ 
       if (event.getGestureSource() != bottom && 
         event.getDragboard().hasString()) { 
        /* allow for both copying and moving, whatever user chooses */ 
        event.acceptTransferModes(TransferMode.COPY_OR_MOVE); 
       } 

       event.consume(); 
      } 
     }); 

     bottom.setOnDragEntered(new EventHandler <DragEvent>() { 
      public void handle(DragEvent event) { 
       /* show to the user that it is an actual gesture target */ 
       if (event.getGestureSource() != bottom && 
         event.getDragboard().hasString()) { 
        bottom.setStyle("-fx-background-color: green"); 
       } 

       event.consume(); 
      } 
     }); 

     bottom.setOnDragExited(new EventHandler <DragEvent>() { 
      public void handle(DragEvent event) { 
       /* mouse moved away, remove the graphical cues */ 
       bottom.setStyle("-fx-background-color: grey"); 

       event.consume(); 
      } 
     }); 

     bottom.setOnDragDropped(new EventHandler <DragEvent>() { 
      public void handle(DragEvent event) { 
       /* if there is a string data on dragboard, read it and use it */ 
       Dragboard db = event.getDragboard(); 
       boolean success = false; 
       if (db.hasString()) { 
        createPage(db.getString()); 
        success = true; 
       } 
       /* let the source know whether the string was successfully 
       * transferred and used */ 
       event.setDropCompleted(success); 

       event.consume(); 
      } 
     }); 
    } 

} 

任何幫助,將不勝感激。 謝謝

回答

1

你在呼喚你的setDrag()方法的頁面添加到列表ArrayList<Group> pages之前。在行後調用它pages.add(page2);

+0

omg ...不能相信我沒有發現。謝謝 – sazap10

0

您沒有創建一個事件處理程序,但它們是多個。 new運算符創建一個新對象。

while(pagenum<pages.size()){ 
     final Group page = pages.get(pagenum); 
     page.setOnDragDetected(new EventHandler <MouseEvent>() { 
      ... 

while

EventHandler eh = new EventHandler <MouseEvent>() { 
    ... 
}; 

之前創建的事件處理程序,並將其應用

while(pagenum<pages.size()){ 
     final Group page = pages.get(pagenum); 
     page.setOnDragDetected(eh); 
     ... 
+0

但事件處理程序需要對頁面的引用。這就是爲什麼我創建它在循環 – sazap10

+0

@ sazap10,什麼引用一個頁面?你現在把它傳遞給一個事件處理程序?如果你在循環中調用'new',你將得到多個處理程序,而不是一個。 –

+0

處理程序內部我做這一行 Dragboard drag = page.startDragAndDrop(TransferMode.ANY); 它需要數組中的對象,因此它需要在循環中。早些時候,我爲每個單獨的對象創建了一個不同的對象,它們工作正常 – sazap10

相關問題