Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

375
Views
¿Cómo puedo hacer que JavaFX se reinicie cuando se cambia la escena?

Quiero hacer un programa muy simple en JavaFX. Dice así:

El usuario ingresa algo en un TextField

El programa muestra la entrada en una etiqueta pero en una Escena diferente


Aquí está mi código:

Controlador.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); } }

Principal.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); } }

Hay otros 2 archivos FXML que contienen un solo campo de texto o una sola etiqueta.

Pero cada vez que ejecuto este código hay una NullPointerException que indica que la etiqueta es nula porque no se ha inicializado. ¿Cómo puedo solucionar esto?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Creo que deberías hacer otro controlador para el archivo display.fxml (la otra escena). Que en este nuevo controlador puede preparar una función para establecer el valor de la etiqueta:

DisplayController.java

 public class DisplayController { //label in display.fxml @FXML Label label; public void setLabelText(String s){ label.setText(s); } }

y en Controller.java edite la función enter() llamando a una nueva instancia de DisplayController.java :

 @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)); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!