更改/更新另一个类中的FXML组件 [英] Change/Update FXML components in another class

查看:108
本文介绍了更改/更新另一个类中的FXML组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我想从.fxml中更改某些内容,但是无论我做什么,都没有任何变化.这只是一个简单的例子.

My problem is: I want to change something from the .fxml but whatever I do, nothing changes. This is just a simply example.

我走遍了整个互联网,但是没有一个解决方案对我有用. 在这里,我想通过从主类调用相应的方法来更改标签的文本.

I went trough the whole internet but none of the solutions worked for me. Here I want to change the text of the label, by calling the corresponding method from the main class.

在单击按钮时调用相同的方法(此处为setLabel()),并且在控制器类中具有事件处理程序,一切正常,但是当我尝试从另一个类中修改某些内容时,一切正常.

Calling the same method (here setLabel()) when clicking a Button, with an event handler in the controller class, everything works fine, but a soon as I try to modify something from another class nothing works.

主类:

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

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
Controller controller = new Controller();
Platform.runLater(()->controller.setLabel());

}

FXML代码:

FXML Code:

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <center>
      <Label fx:id="label" text="This" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

控制器类: 包装样品;

Controller class: package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class Controller {
@FXML
private Label label=new Label();

public void setLabel(){
    label.setText("Test");
}
}

推荐答案

这很简单...您可以在Main类中更改标签文本,方法是在Controller类中为所需标签添加getter方法,然后然后使用控制器对象(loader.getController())在Main类中获取它并更新其文本.或使用Main中的controller对象在Controller类内调用setter方法. 正如DVagra所说,使用loader.getController()获取控制器对象. (其中loader是FXMLoader的对象).

That is pretty simple... You can change the the label text from your Main class either by adding a getter method in your Controller class for the required label and then get it in the Main class using the controller object (loader.getController()) and update its text. Or call the setter method inside the Controller class using controller object in Main. As DVagra said, use loader.getController() to get the controller object. (where loader is the object of FXMLoader).

此外,您不需要Platform.runLater()来更新gui控件.因为您已经在FX线程上运行.

Moreover you do not need Platform.runLater() to update the gui controls. As you're already running on the FX thread.

无论如何,这就是您所需要的.

Anyway, here is what you need.

主班

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

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(Main.class.getResource("sample.fxml"));
    Parent root = loader.load();
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();

    Controller controller = loader.getController();

    controller.setLabel("Test");
//  Or
//  controller.getLabel().setText("Test");
 }
public static void main(String[] args) {
    launch(args);
 }
}

控制器类

package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable{
@FXML
Label label;

//setter
public void setLabel(String labelText){
    label.setText(labelText);
}

//getter for label
public Label getLabel() {
    return label;
}

@Override
public void initialize(URL location, ResourceBundle resources) {
 }
}

Sample.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.60" 
xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
    <Label fx:id="label" text="This" BorderPane.alignment="CENTER" />
</center>
</BorderPane>

这篇关于更改/更新另一个类中的FXML组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆