1

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.

Elltz
  • 10,730
  • 4
  • 31
  • 59
DeepInJava
  • 1,871
  • 3
  • 16
  • 31

2 Answers2

3

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();
  }
}
BadVegan
  • 494
  • 6
  • 8
0

It is using mouse handler on the table view which checks when mouse is clicked and on clicking it fires a new dialogue in fx and then accepts the input and updates the fx table view.

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

Or if you want right click you can create cell

Eg

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 ;
    }
});
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86