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));
}
Related
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 create a button and I want to set up its width and height through css. I tried this but it didn't work.
Other properties( such as font-size and text fill) are fine and work correctly. But the width of Button seems to be fixed and doesn't change.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
VBox root = new VBox();
Button button = new Button("Button");
root.getChildren().add(button);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
button.setId("button");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And in "Style.css":
#button{
-fx-width: 300;
-fx-font-size:20;
-fx-text-fill: yellow;
}
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();
}
}
I want to change scenes on my stage or close stage when I push the startgame button and exitgame button, but its just show me first scene, and when I'm trying to push one of this buttons compiler close in case of NullPointerException.
I also have main class and two fxml files, but I think it's don't necessary to put it here, it contains just two anchor panes, primitive labels and buttons.
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable{
private Stage stage = new Stage();
private FXMLLoader loader = new FXMLLoader();
private AnchorPane anchorPane = new AnchorPane();
private Scene scene = new Scene(new AnchorPane());
#FXML private Button startGameButton;
#FXML private Button endGameButton;
#FXML private Button buttonAnswer1;
#FXML private Button buttonAnswer2;
#FXML private Button buttonAnswer3;
#FXML private Button buttonAnswer4;
public void createScene(int typeOfScene) throws Exception
{
if (typeOfScene == 1)
{
loader = new FXMLLoader(getClass().getResource("/sample/sample.fxml"));
anchorPane = loader.load();
scene = new Scene(anchorPane);
stage.setScene(scene);
stage.show();
}
if (typeOfScene == 2)
{
scene = null;
loader = new FXMLLoader(getClass().getResource("/sample/episodesFXML.fxml"));
anchorPane = loader.load();
scene = new Scene(anchorPane);
stage.setScene(scene);
stage.show();
}
}
public void getPrimaryStage(Stage stage) throws Exception
{
this.stage = stage;
createScene(1);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
startGameButton.setOnAction(event -> {
try {
createScene(2);
} catch (Exception e)
{
e.printStackTrace();
}
});
endGameButton.setOnAction(event -> {
stage.close();
});
}
}
I remember a problem like that, fxml file is linked to a controller file by the fx:controller attribute. You try to load a fxml in a controller that it's not handled/loaded. Create another form and showing/hiding forms seems simpler.
I have a TextField in my program that will have data entered by the user, but I also have a variable value somewhere else that I need to permanently display at the end of my TextField. It cannot disappear when the user enters any data in the TextField. Can anyone give me a good implementation? Thanks.
[UserInput (miles)]
**Above is an example of what I am talking about. "Miles" needs to always be in the TextField while the UserInput is changing.
EDIT: "Implementation" was a bad choice of words. Let me rephrase, I can set up the field myself, but I am having trouble finding a way to set permanent text in a textfield. Just wondering if anyone knows an easy way.
You could put a transparent textfield over a label and bind the 2 together. Something like this but with better styling.
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Text extends Application {
#Override
public void start(Stage primaryStage) {
TextField txtUser = new TextField();
txtUser.setStyle("-fx-background-color: transparent;-fx-border-color:blue;");
Label txtBG = new Label(" (miles)");
Label labelUser = new Label();
labelUser.textProperty().bind(txtUser.textProperty());
Label labelAll = new Label();
labelAll.textProperty().bind(Bindings.concat(
labelUser.textProperty())
.concat(txtBG.textProperty()));
StackPane sp = new StackPane();
sp.getChildren().addAll(txtBG, txtUser);
sp.setPrefSize(100, 12);
VBox root = new VBox();
root.getChildren().addAll(sp,labelUser,labelAll);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("transparent text test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
I would use a HBox instead of a stack pane but it's one way to satisfy the requirement that "miles" is 'inside' the texfield's borders.
This is a small example doing what you want ! I have used the focus property of textfield to add and remove miles from it !
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextBinding extends Application {
#Override
public void start(Stage primaryStage) {
final TextField user = new TextField();
TextField demo = new TextField();
user.setStyle("-fx-background-color: transparent;-fx-border-color:blue;");
user.focusedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
user.setText(user.getText().replace(" miles", ""));
}
else
{
user.setText(user.getText().concat(" miles"));
}
}
});
VBox root = new VBox();
root.getChildren().addAll(user,demo);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("transparent text test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
launch(args);
}
}