2017-03-16 96 views
1
tab.setOnCloseRequest(e -> { 
       e.consume(); 
       if (currentEditor.isModified()){ 
        switch (DialogBox.newSaveConfirmationBox(tab.getText())){ 
         case "Yes": tabPane.fireEvent(???); 
        } 
       } 
      } 

我想对选项卡式文本编辑器进行确认,其中有操作“是”“否”取消“。是否有方法来触发关闭选项卡事件时”是“被选中?谢谢JavaFX火灾关闭标签事件

tab.setOnCloseRequest(e -> { 
       if (currentEditor.isModified()){ 
        switch (DialogBox.newSaveConfirmationBox(tab.getText())){ 
         case "Yes": 
          saveFile(); 
         case "Cancel": 
          e.consume(); 
        } 
       } 
      } 
    ); 

可悲的是,当currentEditor.isModified()false,标签不能关闭,有什么建议?谢谢

+0

找到您需要的一个。 http://code.makery.ch/blog/javafx-dialogs-official/。我猜你需要一个确认对话框。 – Sedrick

回答

2

使用该事件的否决权接近。所以,你应该只消耗如果事件用户决定他们不想关闭标签,否则让事件传播,并且标签将关闭:

tab.setOnCloseRequest(e -> { 
    // remove the next line: you only want to consume the event for "No" 
    // e.consume(); 
    if (currentEditor.isModified()){ 
     if ("No".equals(DialogBox.newSaveConfirmationBox(tab.getText()))) { 
      e.consume(); 
     } 
    } 
});