'JavaFX label.getText() returns nothing in another class
I need that label values (num, cost) to be displayed in Bank.java from Work-window.java
num = random int value
cost = int value
Code Work-window.java
package s.lab9;
import java.io.IOException;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
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.stage.Stage;
public class WorkWindow {
@FXML
private Button enter;
@FXML
private Button payment;
public int a;
@FXML
private TextField ticket;
private ActionEvent event;
private Stage stage;
private Scene scene;
private Parent root;
@FXML
public Label num;
@FXML
public Label cost;
public WorkWindow(){ }
public void click(ActionEvent event) throws IOException {
String b= (ticket.getText());
Random rnd= new Random();
if (b.equals(String.valueOf(1))){
a= rnd.nextInt(1000);
num.setText(String.valueOf(a));
cost.setText(String.valueOf(500));
}
if (b.equals(String.valueOf(2))){
a= rnd.nextInt(1000);
num.setText(String.valueOf(a));
cost.setText(String.valueOf(10000));
}
if (b.equals(String.valueOf(3))){
a= rnd.nextInt(1000);
num.setText(String.valueOf(a));
cost.setText(String.valueOf(10));
}
}
public void switchtoPay(ActionEvent event) throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("Bank.fxml"));
root = loader.load();
Bank bank = loader.getController();
bank.display2(num.getText(), cost.getText());
payment.getScene().getWindow().hide();
root = FXMLLoader.load(getClass().getResource("Payment.fxml"));
Stage stage = new Stage();
stage.setTitle("Payment");
stage.setScene(new Scene(root));
stage.show();
}
}
Code Bank.java
package s.lab9;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Bank {
@FXML
private Label cart;
@FXML
public Label ticket;
@FXML
public Label cost1;
public void display(String numcart){
cart.setText(numcart); }
public void display2(String num, String cost){
ticket.setText(num);
cost1.setText(cost);
} }
Btw method display1 works correct, but display 2 isn't, Idk where i did mistake. fx:id:controllers assigned correctly, there are have prefWidth, so they cannot be "empty"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
