我正在尝试为我设计的注释窗口(舞台)实现一个侦听器,其中包含一个文本框和两个活动按钮。
" OK"按钮方法:
// Ok Button listener.
okButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
try
{
//Close the stage
Stage stage = (Stage) okButton.getScene().getWindow();
stage.close();
//Parse the textbox text to a singleton
TOPGun_Site_Archive_Singleton.getInstance().setAddNewCommentText(siteArchive_Comments_TextArea.getText());
}
catch (Exception ex)
{Logger.getLogger(Project_TOP_GUN.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
这是&#34;取消&#34;按钮方法:
//Cancel Button listener
cancelButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
try
{
//Close the stage
Stage stage = (Stage) okButton.getScene().getWindow();
stage.close();
}
catch (Exception ex)
{
Logger.getLogger(Project_TOP_GUN.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
当用户完全离开评论阶段时,我想触发&#34;确定&#34;我在评论阶段的控制器中实现的按钮事件方法。
我尝试在评论窗口的文本框中附加焦点监听器,但如果用户尝试在其他任何地方点击 ,则文本框失去焦点并且&#34; OK&#34;方法被触发。
siteArchive_Comments_TextArea.focusedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Textfield on focus");
//do nothing
}
else
{
System.out.println("Textfield out focus");
//If the Textfield gets out of focus, run the OK method. i.e copy the contents of the Textfield to another stage and close the comment window
okButton.fire();
}
}
});
我还尝试将焦点侦听器附加到调用此注释窗口的父级,但是当用户点击评论阶段时,我无法访问&#34; OK&#34;评论阶段的控制器内部的方法。
// Call and open the comment stage from here
// and place a listener on the comment stage itself
floating_TextArea_Stage.setScene(floating_TextArea_Scene);
floating_TextArea_Stage.initStyle(StageStyle.TRANSPARENT);
floating_TextArea_Stage.show();
// add the focus listener on the stage
floating_TextArea_Stage.focusedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Stage on focus");
//no need to do anything here
}
else
{
System.out.println("Stage out of focus");
//this doesnot work
//okButton.fire();
// This doesn't work either
floating_TextArea_Stage.okButton.fire()
}
}
});
我一直在努力为DAYS找到最好的办法!你有什么想法吗?