Swap JavaFX scene when clicking a button - java

I am trying to create a program to teach people about GNU/Linux and the command line, I have my main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
primaryStage.setTitle("Learnix");
primaryStage.setScene(new Scene(root, 800, 500));
primaryStage.show();
}
}
And the controller to go with it.
package sample;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import java.io.IOException;
public class loginController {
public Button loginBtn;
public void loginBtnClick() throws IOException {
System.out.println("You are logged in");
}
}
I have tried things such as:
FXMLLoader.load(getClass().getResource("lessons.fxml"));
But I can't figure out how to get it to swap scenes. I have seen many tutorials on YouTube and it Stack Overflow but many of them have all of the JavaFX on the main.java and not in separate files as I am using scenebuilder.
Thank you.

You can either call Stage.setScene() to change the whole scene or just substitute a root to the new one by Scene.setRoot():
Parent newRoot = FXMLLoader.load(getClass().getResource("lessons.fxml"));
primaryStage.getScene().setRoot(newRoot);

Related

Java FX 'cannot find symbol'

I'm new to java and java fx, I am trying to create a simple window but I keep getting the error
java: cannot find symbol
symbol: class Group
location: class sample.Main
This is my code:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
Group root = new Group();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
You aren't importing javafx.scene.Group. Add
import javafx.scene.Group;

can't open java exported jar file

I'm trying to open my jar file which I exported from IntelliJ-IDEA but the jar file unfortunately doesn't open and no error happens, although the app is running on IntelliJ, I dont know if the problem is being from the jar itself or from the code ! here is the main screen code:
package controllers;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("../views/main_screen.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root);
primaryStage.setTitle("Salesman Manager");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX; Eclipse points error at main method

I was trying to learn JavaFX and used the code on this video: https://www.youtube.com/watch?v=FLkOX4Eez6o.
I have Google it but could not find a solution.
Then Eclipse IDE shows this error:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Testes extends Application{
Button botao;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Controle de processos");
botao = new Button();
botao.setText("+");
StackPane layout = new StackPane();
layout.getChildren().add(botao);
Scene cena = new Scene(layout, 300, 250);
primaryStage.setScene(cena);
primaryStage.show();
}
}

Full-screen Overlay

Currently, I am trying to create a timer that displays in the corner of your screen. I want it to show even if there is currently a full-screened application running. Currently I've tried Stage#setAlwaysOnTop(true), however that only functions for normal applications, not when they are full-screened.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
stage.setTitle("Timer");
stage.setScene(new Scene(root, 300, 275));
stage.setAlwaysOnTop(true);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the standard JavaFX example with IntelliJ, however with the one modification which was Stage#setAlwaysOnTop(true). How would I get this to function ontop of the full-screen, or at least stay omnipresent no matter the application.

Javafx Button With EventHandler. Getting cannot find symbol error

I am trying to get a button to print out Java is fun after being clicked and I keep getting a Cannot find symbol error when I run it. I'm not sure whether it is my import statements or an actual error in the code itself. I was wondering if someone could help me figure out why I'm getting the error?
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class ButtonHandler extends Application {
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Pane pane = new Pane();
Button btOK = new Button("OK");
pane.getChildren().add(btOK);
btOK.setOnAction((ActionEvent e) -> {
System.out.println("Java is Fun");
});
// Create a scene and place it in the stage
Scene scene = new Scene(Pane);
primaryStage.setTitle("Button Demo"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}

Categories