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

时间:2016-09-17 20:13:44

标签: java javafx event-handling controlsfx

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

2 个答案:

答案 0 :(得分:3)

这是一个代码,您可以在其中关闭标准ContextMenu并使用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());
        }
      }
    });

我不太了解这个图书馆,但它运作良好。 示例如何工作:

enter image description here

我的节目:

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

答案 1 :(得分:0)

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

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