我正在使用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
对象。
答案 0 :(得分:0)
这是我的答案。在每个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 {
public static final String PRIMARY_VIEW = HOME_VIEW;
public static final String SECONDARY_VIEW = "Secondary View";
@Override
public void init() {
addViewFactory(PRIMARY_VIEW, () -> new PrimaryView().getView());
addViewFactory(SECONDARY_VIEW, () -> new SecondaryView().getView());
DrawerManager.buildDrawer(this);
}
@Override
public void postInit(Scene scene) {
Swatch.GREEN.assignTo(scene);
scene.getStylesheets().add(GluonApplication.class.getResource("style.css").toExternalForm());
((Stage) scene.getWindow()).getIcons().add(new Image(GluonApplication.class.getResourceAsStream("/icon2.png")));
}
}
有权访问控制器类中的所有字段。只需这样做:
public class GluonApplication extends MobileApplication {
public static final String PRIMARY_VIEW = HOME_VIEW;
public static final String SECONDARY_VIEW = "Secondary View";
private SocketConnection socketConnection;
private View primaryView; // Add
private View secondaryView; // Add
@Override
public void init() {
primaryView = new PrimaryView().getView();
secondaryView = new SecondaryView().getView();
addViewFactory(PRIMARY_VIEW, () -> primaryView);
addViewFactory(SECONDARY_VIEW, () -> secondaryView);
DrawerManager.buildDrawer(this);
/*
* This will start the socket connection
*/
socketConnection = new SocketConnection(primaryView, secondaryView);
socketConnection.start();
}
@Override
public void postInit(Scene scene) {
Swatch.GREEN.assignTo(scene);
scene.getStylesheets().add(GluonApplication.class.getResource("style.css").toExternalForm());
((Stage) scene.getWindow()).getIcons().add(new Image(GluonApplication.class.getResourceAsStream("/icon2.png")));
}
}
然后在SocketConnection的构造函数中。您可以访问如下字段:
/*
* Constructor
*/
public SocketConnection(View primaryView, View secondaryView) {
/*
* For secondaryView
*/
statusTextField = (TextField) secondaryView.lookup("#statusTextField");
ipAddressTextField = (TextField) secondaryView.lookup("#ipAddressTextField");
startSignalModeComboBox = (ComboBox<String>) secondaryView.lookup("#startSignalModeComboBox");
predictHorizonTextField = (TextField) secondaryView.lookup("#predictHorizonTextField");
controlHorizonTextField = (TextField) secondaryView.lookup("#controlHorizonTextField");
sampleTimeTextField = (TextField) secondaryView.lookup("#sampleTimeTextField");
pwmDutyCallTextField = (TextField) secondaryView.lookup("#pwmDutyCallTextField");
endTimeOfStartSignalTextField = (TextField) secondaryView.lookup("#endTimeOfStartSignalTextField");
referencePointTextField = (TextField) secondaryView.lookup("#referencePointTextField");
portTextField = (TextField) secondaryView.lookup("#portTextField");
/*
* For primaryView
*/
lineChart = (LineChart<String, Number>) primaryView.lookup("#lineChart");
/*
* Declare the data object inside the chart
*/
time_output = new Series<String, Number>();
lineChart.getData().add(time_output);
}