Fill label automatically when Stage opens - Javafx

Asked

Viewed 388 times

0

I need a label receive a text automatically as soon as the stage started without having to click a button. As different texts are saved in a database, I need them to be loaded in the label when the stage initiate.

Is there any method to it?

This is the application’s controller class:

public class DesafioController {
    @FXML Button btnCancelar = new Button();
    @FXML TextArea AreaSolucao = new TextArea();
    @FXML Label labelDesafio = new Label();
    @FXML AnchorPane pane = new AnchorPane();

    @FXML
    void clickRecomecar() {
        //Reinicia o desafio do zero, apagando o que já foi escrito.
        AreaSolucao.setText("TESTE RECOMEÇAR");
    }

    @FXML
    void clickCancelar() {
        //Fecha o desafio voltando para o jogo.
        Stage atual = (Stage) btnCancelar.getScene().getWindow();
        atual.close();
    }

    @FXML
    void clickEnviar() {
        //Aqui será enviado a solução do desafio para o banco de dados.(INSERT)
    }
}

Application window:

inserir a descrição da imagem aqui

  • There is, but edit your question and include the code of what you have so far.

  • @Renan, includes the code and a print of the application window.

1 answer

0


You can override the behavior of the method initialize(), which briefly is the method called when all controls noted by @FXML are injected. For this, your control class must implement Initializable:

public class DesafioController implements Initializable {

   private @FXML Label label;

   @Override
   public void initialize(URL url, Resources res)
      label.setText("Texto Inicial.");
   }
}
  • 1

    It worked!! Thank you Renan!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.