2016-12-26 117 views
1

如何选择像ListView这样的项目来处理clickListener?是否有gridView示例?爱你JavaFX在org.controlsfx.control.GridView.GridView中选择项目

 ObservableList<Image> images = FXCollections.<Image>observableArrayList(list); 

     GridView<Image> gridView = new GridView<Image>(images); 
     gridView.setOnMouseClicked(new EventHandler<MouseEvent>(){ 

      @Override 
      public void handle(MouseEvent event) { 
       //TODO     
      }    
     }); 

     gridView.setCellFactory(new Callback<GridView<Image>, GridCell<Image>>() { 

      @Override 
      public GridCell<Image> call(GridView<Image> param) { 

       return new ImageGridCell() ; 
      } 

     }); 

如何选择项像ListView处理程序clickListener?

回答

0

你可以像这样做一个简单的方法。

class ImageCell extends GridCell<String>{ 
    ImageCell(){ 
     setOnMouseClicked(new EventHandler<Event>() { 

     @Override 
     public void handle(Event event) { 
      // TODO Auto-generated method stub 
      if (((MouseEvent) event).getClickCount() >= 2) { 
        //do something when it's clicked 

      } 

     } 
     }); 
    } 
    protected void updateItem(String item, boolean empty) { 
     // TODO Auto-generated method stub 
     super.updateItem(item, empty); 
     if (!empty) { 
      // you can set content of cell here just as you do in 
      //javaFX apllcation 
      // and call setGraphic(root) eventually. 
      // you can also call setOnAction on sub node in root rather 
      // than do it in constructor. 
     } 
    } 

} 
+0

你能告诉我一个详细的代码示例如何添加监听器吗?非常感谢。 –

+0

我编辑过它,你能理解如何处理你的问题吗? –