Hello I am currently working on a PIN Generator for my work and as I am completely new to Java I am having a few difficulties, especially when using JavaFX.
I want to make the programm show another .fxml file when clicking on one of each buttons.
Here is my code:
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 {
public static void main(String[] args) {
Application.launch(main.class, args);
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
I have created a controller Class for each button on the scene.
Controller Class Code:
package view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class MainController {
#FXML
private Label dpmadirektpropingenerator;
#FXML
private Button unlockPinButton;
#FXML
private Button confirmationPinButton;
}
This code should works.
Modify your main class like this:
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 {
public static void main(String[] args) {
Application.launch(main.class, args);
}
static Stage stg;
#Override
public void start(Stage stage) throws Exception {
this.stg = stage;
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
and here is your pressButton function:
public void pressButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sedondFXML.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
main.stg.close();
} catch(Exception e) {
e.printStackTrace();
}
}
Related
I want to make a very simple program in JavaFX. It goes like this:
The user inputs something into a TextField
The program displays the input on a label but on a different Scene
Here is my code:
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javax.xml.soap.Text;
import java.io.IOException;
public class Controller {
//textField in sample.fxml
#FXML
TextField textField;
//label in display.fxml
#FXML
Label label;
String text;
#FXML
public void enter() throws IOException {
text = textField.getText();
Stage stage = (Stage) (textField).getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("display.fxml"));
stage.setResizable(false);
stage.setTitle("Display");
stage.setScene(new Scene(root, 300, 275));
label.setText(text);
}
}
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 {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
There are 2 other FXML files containing either a single textField or a single Label.
But whenever I run this code there is a NullPointerException signaling that label is null, because it hasn't been initialized. How do I fix this?
I believe you should make antoher controller for the display.fxml file (the other scene). Than in this new controller you can prepare a function to set label value:
DisplayController.java
public class DisplayController {
//label in display.fxml
#FXML
Label label;
public void setLabelText(String s){
label.setText(s);
}
}
and in Controller.java edit enter() function by calling a new DisplayController.java instance:
#FXML
public void enter() throws IOException {
text = textField.getText();
Stage stage = (Stage) (textField).getScene().getWindow();
FXMLLoader loader = new FXMLLoader.load(getClass().getResource("display.fxml"));
Parent root = loader.load();
DisplayController dc = loader.getController();
//here call function to set label text
dc.setLabelText(text);
stage.setResizable(false);
stage.setTitle("Display");
stage.setScene(new Scene(root, 300, 275));
}
I have 2 scenes. One of the scenes is Login and the other is Register. When I click the register button on the Log in screen it switches me to the register scene. The actions I have made in the controller for the button is not working. Do I need to do something in the main or controller to get the Register.fxml to work?
ModelController:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.sql.*;
public class ModelController extends Main{
#FXML
private Label LblStatus;
#FXML
private TextField UserEmail;
#FXML
private TextField UserPassword;
#FXML
private TextField RegisterEmail;
#FXML
private TextField RegisterPassword;
#FXML
private TextField RegisterFirst;
#FXML
private TextField RegisterLast;
#FXML
private TextField RegisterSSN;
#FXML
private TextField RegisterAddress;
#FXML
private TextField RegisterCity;
#FXML
private TextField RegisterState;
#FXML
private TextField RegisterZip;
#FXML
private Button LoginRegister;
public void Login(ActionEvent event) throws Exception {
if(UserEmail.getText().equals("User") && UserPassword.getText().equals("pass")) {
LblStatus.setText("Login Success");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
else {
LblStatus.setText("Login Failed");
}
}
public void handleButtonAction(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Register.FXML"));
Parent root1 = (Parent) fxmlLoader.load();
Scene Register = new Scene(root1);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(Register);
window.show();
}
catch(Exception e) {
}
}
public void CompleteRegistration(ActionEvent event) {
}
Main:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
The 2nd scene was not pulling in the controller.
I'm trying to write the simplest code to start learning how JavaFX works. Here is my code:
package app;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class mainClass extends Application{
Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
StackPane layout = new StackPane();
Scene scene = new Scene(layout, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
It should just show an empty window. My compilator gives no errors, but nothing shows up, even if I can see my program running in my dock. How is this possible? I'm sure I'm missing some stupid simple thing, but I cannot figure out what it is.
I am just starting to learn javafx.I am trying to make a simple application where you enter into a textfield and simultaniously give it out via the label.
Heres the main class
package testing;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class testing extends Application {
private Stage primaryStage;
private BorderPane RootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("First Application");
initRootLayout();
initProjectTest();
}
public static void main(String[] args) {
launch(args);
}
public Stage getPrimaryStage() {
return primaryStage;
}
public void initRootLayout() {
try{ FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/RootLayout.fxml"));
RootLayout=(BorderPane)loader.load();
primaryStage.setScene(new Scene(RootLayout));
primaryStage.show();
}
catch(IOException e) {
e.printStackTrace();
}
}
public void initProjectTest() {
try {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/ProjectTest.fxml"));
AnchorPane ProjectTest=(AnchorPane)loader.load();
RootLayout.setCenter(ProjectTest);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Here is the controller class
package testing.view;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class ProjectTestController {
#FXML
private Label label1=new Label();
#FXML
private TextField textfield1=new TextField();
label1.textProperty().bind(textfield1.textProperty());
}
If I try to implement it via a button where the text is updated everytime I click the button(via a onAction function) the code works fine. However I get syntax error when I try to bind. I tried to search for answers but none of were having syntax errors.
Say You have a class with the JavaFX start().
This class is launched by another class, with main. I want to get information from the class with main, to the class with JavaFX. For some reason, a constructor won't work.
Here's the code that does the webview:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class htmlRender extends Application {
#Override
public void start(Stage stage) {
stage.setTitle("HTML");
stage.setWidth(500);
stage.setHeight(500);
Scene scene = new Scene(new Group());
VBox root = new VBox();
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
Hyperlink hpl = new Hyperlink("LINK");
hpl.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
webEngine.load("LINK");
}
});
root.getChildren().addAll(hpl, browser);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
}
Where it says "LINK", I want to insert a string from another class.
The other class has
public static void main(String[]args){
htmlRender.launch(htmlRender.class, args);
}
JavaFX application launch parameters can be accessed via the Application.getParameters() method.