JavaFX FXML Controller onClose or equivalent? - javafx

Is there a method for a JavaFX controller that gets called when the user leaves that view in the application? The opposite of the initialize() method basically.
I want to do this because I have a page that shows a webcam stream waiting for the user to press a button that takes a snapshot, and I need to close that stream if the user doesn't take a snapshot and leaves the screen. Any other ways of doing this would be appreciated too.
Thanks!

You can use the window's hiding event...
private void detachHook(final Node parent) {
parent.getScene().getWindow()
.addEventFilter(WindowEvent.WINDOW_HIDING, event -> {
//Kill your webcam stream
});
}
You will have to check if you also get the parent as we do in our application, but the window hiding event should work if a node and a scene are present.

Related

android don't lose Fragment when back button is pressed

The user create and add some Fragments to the Activity identified by a tag. I noticed that on pressing the back button the Fragment is destroyed. How can I don't lose all the fragments when the back button is pressing? So that I can navigate without recreating all the time the fragments. I'm asking for a properly way to do this, indeed for now my idea is to override the onBackPressed() and save the Fragment in a List global variable of Fragments before destroy it.
While creating the fragment's instead of FragmentTranscation.replace, use FragmentTranscation.addTobackStack() to add the fragment to backstack and do nothing onBackPressed.
On pressing back button FragmentTranscation will take care of navigating to the previous fragments.
You can try this in the onBackPressed() function.
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}

How to set default close operation in JavaFX?

When I press the red close button, I want the window not to close on JavaFX.
Any suggestions?
From the Javadocs for Window.setOnCloseRequest():
Called when there is an external request to close this Window. The installed event handler can prevent window closing by consuming the received event.
So all you need is
stage.setOnCloseRequest(Event::consume);
or, if you are going to perform other actions as well:
stage.setOnCloseRequest(event -> {
// do some stuff...
event.consume();
});

javafx: How to get back to original application window without closing the new window?

Apologies for asking such a simple question but english is not my first language, so I cannot find the right word to describe my problem and cannot google up the the right question.
I have a javafx application and there is a button whereby if the user clicks it, it generates a new window (like a display box just to display more info to a user). The problem is when the new window is displayed and I click anywhere of the Javafx application, I cannot get back to it. I have to close the new window first in order to interact with the original javafx application.
How can I have the ability to open the new window on a button click, while retaining the ability to interact the original javafx application without having to close the new window ?
Button moreInfo = new Button("More Info");
moreInfo.setOnAction(e -> {
MoreInfoDisplayBox.display();
});
public class MoreInfoDisplayBox {
public static void display(){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("More Info");
window.setMinWidth(400);
window.setMinHeight(400);
window.show();
}
}
The behavior you describe is caused by calling
window.initModality(Modality.APPLICATION_MODAL);
(See docs.)
Instead, just use the default, which you can do explicitly with
window.initModality(Modality.NONE);
or of course just omit that line entirely.

How to scroll QLabel?

I am trying to implement Picture Zoom In/Out and Picture Scroll on Meego/Qt/QML.
I have written a class A which is inherited from QLabel.
A::A( "parent" )
{
setAlignment();
setGeometry();
setScaledContents();
}
Now I have a Controller class B. This class is responsible to handle the events from QML to the my Class A. In my controller class I have instantiate in the following way.
B :: B()
{
a = new A();
proxyWidget = new QGraphicsProxyWidget();
proxyWidget->setWidget(a);
}
Since this is a QML based application I am handling events from QML.
For Zoom I have used PinchArea. Whenever I am getting PinchUpdated event I am setting the setGeometryof the QLabel accordingly. I am zooming in and zooming out.
For scroll I have used MouseArea with onPositionChanged event. However I am unable to scroll the label event after calling the scroll API of the QLabel.
Can someone please tell me where am I doing wrong?
I assume you want to zoom using mouse scroll for instance.
I am not good at QML but you certainly should be handling wheel events in your class A. It is not clear for me if every Qt event has a QML equivalent, but you can always put C++ code. The function to implement is :
virtual void wheelEvent ( QWheelEvent * event );
You have the delta variable which can be useful to determine the speed of zoom (using delta absolute value) and whether it should grow or shrink (using sign of delta)

Who should remove popup, calling app or popup itself?

Here is the scenario...I have an application that has an "Open" button. When you click the open button, a TitleWindow pops up (via the PopUpManager) a dialog that allows you to select a record. In that window, you can either click "Ok" once you've selected a record, or cancel the dialog which should close the window.
What is the best way to handle this, knowing I need to return the record number to the main application? Right now I have the main application opening up the custom TitleWindow. In the TitleWindow, I have an event that fires when the user clicks "Ok" and a record is selected. That's how the main application listens for the recordId. What I'm wondering is if I can do something like this:
private function RecordSelected():void
{
this.selectedRecord = someControl.selectedIndex;
this.dispatchEvent(new Event("RecordSelected"));
PopUpManager.removePopup(this);
}
Will the instance of the TitleWindow stay active until after the event is handled by the main application, or could it be removed before the main application fully handles the event (thus ending with a null reference exception)?
Alternatively, is it better to close the window in the event handler like so:
private function titleWindow_RecordSelected(event:Event)
{
var openDialog:CustomTitleWindow = CustomTitleWindow(e.currentTarget);
this._selectedRecord = openDialog.selectedRecord;
PopUpManager.removePopup(openDialog);
}
I'm also wondering if sending the selection out in an event is the best way to do this also. Any insight would be appreciated. Thanks in advance.
What is the best way to handle this,
knowing I need to return the record
number to the main application?
Best is always subjective, but I usually handle closing of the window in the window component; and then dispatch an event with appropriate data (In this case Record No) back to the application. It sounds like your 90% there. Just instead of dispatching an event Event, create a custom Event class with your recordID. Then it won't matter if the pop up is still in memory or not when you need to use the record ID.

Resources