javafx2.0 - force refresh the scene - javafx-2

In my app, one scene having the popup dialog which consists of some fields and buttons. If you click on the button then I want to dismiss the popup dialog as well as update the some fields in the scene. Indirectly I want to refresh scene. is it possible?
I used the following code.Here what I did is, I get the controller of that scene and then update the field using id. but it doesn't work.
URL location = AdmincontrolController.class.getResource("admincontrol.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
try {
Parent root = (Parent) fxmlLoader.load(location.openStream());
AdmincontrolController controller = fxmlLoader.getController();
System.out.println("AdmincontrolController: "+controller);
controller.setEmail(item.getEmail());
} catch (IOException ex) {
Logger.getLogger(Add_loginController.class.getName()).log(Level.SEVERE, null, ex);
}
Scenario:
scene
Popup - If we clicks on the add then we need to dismiss that dialog and change the email text on the previous scene.

As Alexander mentioned above, updating the underlying text property of the object you are using to display the email should Just Work. You need to make sure that you are working with the property (see Oracle Java FX Property Tutorial for more info). As a concrete example:
FXML
<Text fx:id="email" />
<TextField fx:id="emailInput" />
<Button onAction="#doSetEmail" text="Set Email"/>
In your controller, use the #FXML annotation to inject concrete instances of objects and set the handler to adjust the text:
Controller
#FXML
Text email;
#FXML
TextField emailInput;
#FXML
public void doSetEmail(ActionEvent ae) {
email.setText(emailInput.getText());
}
Alternatively, you could just bind the email text property to the email label property so that changes would automatically get propagated:
email.textProperty().bind(emailInput.textProperty());
You could do this in your controller initialize() method.
Now, the caveat to all this working depends on how you are handling the event and what you are doing in this. You still haven't posted the code for that as requested by the first answer, so you may be having issues there. Namely, if you are starting threads and then trying to update UI elements on the JavaFX thread from a worker thread, then you can get into trouble (potentially) with things not updating. This depends substantially on the structure of your objects, and you have not given enough information to comment in any meaningful way on that.
chooks

Everytime you have the Feeling that you manually want to update a Scene you should probably use a backgroundWorker Thread to do the work.
This way your UI Thread can use the time to update Labels etc.

JavaFX is built so that you don't need to directly call the scene update routine. All you need - update properties of scene components, and they will be updated on the nearest pulse.
So, all you need is to update properties. Or, is there any real trouble?

refreshing scene its not possible without closing...but if you can do class level declaration for control..i.e make them static its may work...
try this..
make a function in main file.
MainPanel.java
public static void SetMail(String email)
{
txtmail.setText(email);
}
LoginPanel.java
btnclear.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
MainPanel.SetMail(txtEmail.getText());
}
});

Related

Warn the user that he is about to loose his change in Edit view when leaving to view to another in GWT

I want to prevent the user that he will loose his changes in an EditView when changing the view to another.
I use MVP4G in my project and the project is divided as mvp's structure (one package for the template another one for views ..) is there any solution to detect the EditView in the eventBus. or detect the current View shown to user
Thanks in advance
Thanks to the Navigation Event feature in mvp4g, the presenter will get control before the view changes. At this point the presenter can decide if the navigation will be done or not. This is the correct place in a mvp4g application to save your data.
First zu have to mark all events in the eventbus that will change your view with:
#Event(..., navigationEvent = true)
void goToPage1();
Next your presenters have to implement the NavigationConfirmationInterface and the requires confirm-method:
public class Presenter extends ... implements NavigationConfirmationInterface {
public void confirm(NavigationEventCommand event) {
//pseudo method to verify if the view has changed
if (isViewModified(){
//Window shouldn't be used inside a presenter
//this is just to give a simple example
if (Window.confirm("Are you sure you want to leave?")){
event.fireEvent();
}
} else {
event.fireEvent();
}
}
}
And the last thing to do, is to set the presenter of the current view to the confirmation presenter by calling:
event.fireEvent(false);
This is usually done when the presenter gets control.
You will find the documentation here:
https://github.com/FrankHossfeld/mvp4g/wiki/03.-Defining-EventBus#navigation-event
Thanks to MVP4G's team including El Hoss who gives me a hint to check the MVP4G's blog.. I've solved my problem by following this example
http://mvp4g.blogspot.com/2011/06/navigation-control.html

MvvmCross: Display a BTProgressHUD spinner immediately after ViewModel creation

I'm using the BTProgressHUD Xamarin component in an app that is built using MvvmCross. I'm currently working on the iOS version of the app. My ViewModels make several web service calls and expose an 'IsBusy' property, which the associated views are binding to, in order to show or hide the progress spinner. This is pretty much the way things are set up in the N=34 MvvmCross sample (https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-34-Progress) as well.
The problem is that in some cases a ViewModel must automatically call a service as soon as it is created. I tried to make the call in the Start() function of the ViewModel, but I noticed that the BTProgressHUD spinner does not show up on top of the view. I suspect that the problem is that BTProgressHUD must be displayed only after a view has been made visible, and probably this is not the case when ViewModel.Start() runs.
Has anyone encountered this before? Is there a simple way to run code in the ViewModel after the view has been made visible?
Thanks.
Is there a simple way to run code in the ViewModel after the view has been made visible?
The N=42 video - http://slodge.blogspot.co.uk/2013/11/n42-is-my-viewmodel-visible-can-i-kill.html - introduces an IVisible interface that you can add to your ViewModel - it's your job to call this from your View - but this is easy to do on each platform. For example,, on iOS it is done using ViewDidAppear/ViewDidDisappear in https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-42-Lifecycles/Lifecycle.Touch/Views/FirstView.cs
protected IVisible VisibleViewModel
{
get { return base.ViewModel as IVisible; }
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
VisibleViewModel.IsVisible(true);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
VisibleViewModel.IsVisible(false);
}

Binding changes in model reflect in TableView only after column resizing

First example work fine, if you run the next code you will see table.
Selected row is reflected in text field.
After saving changes their immediately reflecting in tableview due model properties to table cell binding.
In the second sample
after double clicking table view is hiding and edit form opened.
But after saving changes their reflect in table view only after column resizing.
How to fix this issue?
The issue appears to be that the TableView is not updated because it is not part of the Scene graph when the property is changed. (This would be a bug.) It does work fine in JavaFX8 (though you need to modify your code to ensure that the FX Nodes are initialized on the FX Application Thread; i.e. in the initComponents() method).
For a (fairly ugly) workaround to make this work in JavaFX 2.2, try the following:
private void openTable() {
Platform.runLater(new Runnable() {
#Override
public void run() {
table.setItems(FXCollections.<Person>observableArrayList());
vbox.getChildren().setAll(label, table);
table.setItems(data);
}
});
}
(I streamlined your existing code a little; also, the code you posted doesn't need a Platform.runLater(...) here as openTable() is only invoked from the FX Application Thread. However, this may be different in your real application.)

How do I restrict keyboard focus to controls within a Node?

I'm trying to implement a reusable method for displaying a node as a lightweight dialog. The node is added to the top of a StackPane while the background is then blurred. There are 2 problems:
1) Controls in the background node of the stackpane are still able to receive focus.
2) How do I give focus to the top-level node. I know there is a requestFocus method on Node, but I need to give it to a control nested within the node. Since this is intended to be a reusable method, I can't reference the controls directly.
(if I can sidestep the whole problem by finding an existing implementation, that would be best, but I haven't found a 3rd party solution yet)
Thanks
For:
1) 3 alternative suggestions,
a- Add a key event handler to the dialogbox pane to catch the Tab key pressings
dialogbox.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(javafx.scene.input.KeyEvent event) {
if (event.getCode() == KeyCode.TAB) {
System.out.println("TAB pressed");
event.consume(); // do nothing
}
}
});
b- Temporarily disable all children of the main StackPane except the lastly added dialogbox child.
// Assuming dialogbox is at last in the children list
for(int i=0; i<mainPane.getChildren().size()-1; i++){
// Disabling will propagate to sub children recursively
mainPane.getChildren().get(i).setDisable(true);
}
c- Same as b but manually disable focusing to controls by node.setFocusTraversable(false) (spot kick). Surely this will not be your choice..
2) Tell the dialogbox which node should take the focus after the dialog is shown (through constructor or setter method).
// init dialogbox and show it then
dialogbox.setFocusTo(node_in_dialogbox);

JavaFX 2.0 memory leak on creating multiple scenes

I created a sample application that provokes a memory leak.
The problem is that I need to "reload" scenes. If I have two scenes (Bar and Foo) with one button each (ButtonBar and ButtonFoo), the buttons changes the current scene creating a new one.
If I stay clicking ButtonBar and ButtonFoo for 5 minutes (or less), the memory consumption of that simple program gets higher and higher.
Bar.java
public class Bar implements Initializable {
#FXML
private Label label;
#FXML
private void toFoo(ActionEvent event) {
try {
Button button = (Button) event.getSource();
Parent root = FXMLLoader.load(getClass().getResource("Foo.fxml"));
Stage currentStage = (Stage) button.getScene().getWindow();
currentStage.setScene(new Scene(root));
} catch (IOException ex) {
Logger.getLogger(Bar.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
The Foo.java is the same changing de fxml loading.
The fxml contains only one button:
<Button id="buttonBar" layoutX="126" layoutY="90" text="Bar!" onAction="#toFoo" fx:id="buttonBar" />
Is there a real memory-leak problem? Does anyone knows another way to do this? I want that this app stay alive and doing changes forever, like a service.
Thanks
I'm with a similar problem.
When i move the mouse on scene. The memory usage increases and never down.
I believe this is associated with mouse events.
Good Luck!
I'm not sure about the memory leaks, but for performance reasons I would avoid the repeated loading of the resource.
What I do is keep that root Parent in a variable and add it to a scene, or in my case to a specific part of another scene as needed, updating the values via a static method of the controller class.
I have several of these preloaded scenes, that update a part of a bigger scene every time the user selects certain values from a list.
I suggest you try FX8. An amount of fixes for memory leaks have been made there already.
In case you can't update to the latest version of JavaFX though, I suggest you do not use onAction="#toFoo" in FXML.
Rather, declare an event handler and call the following: buttonBar.addEventHandler(MouseEvent.MOUSE_CLICKED, declaredEventHandler);.
Release then the event handler before exiting your current screen: buttonBar.removeEventHandler(declaredEventHandler);

Resources