Edit Button FXML Text

1

I can not change the text of the button created in FXML. Even though I put it inside initialize(... butao.setText("..."); it does not change.

What do 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's 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

Button:

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

And 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(); 
     }

}
    
asked by anonymous 17.06.2015 / 20:05

1 answer

2

I had this same problem to load a properties file when opening the screen, I do not know if it is the best way to solve it, but it follows 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 to any javafx component and then set whatever values you want.

An example:

Button button1 = (Button) getFXMLField(formPai, "nome do botão");
button1.setText("xanana");
    
21.06.2015 / 18:06