Edit FXML Button text

Asked

Viewed 469 times

1

I cannot change the text of the button created in FXML. Even if I put inside the initialize(... butao.setText("..."); he doesn’t change.

What I do?


Personcontroller, intializable:

@Override
public void initialize(URL arg0, ResourceBundle arg1){...

    adicionareditarnovadivida.getItems().addAll("Adicionar","Editar","Nova Divida","Imprimir Carnê");
    adicionareditarnovadivida.getSelectionModel().select(0);
...}

Personcontroller, Combobox Method:

@FXML
private void adednd(ActionEvent event){
    String saf = adicionareditarnovadivida.getSelectionModel().selectedItemProperty().get();
    System.out.println(saf);
    switch(saf){
    case"Adicionar":
        adicionar.setText("Adicionar");
        imprime.setDisable(true);
        clientes.setDisable(true);
        rg.setDisable(false);
        cpf.setDisable(false);
        endereco.setDisable(false);
        break;
    case "Editar":
        adicionar.setText("Editar");
        imprime.setDisable(true);
        clientes.setDisable(false);
        rg.setDisable(true);
        cpf.setDisable(true);
        break;
    case "Nova Divida":
        adicionar.setText("Nova Divida");
        imprime.setDisable(true);
        clientes.setDisable(false);
        rg.setDisable(true);
        cpf.setDisable(true);
        endereco.setDisable(true);
        break;
    case "Imprimir Carnê":
        imprime.setDisable(false);
        clientes.setDisable(false);
        break;
    default:
        SI.AlertDialog(stage, "Erro Desconhecido.", AlertType.ERROR);
    }
}

Personcontroller, Button Method:

 @FXML  
 private void Adicionar(ActionEvent event) { ...
                switch(adicionar.getText()){
                    case "Adicionar":
                        TextFieldMascaras.adicionar(nome, divida, pagamento,compradata, vencimento, cpf, rg,telefone, endereco, produtos,stage,carne.getText());
                        break;
                    case "Editar":
                        TextFieldMascaras.editar(nome, divida, pagamento, compradata, vencimento, carne.getText(), stage,produtos);
                        break;
                    case "Nova Divida":
                        TextFieldMascaras.editar(nome, divida, pagamento, compradata, vencimento, carne.getText(), stage, produtos);
                        break;
                    case "Imprimir Carnê":
                        TextFieldMascaras.Imprimir(nome, divida, pagamento, compradata, vencimento, carne.getText(), stage, produtos);
                        break;
                    default:
                        SI.AlertDialog(stage, "Erro Deconhecido!!", AlertType.ERROR);
                    }
    ...}

What is wrong?


Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1828305.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/C:/lucas/site/Sistema%20Rose%20Banco%20de%20Dados/bin/pc/Main_m1.fxml:7

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at pc.MainApp.start(MainApp.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$54/8684414.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/18503843.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/12676431.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/2180324.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/3326003.run(Unknown Source)
    ... 1 more
Caused by: java.lang.ClassCastException: javafx.scene.layout.Pane cannot be cast to javafx.scene.layout.AnchorPane
    at pc.PersonController.<init>(PersonController.java:91)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
    ... 23 more
Exception running application pc.MainApp

Knob:

    private Button adicionar = (Button) getFXMLField((AnchorPane)pane, "Adicionar");

And the Mainapp:

import java.net.URL;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

     @Override
     public void start(Stage palco) throws Exception {
         URL arquivoFXML = getClass().getResource("Main_m1.fxml");
         Parent fxmlParent = (Parent) FXMLLoader.load(arquivoFXML);
         palco.setScene(new Scene(fxmlParent));
         palco.setTitle("Contas");
         palco.show();
     }

     public static void main(String[] args) {
         launch(); 
     }

}
  • 1

    How so boot? In a constructor? Put the full code and/or detail better what is your question.

  • I made an example to not have to put too much code here, but the example worked and now I’m seeing what’s wrong. If you can’t find it, I’ll put the code.

  • I posted the code every time the button pops up, and I’m sorry I didn’t mention Combobox.

1 answer

2


I had this same problem to load a properties file when opening the screen, I don’t know if it is the best way to solve, but follow the code:

private Node getFXMLField(AnchorPane ap, String fxid) {
    ObservableList<Node> fxmlFields = ap.getChildrenUnmodifiable();
    for (Node field : fxmlFields) {
        if(field.getId().equals(fxid)){
            return field;
        }
    }

    throw new IllegalStateException("Campo " + fxid + "não existente");
}

With Node you can cast for any javafx component and then set the values you want.

An example:

Button button1 = (Button) getFXMLField(formPai, "nome do botão");
button1.setText("xanana");
  • How do I use this, can you explain? Thank you for Reply.

  • I added an example of usage to the answer, if you have any further questions you can ask

  • Gave a new error. I edited the question with the new error.

  • I switched from Anchorpane to Pane and the other error: Caused by: java.lang.IllegalStateException: Campo Adicionarnão existente

  • In FXML you set the fxid property of the button to Add?

  • How do you do it? I didn’t use FXML before, so it’s hard.

  • In the Voce FXML file you have to add the example fx.id property: <Button fx:id="Add" .../>

  • I’m using Scene Builder and java made it fx:id="Add"

  • You have how to post the code where you load the FXML?

  • Make sure you are switching to the getFXMLField method the pane that contains the button. Also check the fx.id case.

  • fx.id is right, but I don’t know if the crash contains the Button, but if I try to add it from the same error. I don’t know what’s going on but the other actions that the switch of ComboBox( that are some setDisable(false) in some Textfield fields) work and even another button changes, but this one does not.

  • Error when using setText?

  • No he just does nothing.

  • I managed to find the error, it is a mistake so beginner that I did not realize, the fx.id="Adicionar" with a capital "A" and on the button was @FXML private Button adicionar, I don’t know how I didn’t realize it, but thanks for responding.

Show 9 more comments

Browser other questions tagged

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