如何从常规类访问控制器类的字段?

我正在使用 JavaFX 通过 Gluon 框架创建 Android/Iphone 应用程序。


我知道这种类型的“我怎样才能获得控制器类”的问题。但这是不同的。


我知道如何获得控制器类,但这不是我要求的。我问如何在不创建任何新对象的情况下从控制器类访问字段。


假设我们有一个这样的 JavaFX 控制器类:


public class PrimaryPresenter {


    @FXML

    private View primary;


    @FXML

    public LineChart<String, Number> lineChart; // Every time we update the socket, we update the chart too


    public void initialize() {


        primary.setShowTransitionFactory(BounceInRightTransition::new);

        primary.showingProperty().addListener((obs, oldValue, newValue) -> {

            if (newValue) {

                AppBar appBar = MobileApplication.getInstance().getAppBar();

                appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> 

                        MobileApplication.getInstance().getDrawer().open()));

                appBar.setTitleText("Plot");


            }

        });   


        /*

         * Initial stuffs for the lineChart

         */

        lineChart.setTitle("Adaptive MPC");

    }



}

我想lineChart从另一个类访问对象而不创建新的PrimaryPresenter控制器类。FXML 文件由 Scene Builder 创建。


我问这个问题的原因是因为我有另一个通过线程循环的类,并且该线程将lineChart实时更新对象。


跃然一笑
浏览 129回答 1
1回答

SMILET

这是我的答案。在每个 Gluon 项目中,都有一个名为的类GluonApplication,它看起来像这样:package com.gluonapplication;import com.gluonapplication.thread.SocketConnection;import com.gluonapplication.views.PrimaryView;import com.gluonapplication.views.SecondaryView;import com.gluonhq.charm.glisten.application.MobileApplication;import com.gluonhq.charm.glisten.visual.Swatch;import javafx.scene.Scene;import javafx.scene.image.Image;import javafx.stage.Stage;public class GluonApplication extends MobileApplication {&nbsp; &nbsp; public static final String PRIMARY_VIEW = HOME_VIEW;&nbsp; &nbsp; public static final String SECONDARY_VIEW = "Secondary View";&nbsp; &nbsp; @Override&nbsp; &nbsp; public void init() {&nbsp; &nbsp; &nbsp; &nbsp; addViewFactory(PRIMARY_VIEW, () -> new PrimaryView().getView());&nbsp; &nbsp; &nbsp; &nbsp; addViewFactory(SECONDARY_VIEW, () -> new SecondaryView().getView());&nbsp; &nbsp; &nbsp; &nbsp; DrawerManager.buildDrawer(this);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void postInit(Scene scene) {&nbsp; &nbsp; &nbsp; &nbsp; Swatch.GREEN.assignTo(scene);&nbsp; &nbsp; &nbsp; &nbsp; scene.getStylesheets().add(GluonApplication.class.getResource("style.css").toExternalForm());&nbsp; &nbsp; &nbsp; &nbsp; ((Stage) scene.getWindow()).getIcons().add(new Image(GluonApplication.class.getResourceAsStream("/icon2.png")));&nbsp; &nbsp; }}可以访问控制器类中的所有字段。只需这样做:public class GluonApplication extends MobileApplication {&nbsp; &nbsp; public static final String PRIMARY_VIEW = HOME_VIEW;&nbsp; &nbsp; public static final String SECONDARY_VIEW = "Secondary View";&nbsp; &nbsp; private SocketConnection socketConnection;&nbsp; &nbsp; private View primaryView; // Add&nbsp; &nbsp; private View secondaryView; // Add&nbsp; &nbsp; @Override&nbsp; &nbsp; public void init() {&nbsp; &nbsp; &nbsp; &nbsp; primaryView = new PrimaryView().getView();&nbsp; &nbsp; &nbsp; &nbsp; secondaryView = new SecondaryView().getView();&nbsp; &nbsp; &nbsp; &nbsp; addViewFactory(PRIMARY_VIEW, () -> primaryView);&nbsp; &nbsp; &nbsp; &nbsp; addViewFactory(SECONDARY_VIEW, () -> secondaryView);&nbsp; &nbsp; &nbsp; &nbsp; DrawerManager.buildDrawer(this);&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* This will start the socket connection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; socketConnection = new SocketConnection(primaryView, secondaryView);&nbsp; &nbsp; &nbsp; &nbsp; socketConnection.start();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void postInit(Scene scene) {&nbsp; &nbsp; &nbsp; &nbsp; Swatch.GREEN.assignTo(scene);&nbsp; &nbsp; &nbsp; &nbsp; scene.getStylesheets().add(GluonApplication.class.getResource("style.css").toExternalForm());&nbsp; &nbsp; &nbsp; &nbsp; ((Stage) scene.getWindow()).getIcons().add(new Image(GluonApplication.class.getResourceAsStream("/icon2.png")));&nbsp; &nbsp; }}然后在SocketConnection的构造函数中。您可以像这样访问字段:&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* Constructor&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public SocketConnection(View primaryView, View secondaryView) {&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* For secondaryView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; statusTextField = (TextField) secondaryView.lookup("#statusTextField");&nbsp; &nbsp; &nbsp; &nbsp; ipAddressTextField = (TextField) secondaryView.lookup("#ipAddressTextField");&nbsp; &nbsp; &nbsp; &nbsp; startSignalModeComboBox = (ComboBox<String>) secondaryView.lookup("#startSignalModeComboBox");&nbsp; &nbsp; &nbsp; &nbsp; predictHorizonTextField = (TextField) secondaryView.lookup("#predictHorizonTextField");&nbsp; &nbsp; &nbsp; &nbsp; controlHorizonTextField = (TextField) secondaryView.lookup("#controlHorizonTextField");&nbsp; &nbsp; &nbsp; &nbsp; sampleTimeTextField = (TextField) secondaryView.lookup("#sampleTimeTextField");&nbsp; &nbsp; &nbsp; &nbsp; pwmDutyCallTextField = (TextField) secondaryView.lookup("#pwmDutyCallTextField");&nbsp; &nbsp; &nbsp; &nbsp; endTimeOfStartSignalTextField = (TextField) secondaryView.lookup("#endTimeOfStartSignalTextField");&nbsp; &nbsp; &nbsp; &nbsp; referencePointTextField = (TextField) secondaryView.lookup("#referencePointTextField");&nbsp; &nbsp; &nbsp; &nbsp; portTextField = (TextField) secondaryView.lookup("#portTextField");&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* For primaryView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; lineChart = (LineChart<String, Number>) primaryView.lookup("#lineChart");&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Declare the data object inside the chart&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; time_output = new Series<String, Number>();&nbsp; &nbsp; &nbsp; &nbsp; lineChart.getData().add(time_output);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java