JavaFx 电子表格单元格右键单击打开对话框

JavaFx Spreadsheet Cell Right Click Open Dialog

我是 ControlsFx Api 的 SpreadSheet 功能的新手。我想在 Javafx 中右键单击 SpreadsheetViewSpreadsheetcell 打开 Dialog。非常感谢任何帮助。

这是您可以关闭标准上下文菜单并使用 Dialog 实现自己的处理程序的代码,在本例中 TextInputDialog:

  SpreadsheetView spreadsheetView = new SpreadsheetView();
    //off the standard ContextMenu
    spreadsheetView.setContextMenu(null);
   //set own handler for right click with Dialog
    spreadsheetView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
      @Override public void handle(ContextMenuEvent event) {
        CellView cellView = (CellView) event.getTarget();
        TextInputDialog dialog = new TextInputDialog(cellView.getText());
        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()){
          System.out.println(cellView.getText());
        }
      }
    });

我不太了解这个库,但它很好用。 工作原理示例:

我的程序:

public class MainController extends Application {

  public static void main(String[] args) {
    launch(args);
  }

  @Override public void start(Stage primaryStage) throws Exception {


    SpreadsheetView spreadsheetView = new SpreadsheetView();
    //off the standard ContextMenu
    spreadsheetView.setContextMenu(null);
    //set own handler for right click with Dialog
    spreadsheetView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
      @Override public void handle(ContextMenuEvent event) {
        CellView cellView = (CellView) event.getTarget();
        TextInputDialog dialog = new TextInputDialog(cellView.getText());
        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()) {
          System.out.println(cellView.getText());
        }
      }
    });

    HBox hBox = new HBox();
    hBox.getChildren().add(spreadsheetView);
    Scene scene = new Scene(hBox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

它在 table 视图上使用鼠标处理程序,它检查何时单击鼠标并在单击鼠标时触发 fx 中的新对话,然后接受输入并更新 fx table 视图。

table.setOnMousePressed(new          EventHandler<MouseEvent>() {
     @Override 
    public void handle(MouseEvent event) {
        if (event.getClickCount() == 1) {
            Call dialogue method of java fx           
        }
    }
});

或者如果你想右击你可以创建单元格

例如

FirstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
    @Override
    public TableCell<Person, String> call(TableColumn<Person, String> col) {
        final TableCell<Person, String> cell = new TableCell<>();
        cell.textProperty().bind(cell.itemProperty()); // in general might need to subclass TableCell and override updateItem(...) here
        cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getButton == MouseButton.SECONDARY) {
                    // handle right click on cell...
                    // access cell data with cell.getItem();
                    // access row data with (Person)cell.getTableRow().getItem();
                }
            }
        });
        return cell ;
    }
});