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.
Asked
Active
Viewed 2,117 times
1

Elltz
- 10,730
- 4
- 31
- 59

DeepInJava
- 1,871
- 3
- 16
- 31
-
Hello, you want to show dialog directly after right click? – BadVegan Sep 24 '16 at 16:06
-
@BadVegan, yes I want to show dialog directly after right click of cell. – DeepInJava Sep 24 '16 at 19:10
2 Answers
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:
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
-
thanks for your effort, but somehow it is not working for me, Can you please paste your whole program. – DeepInJava Sep 25 '16 at 18:33
-
-
It worked for me now. My project wasn't build properly at that time. Sorry! – DeepInJava Sep 25 '16 at 19:14
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