如何从不同的fxml更改标签文本

时间:2014-12-29 04:46:44

标签: javafx label fxml

实际上,我试图更改FXML标签上的文字

这是控制器的代码

public class ShowDeleteController implements Initializable {

@FXML
Label labelType;
@FXML
Label labelID;
@FXML
Label labelName;
@FXML
Label LabelBasedOnTypeText;
@FXML
Label LabelBasedOnType;

@Override
public void initialize(URL url, ResourceBundle rb) {
    /*Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
    labelType.setText(prefs.get("userName", labelType.getText()));*/ tried with this but don't work
}    

public void onClickCancel(ActionEvent event) {
    Node source = (Node) event.getSource();
    Stage stage = (Stage) source.getScene().getWindow();        
    stage.close();
}

public void SettingText(String TypeData, String ID, String Name, String Add, String AddText, ActionEvent event) {

    labelType.setText(TypeData);
    labelID.setText(ID);
    labelName.setText(Name);
    LabelBasedOnTypeText.setText(AddText);
    LabelBasedOnType.setText(Add);
}    

}

这是我调用fxml

的代码
 public void showDeletedData(String type, String ID, String Name, String Add, String AddText, ActionEvent event) throws Exception{
    try {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("PopUp/showDelete.fxml"));
        AnchorPane page = (AnchorPane) loader.load();
        ShowDeleteController controller = loader.getController();
        controller.SettingText(type, ID, Name, Add, AddText, event);            
        Stage PopUpStage = new Stage();
        PopUpStage.setTitle("Registro Eliminado");
        PopUpStage.initModality(Modality.APPLICATION_MODAL);
        PopUpStage.centerOnScreen();
        PopUpStage.setResizable(false);
        Scene scene = new Scene(page);
        PopUpStage.setScene(scene);

        PopUpStage.showAndWait();


    } catch (Exception ex) {
        System.out.println("Internal error: " + ex.getMessage());
    }
}

所以实际上只是想知道是否有必要初始化标签,因为我在没有在标签上设置文本的情况下尝试并且它有效,但是,当然文本只显示了defaullt。

请希望你能帮我解决这个问题,Sry因为我的英语不好

1 个答案:

答案 0 :(得分:0)

代码已经工作了

public class ShowDeleteController implements Initializable {

@FXML
Label LabelType;
@FXML
Label LabelID;
@FXML
Label LabelName;
@FXML
Label LabelBasedOnTypeText;
@FXML
Label LabelBasedOnType;

public void onClickCancel(ActionEvent event) {
    Node source = (Node) event.getSource();
    Stage stage = (Stage) source.getScene().getWindow();
    stage.close();
}

@Override
public void initialize(URL url, ResourceBundle rb) {

}

public void setTextDeleted(String Type, String ID, String Name, String BasedType, String BasedTypeText) {
    LabelType.setText(Type);
    LabelID.setText(ID);
    LabelName.setText(Name);
    LabelBasedOnTypeText.setText(BasedTypeText);
    LabelBasedOnType.setText(BasedType);
}

}

因为FXML文件以这种方式获得了标签的名称

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" minHeight="300.0" minWidth="450.0" prefHeight="300.0"     prefWidth="450.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="App.PopUp.ShowDeleteController">
  <children>
  <Label layoutX="151.0" layoutY="28.0" text="Se Eliminó un Registro"     AnchorPane.leftAnchor="151.0" AnchorPane.rightAnchor="150.0" AnchorPane.topAnchor="30.0" />
  <Label layoutX="32.0" layoutY="84.0" text="Tipo" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="100.0" />
  <Label layoutX="47.0" layoutY="138.0" text="ID" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="130.0" />
  <Label layoutX="37.0" layoutY="200.0" text="Nombre" AnchorPane.leftAnchor="30.0"        AnchorPane.topAnchor="160.0" />
  <Label fx:id="LabelBasedOnType" layoutX="37.0" layoutY="255.0" text="LabelBasedOnType" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="190.0" />
  <Button layoutX="175.0" layoutY="241.0" minWidth="100.0" mnemonicParsing="false" onAction="#onClickCancel" prefWidth="100.0" text="Aceptar" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="175.0" AnchorPane.rightAnchor="175.0" />
  <Label fx:id="LabelBasedOnTypeText" layoutX="175.0" layoutY="190.0" text="Label" />
  <Label fx:id="LabelName" layoutX="175.0" layoutY="160.0" text="Label" />
  <Label fx:id="LabelID" layoutX="175.0" layoutY="130.0" text="Label" />
  <Label fx:id="LabelType" layoutX="175.0" layoutY="100.0" text="Label" />
  </children>
  </AnchorPane>

现在它起作用=)