Sanket Pipariya Sanket Pipariya - 2 months ago 33
Java Question

JavaFx Spreadsheet Cell Right Click Open Dialog

I am new to SpreadSheet functionality of ControlsFx Api. I would like to open

Dialog
on right click of
Spreadsheetcell
of
SpreadsheetView
in Javafx. Any help is greatly appreciated.

Answer

this is code where you can off the standard ContextMenu and implements own handler with Dialog, in this example 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());
        }
      }
    });

I don't know very good this library, but it works good. Example how it works:

enter image description here

My program:

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();
  }
}