ProgressIndicator finishes when I start another Stage - javafx

I want to create an application starter. The ProgressIndicator should running during start of the new Stage in new Thread. But if I click at the start button the ProgressIndicator stops running.
It works if I make some I/O and ProgressIndicator shows progress. It is apparently not possible to update two stages in JavaFX in parallel or does anyone have a solution for me?
public class Main extends Application {
public void start(Stage primaryStage) {
primaryStage.setScene(new Scene(new UserVC().getView(), 600, 200));
primaryStage.show();
userVC.autologin();
}
public static void main(String[] args) { launch(args); }
}
public class UserView extends BorderPane {
private ProgressIndicator progressIndicator = new ProgressIndicator();
public UserView() {
super.setCenter(createContent());
}
public Node createContent() {
HBox userbox = new HBox();
userbox.getChildren().add(progressIndicator);
progressIndicator.show();
return userbox;
}
}
public class UserVC {
private UserView view = new UserView();
public UserVC() {
}
public void autologin() {
Task<Void> task = new Task<Void>() {
#Override public Void call() throws InterruptedException {
try {
Thread.sleep(1000);
Platform.runLater(new Runnable() {
#Override public void run() {
Stage stage = new Stage();
stage.setScene(new Scene(new MainControlVC().getView(), 900, 300));
stage.show();
}
});
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
};
view.getProgressIndicator().progressProperty().unbind();
view.getProgressIndicator().progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
}

I changed the Task to Thread and it works fine now:
Thread thread = new Thread() {
public void run() {
mainController = new MainControlVC(dataDirectory, user);
Platform.runLater(new Runnable() {
#Override public void run() {
Stage stage = new Stage();
Scene scene = new Scene(mainController.getView(), mainController.getWidth(), mainController.getHeight());
stage.setScene(scene);
stage.show();
}
});
}
};
thread.start();

Related

How to call the public void start(Stage primaryStage) Method from a different class

I'am new to JavaFX, so my coding is not the best. My programme has one Stage with 2 different Scenes. For a better overview I have created a new class for the second Scene. From this second Scene I want to go back to first one, using a button. The method: primaryStage.setScene(scene) is not working and I dont know why. Can someone please help me? :)
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
GridPane primarygridpane = new GridPane();
Scene scene = new Scene(primarygridpane,400,400);
...
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
public class Harnwegsinfektion {
public static void create (Stage primaryStage){
GridPane secondarygridpane = new GridPane();
Scene scene2 = new Scene(secondarygridpane,400,400);
buttonBack.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(scene);
}
});
primaryStage.setScene(scene2);
primaryStage.show();
}
}
The problem is that your scene variable is named scene2 and you are calling setScene() with scene.
I don't really know when and where create() is called, but if you want to go back to the first scene from the button, you have to save the original one before switching :
public static void create (Stage primaryStage){
GridPane secondarygridpane = new GridPane();
Scene scene2 = new Scene(secondarygridpane,400,400);
Scene originalScene=primaryStage.getScene(); //save the previous scene
buttonBack.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(originalScene); //go back to the previous scene
}
});
primaryStage.setScene(scene2);
primaryStage.show();
}

Can we change the scene in Java Scene Builder 2.0 without using button after 5 seconds

Basically i am using JavaFX scene buidler 2.0 i want to change the scene from one to other without using any button for them.
Main File
public class OurFirstProject extends Application {
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.setFullScreenExitHint("");
//stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
public static void main(String[] args) {
Application.launch(args);
}
}
public class FXMLDocumentController implements Initializable {
#FXML
private void change(ActionEvent event) throws IOException {
Parent sceneChange = FXMLLoader.load(getClass().getResource("Change.fxml"));
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene changeScene = new Scene(sceneChange, screenBounds.getWidth(), screenBounds.getHeight());
Stage Window = (Stage) ((Node) event.getSource()).getScene().getWindow();
Window.setScene(changeScene);
Window.setFullScreen(true);
Window.show();
}
int a = 0;
#FXML
public Button helloButton;
#FXML
private Label ourLabel;
#FXML
private void printHello(ActionEvent e) {
a++;
if (a % 2 == 0) {
ourLabel.setText("Hello World! Kyun" + a);
} else {
ourLabel.setText("Hello Dunia" + a);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
enter code here
}
The Scene Mentioned in the FXML File Name"change", i want to Run this Scen without Using Button I wanna run this on the delay of five second of FIrst scene.
You can use a Timer() like,
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Platform.runLater(new Runnable() {
#Override
public void run() {// After this you can add your change.fxml load code
Parent root = null;
try {
root = fxmlLoader.load();
}catch(Exception e)
{
//Exception catch code here
}
primaryStage.show();//Here you can write your show code like window.show()
}
});
}
},5000);// 5000- time delay in milliseconds

JavaFX setIconified(false) does not display window

I have a situation in which I have a JavaFX application that allows the primary stage to be iconified by a set of REST endpoints. Here's the application code:
public class server extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/Hello.fxml"));
Scene scene = new Scene(root, 300, 275);
primaryStage.setTitle("FXML Welcome");
primaryStage.setScene(scene);
Server jettyServer = new Server(65534);
ServletContextHandler context = new ServletContextHandler(jettyServer, "/");
jettyServer.setHandler(context);
final ResourceConfig rc = new ResourceConfig().register(
new AbstractBinder() {
#Override
protected void configure() {
bind(primaryStage).to(Stage.class);
}
}
);
rc.packages("com.cognivue.jettypoc.rest");
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(rc));
jerseyServlet.setInitOrder(0);
context.addServlet(jerseyServlet, "/*");
jettyServer.start();
primaryStage.show();
}
}
Here's the REST controller code:
#Path("stagecontrol")
public class StageControl {
#Inject
private Stage primaryStage;
public StageControl() {
}
#GET
#Path("start")
public Response start() {
Platform.runLater(() ->primaryStage.setIconified(false));
return Response.ok().build();
}
#GET
#Path("stop")
public Response stop() {
Platform.runLater(() ->primaryStage.setIconified(true));
return Response.ok().build();
}
}
When I make a call to /stagecontrol/stop, the application iconifies; when I make a call to /stagecontrol/start, nothing happens. Is there something about setIconified?

Javafx web browser

I am trying to add a history , backward and forward buttons that actually do there jobs , so the history opens a drop down menu that shows the links the user went to and provided with a clear button to delete them if wanted and the backward to move back and the forward to go the the page the user was on.
public Test4(){
initComponents();
}
public static void main(String ...args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.getContentPane().add(new Test4());
frame.setMinimumSize(new Dimension(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
private void initComponents(){
jfxPanel = new JFXPanel();
createScene();
setLayout(new BorderLayout());
add(jfxPanel, BorderLayout.CENTER);
swingButton = new JButton();
swingButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {
public void run() {
webEngine.reload();
}
});
}
});
swingButton.setText("Reload");
add(swingButton, BorderLayout.SOUTH);
}
private void createScene() {
PlatformImpl.startup(new Runnable() {
public void run() {
stage = new Stage();
stage.setTitle("Hello Java FX");
stage.setResizable(true);
Group root = new Group();
Scene scene = new Scene(root,80,10);
stage.setScene(scene);
browser = new WebView();
webEngine = browser.getEngine();
webEngine.load("http://www.google.com");
ObservableList<Node> children = root.getChildren();
children.add(browser);
jfxPanel.setScene(scene);
}
});
}
}

JavaFX stage.setOnCloseRequest without function?

This is my Start-Methode. First i create a stage and set a title and a scene. And i wanna create a dialog if someone wants to close the window on the window-close-btn [X]. i thought i will catch this event with the setOnCloseRequest()-function. But i still can close all stages i open while runtime.
#Override
public void start(final Stage primaryStage) throws Exception {
primaryStage.setTitle("NetControl");
primaryStage.setScene(
createScene(loadMainPane())
);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(final WindowEvent event) {
//Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
// Frage - Label
Label label = new Label("Do you really want to quit?");
// Antwort-Button JA
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
dialog.close();
}
});
// Antwort-Button NEIN
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.show();
dialog.close();
}
});
}
});
primaryStage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(
getClass().getResourceAsStream(ContentManager.DEFAULT_SCREEN_FXML)
);
MainController mainController = loader.getController();
ContentManager.setCurrentController(mainController);
ContentManager.loadContent(ContentManager.START_SCREEN_FXML);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
setUserAgentStylesheet(STYLESHEET_MODENA);
return scene;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
Are there any other functions to catch the window-events?
or isnt it logical to run the CloseRequest on the primaryStage, i read something with platforms (but i dont know if it necessary for my problem)?
In the onCloseRequest handler, call event.consume();.
That will prevent the primary stage from closing.
Remove the primaryStage.show(); call from the cancel button's handler and add a call to primaryStage.hide(); in the OK button's handler.

Resources