Get a reference to a Tab from outside FXML Controller - javafx

I'm playing around with JavaFX for the first time on a personal project.
I want to be able to update the content in a Tab (adding a PieChart), but from outside the FXML controller, can anyone tell me if that's possible? How would I get a reference to the relevant tab, and is there a way to specify the location of the item I am adding?
UPDATE: Added some example code.
Hope this gives a clear idea of what I'm trying to do:
The interface:
interface ChartStrategy {
public void DisplayChart(Info info)
}
Two implementations:
public class BarChartStrategy extends ChartStrategy {
public void DisplayChart(Info info)
{
// Create bar charts on specific tabs in the UI
}
}
public class PieChartStrategy extends ChartStrategy {
public void DisplayChart(Info info)
{
// Create pie charts on specific tabs in the UI
}
}
The context:
public class ChartContext {
private ChartStrategy strategy;
public void setChartStrategy(ChartStrategy strategy) {
this.strategy = strategy;
}
public void drawGraphs(Info info) {
strategy.DisplayChart(info);
}
}
In my Controller, I'm reading in a file the user selects from which the data to generate the charts is parsed, .e.g.
#FXML
private void handleButtonAction(ActionEvent event) {
// Load the file and parse the data
...
ChartContext charts = new ChartContext();
charts.setChartStrategy(new PieChartStrategy());
}
So my original thought was that I could draw the charts from the DisplayChart function in the implementations, but it seems that's not a good idea - can anyone give me some advice here on the best way to get this to work?

I would refactor this a bit.
First, letting the strategy display the chart is a bit inflexible. You are giving the strategy two responsibilities: first to decide how to represent the data visually (choose a chart) and second to actually display it somewhere. That violates the single responsibility principle.
So I would do
interface ChartStrategy {
public Chart generateChart(Info info)
}
and similarly for the implementations, of course. Then the responsibility of the strategy is simply to provide a chart: the context can decide what to do with it.
(You can also consider whether returning a Chart here is too rigid: maybe you just want it to return a Parent, or Node. Then your "chart" could be, e.g., a TableView, for example.)
In the theoretical descriptions of the strategy pattern, at least in my interpretation, the "context" just represents "something that is using the strategy". So your context is likely a view or controller (depending on your choice of MVC variant...). As a trivial example you might have something like
public class ChartTab {
private ChartStrategy chartGenerator ;
public void setChartGenerator(ChartStrategy chartGenerator) {
this.chartGenerator = chartGenerator ;
}
public Tab createChartTab(Info info) {
Tab tab = new Tab();
tab.setContent(chartGenerator.generateChart(info));
return tab ;
}
}
and then in your controller
#FXML
private TabPane tabPane ;
#FXML
private void handleButtonAction(ActionEvent event) {
// Load the file and parse the data
...
ChartTab chartTab = new ChartTab();
chartTab.setChartGenerator(new PieChartStrategy());
tabPane.getTabs().add(chartTab.getTab(info));
}
It's also possible just to consider the controller the context (if for a fixed controller you are just creating one kind of chart, which depends on how you split up the FXML files and their corresponding controllers):
public class MyController {
private ChartStrategy chartGenerator ;
#FXML
private TabPane tabPane ;
public MyController(ChartStrategy chartGenerator) {
this.chartGenerator = chartGenerator ;
}
#FXML
private void handleButtonAction(ActionEvent event) {
// Load the file and parse the data
...
Tab tab = new Tab();
tab.setContent(chartGenerator.generateChart(info));
tabPane.getTabs().add(tab);
}
}
Note this controller doesn't have a no-arg constructor, so you cannot use the fx:controller attribute in the FXML file (i.e. remove that attribute from the FXML file). Instead, you'd do
FXMLLoader loader = new FXMLLoader("/path/to/DataDisplay.fxml");
MyController controller = new MyController(new PieChartStrategy());
loader.setController(controller);
Parent root = loader.load();
Now you have an FXML and controller with the functionality to generate charts and display them in a tab pane (or whatever), but the details of what kind of chart is generated are factored out into a pluggable strategy. You still have the proper MVC (or MVP, etc etc) encapsulation in which the details of the UI are kept private to the view-controller (here it's really a presenter, but who's counting...) pair. In other words the strategy knows nothing about the rest of the view, which is as it should be.

Related

JavaFX Implementing 2 different MapChangeListeners [duplicate]

This question already has answers here:
How to make a Java class that implements one interface with two generic types?
(9 answers)
Closed 8 years ago.
I have the following interface, which I want to implement multiple times in my classes:
public interface EventListener<T extends Event>
{
public void onEvent(T event);
}
Now, I want to be able to implement this interface in the following way:
class Foo implements EventListener<LoginEvent>, EventListener<LogoutEvent>
{
#Override
public void onEvent(LoginEvent event)
{
}
#Override
public void onEvent(LogoutEvent event)
{
}
}
However, this gives me the error: Duplicate class com.foo.EventListener on the line:
class Foo implements EventListener<LoginEvent>, EventListener<LogoutEvent>
Is it possible to implement the interface twice with different generics? If not, what's the next closest thing I can do to achieve what I'm trying to do here?
Is it possible to implement the interface twice with different generics
Unfortunately no. The reason you can't implement the same interface twice is because of type erasure. The compiler will handle type parameters, and a runtime EventListener<X> is just a EventListener
If not, what's the next closest thing I can do to achieve what I'm trying to do here?
Type erasure can work in our favor. Once you know that EventListener<X> and EventListener<Y> are just raw EventListener at run-time, it is easier than you think to write an EventListener that can deal with different kinds of Events. Bellow is a solution that passes the IS-A test for EventListener and correctly handles both Login and Logout events by means of simple delegation:
#SuppressWarnings("rawtypes")
public class Foo implements EventListener {
// Map delegation, but could be anything really
private final Map<Class<? extends Event>, EventListener> listeners;
// Concrete Listener for Login - could be anonymous
private class LoginListener implements EventListener<LoginEvent> {
public void onEvent(LoginEvent event) {
System.out.println("Login");
}
}
// Concrete Listener for Logout - could be anonymous
private class LogoutListener implements EventListener<LogoutEvent> {
public void onEvent(LogoutEvent event) {
System.out.println("Logout");
}
}
public Foo() {
#SuppressWarnings("rawtypes")
Map<Class<? extends Event>, EventListener> temp = new HashMap<>();
// LoginEvents will be routed to LoginListener
temp.put(LoginEvent.class, new LoginListener());
// LogoutEvents will be routed to LoginListener
temp.put(LogoutEvent.class, new LogoutListener());
listeners = Collections.unmodifiableMap(temp);
}
#SuppressWarnings("unchecked")
#Override
public void onEvent(Event event) {
// Maps make it easy to delegate, but again, this could be anything
if (listeners.containsKey(event.getClass())) {
listeners.get(event.getClass()).onEvent(event);
} else {
/* Screams if a unsupported event gets passed
* Comment this line if you want to ignore
* unsupported events
*/
throw new IllegalArgumentException("Event not supported");
}
}
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo instanceof EventListener); // true
foo.onEvent(new LoginEvent()); // Login
foo.onEvent(new LogoutEvent()); // Logout
}
}
The suppress warnings are there because we are "abusing" type erasure and delegating to two different event listeners based on the event concrete type. I have chosen to do it using a HashMap and the run-time Event class, but there are a lot of other possible implementations. You could use anonymous inner classes like #user949300 suggested, you could include a getEventType discriminator on the Event class to know what do to with each event and so on.
By using this code for all effects you are creating a single EventListener able to handle two kinds of events. The workaround is 100% self-contained (no need to expose the internal EventListeners).
Finally, there is one last issue that may bother you. At compile time Foo type is actually EventListener. Now, API methods out of your control may be expecting parametrized EventListeners:
public void addLoginListener(EventListener<LoginEvent> event) { // ...
// OR
public void addLogoutListener(EventListener<LogoutEvent> event) { // ...
Again, at run-time both of those methods deal with raw EventListeners. So by having Foo implement a raw interface the compiler will be happy to let you get away with just a type safety warning (which you can disregard with #SuppressWarnings("unchecked")):
eventSource.addLoginListener(foo); // works
While all of this may seem daunting, just repeat to yourself "The compiler is trying to trick me (or save me); there is no spoon <T>. Once you scratch your head for a couple of months trying to make legacy code written before Java 1.5 work with modern code full of type parameters, type erasure becomes second nature to you.
You need to use inner or anonymous classes. For instance:
class Foo {
public EventListener<X> asXListener() {
return new EventListener<X>() {
// code here can refer to Foo
};
}
public EventListener<Y> asYListener() {
return new EventListener<Y>() {
// code here can refer to Foo
};
}
}
This is not possible.
But for that you could create two different classes that implement EventListener interface with two different arguments.
public class Login implements EventListener<LoginEvent> {
public void onEvent(LoginEvent event) {
// TODO Auto-generated method stub
}
}
public class Logout implements EventListener<LogoutEvent> {
public void onEvent(LogoutEvent event) {
// TODO Auto-generated method stub
}
}

How to pass data from child JFXDrawer controller to a node residing in parent controller in JavaFX?

I have a parent controller say HomeController
It has a node SidePanel (JFXDrawer) with SidePanelController and a node anchorPane with varying controller.
HomeController
|
/ \
/ \
/ \
anchorPane SidePanel
(Controller (Controller = SidePanelController)
= varies)
The anchorPane node should load multiple fxml views with menu buttons clicked from SidePanelController.
The problem here is in SidePanelController since the buttons are inside it, I cannot directly load onanchorPane as for SidePanelController the node anchorPane does not exists
This question seems duplicate of this but its not because the parent controller is waiting for scene to close so it fetches back the data to parent controller. But in my case, every time I click on menu button, it will load a view accordingly.
Can anybody provide resources for making controller for JFXDrawer (as child node).
If say, I have a side navigation drawer sidepanel.fxml like this
And I have a HomeScreen like this
So by using following code, I stacked drawer in my homecontroller
try {
SidePanelController controller = new SidePanelController();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/sidepanel.fxml"));
loader.setController(controller);
Parent root = loader.load();
drawer.setSidePane(root);
} catch (IOException err) {
err.printStackTrace();
}
Finally, I will be getting this as combined output
Now what I want is whenever I try to click on say Lorry Receipt button on Side Navigation Panel, it should trigger the node residing on Parent controller. Even the event which will pass data back to parent controller without closing the child node will work.
As #Sedrick suggested, I initialized events on all buttons of SidePanelController in HomeController (Parent) itself. At first it returned NPE, but later I let the buttons initialize and then fetch it back to parent controller.
So here is the solution. It might be non-ethical, so other alternatives still appreciated.
public class SidePanelController implements Initializable {
#FXML
private JFXButton btn_lr;
#FXML
private JFXButton btn_shipment;
#FXML
private JFXButton btn_add_inward;
}
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Your other activities when sidedrawer/pane initializes
}
//After I initialize, I would like all of these buttons to be fetched to my Parent controller. So instead of me passing each button separately, I made a list to save my time.
public ObservableList<JFXButton> fetchAllButtons(){
ObservableList<JFXButton> allbuttons = FXCollections.observableArrayList(btn_lr, btn_add_inward, btn_shipment);
return allbuttons;
}
Now in HomeController or ParentController, I fetch all these buttons and create events for it separately. So here goes the code.
public class HomeController implements Initializable {
#FXML
private JFXHamburger hamburger;
#FXML
private JFXDrawer drawer;
//Creating Hamburger Task to toggle sidebar
HamburgerBackArrowBasicTransition burgerTask;
//Declaring sidepanelcontroller globally because I need it in multiple methods.
SidePanelController controller = new SidePanelController();
//Finally a list to fetch the list of all buttons from SidePanelController
ObservableList<JFXButton> sidebuttons;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Using hamburger to toggle my side drawer
burgerTask = new HamburgerBackArrowBasicTransition(hamburger);
//Initializing the scene of drawer/SidePanel FXML file on Home.fxml or parent fxml itself
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/sidepanel.fxml"));
loader.setController(controller);
Parent root = loader.load();
drawer.setSidePane(root);
sidebuttons = controller.fetchAllButtons();
//Make sure not to declare this before initializing or else it returns NPE
} catch (Exception err) {
err.printStackTrace();
}
//Finally you can declare your tasks for each button by declaring events on each of those buttons. I thought only common but different thing you can between buttons is the name of it. So I used switch statement just to point out each button separately. (I know this might be unethical, but works for me though)
for (JFXButton button: sidebuttons) {
switch (button.getText()){
case "Lorry Receipt":
button.setOnAction(actionEvent -> {
//Your actions here when button with name Lorry Receipt is pressed
});
break;
case "Shipment Memo":
button.setOnAction(actionEvent -> {
//Your actions when button with name shipment memo is pressed
});
break;
case "Inward Challan":
button.setOnAction(actionEvent -> {
//Your actions when button with name Inward Challan is pressed
});
break;
}
}
}
}
Other Advantage I found with this is, I don't have to show ProgressBar/ProgressIndicator of each scene separately. Since Child Component's ActionEvent is on ParentNode, the ProgressBar/Indicator can be binded to it and works like a charm.

JavaFX: Need help understanding setControllerFactory

I set up multiple custom controllers during the creation of an app and would need some help in organizing these controllers with setControllerFactory in JavaFX.
I'm fairly inexperienced with JavaFX but invested quite some time in creating a small app with Scenebuilder and JavaFX.
Background of the app
The app consists of:
- a map (implemented as an imageView)
- a sidebar with buttons and icons for drag and drop events.
- the map also has separate layers as the target for the drag and drop of different icon types.
As a prototype of my drag and drop event I used the instructions of Joel Graff (https://monograff76.wordpress.com/2015/02/17/developing-a-drag-and-drop-ui-in-javafx-part-i-skeleton-application/). He writes "in order for an object to be visible beyond a container’s edges, it must be a child of a parent or other ancestral container – it must belong to a higher level of the hierarchy. In the case of our drag-over icon, this means we had to add it as a child to the RootLayout’s top-level AnchorPane." and he uses dynamic roots for his project.
To teach myself how to use custom control with FXML I used Irina Fedortsova's tutorial https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm.
And to learn how to set up multiple screens I used the video https://www.youtube.com/watch?v=5GsdaZWDcdY and associating code from https://github.com/acaicedo/JFX-MultiScreen.
After building my app the logic tier of my app got more and more entangled with the presentation tier, and I feel as if my code would benefit greatly from some refactoring.
My problem seems to be a lack in the understanding of the load and initialize process of controller classes. Since the drag icons and the RootLayout have to be loaded from the beginning, it is a mystery to me how I can load these classes in a way that I can call them again at a later time.
When I was looking for further solutions, I repeatedly came across the method setControllerFactory. Unfortunately I can't find a good explanation for how to use it properly and what it's specific purpose is.
The only tutorial I found was: https://riptutorial.com/javafx/example/8805/passing-parameters-to-fxml---using-a-controllerfactory, unfortunately it seems to be a bit insufficient for my purpose.
I feel as if I would benefit the most from a methode/class with which I could organize all my custom controllers, load and initialize them at the appropriate time and then later access them again (similar to the interface and superclass in the video for JFX-MultiScreen).
I repeatedly came across the method setControllerFactory. Unfortunately I can't find a good explanation for how to use it properly and what it's specific purpose is
By default, the FXMLLoader.load() method instantiates the controller named in the fxml document using the 0-arg constructor. The FXMLLoader.setControllerFactory​ method is used when you want your FXMLLoader object to instantiate controllers in a certain way, e.g. use a different controller constructor on specific arguments, call a method on the controller before it's returned, etc, as in
FXMLLoader loader = new FXMLLoader(...);
loader.setControllerFactory(c -> {
return new MyController("foo", "bar");
});
Now when you call loader.load() the controller will be created as above. However, calling the FXMLLoader.setController​ method on a preexisting controller may be easier.
I feel as if I would benefit the most from a methode/class with which I could organize all my custom controllers, load and initialize them at the appropriate time and then later access them again
When I first came across this problem, as you have, I tried and retried many approaches. What I finally settled on was turning my main application class into a singleton. The singleton pattern is great when you need to create one instance of a class which should be accessible throughout your program. I know there are many people who will take issue with that (in that it's essentially a global variable with added structure), but I've found that it reduced complexity significantly in that I no longer had to manage a somewhat artificial structure of object references going every which way.
The singleton lets controllers communicate with your main application class by calling, for example, MyApp.getSingleton(). Still in the main application class, you can then organize all of your views in a private HashMap and add public add(...), remove(...), and activate(...) methods which can add or remove views from the map or activate a view in the map (i.e. set the scene's root to your new view).
For an application with many views that may be placed in different packages, you can organize their locations with an enum:
public enum View {
LOGIN("login/Login.fxml"),
NEW_USER("register/NewUser.fxml"),
USER_HOME("user/UserHome.fxml"),
ADMIN_HOME("admin/AdminHome.fxml");
public final String location;
View(String location) {
this.location = "/views/" + location;
}
}
Below is an example of the main application class:
public final class MyApp extends Application {
// Singleton
private static MyApp singleton;
public MyApp() { singleton = this; }
public static MyApp getSingleton() { return singleton; }
// Main window
private Stage stage;
private Map<View, Parent> parents = new HashMap<>();
#Override
public void start(Stage primaryStage) {
stage = primaryStage;
stage.setTitle("My App");
add(View.LOGIN);
stage.setScene(new Scene(parents.get(View.LOGIN)));
stage.show();
}
public void add(View view) {
var loader = new FXMLLoader(getClass().getResource(view.location));
try {
Parent root = loader.load();
parents.put(view, root);
} catch (IOException e) { /* Do something */ }
}
public void remove(View view) {
parents.remove(view);
}
public void activate(View view) {
stage.getScene().setRoot(parents.get(view));
}
public void removeAllAndActivate(View view) {
parents.clear();
add(view);
activate(view);
}
}
If you have application-wide resources you can put them in the app class and add getters/setters so your controllers can access them. Here is an example controller class:
public final class Login implements Initializable {
MyApp app = MyApp.getSingleton();
// Some #FXML variables here..
#FXML private void login() {
// Authenticate..
app.removeAllAndActivate(View.USER_HOME);
}
#FXML private void createAccount() {
app.add(View.NEW_USER);
app.activate(View.NEW_USER);
}
#Override
public void initialize(URL url, ResourceBundle rb) {}
}

JavaFX exchange data between controllers (parent/child forms)

Is this approach lame to do so?
I've tried many approaches e.g.
How can I exchange data between forms
JavaFX pass values from child to parent
JavaFX 2.2 -fx:include - how to access parent controller from child controller
but this seems to be pretty direct and understandable.
public class ParentController {
private Settings settings;
public void setSettings(Settings settings) {
this.settings = settings;
System.out.println(this.settings.toString());
}
#FXML
private Button open;
#FXML
private void pass() throws IOException {
Stage st = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Child.fxml"));
Region root = (Region) loader.load();
Scene scene = new Scene(root);
st.setScene(scene);
ChildController controller = loader.<ChildController>getController();
controller.initialize(this);
st.show();
}
}
public class ChildController {
#FXML
private TextField number; // some settings
#FXML
private Button ok;
private ParentController parentController ;
#FXML
public void pass() {
Stage stage = (Stage) ok.getScene().getWindow();
parentController.setSettings(setSettings());
stage.close();
}
private Settings setSettings(){
return new Settings(Integer.valueOf(this.number.getText()));
}
public void initialize(ParentController parentController) {
this.parentController = parentController;
}
}
In such the way I'm getting settings object generated in Child controller and pass this object to the parent controller.
This works...
Is this approach appropriate? If not, what pitfall is may imply?
The question is probably too opinion-based for this forum. The trade-off for the simplicity you gain (compared to, say, JavaFX pass values from child to parent) is that you have tightly-coupled the child to the parent: in other words you can't use the child view/controller in any context where you don't have that specific parent. In the linked approach, the child view/controller have no dependency on the parent. Whether or not this is desirable/beneficial/worth the added complexity will depend on your exact use case.
I thought it was appropriate. for me it was just a coding style.
but in my opinion , change your settings variable to public static, stored in the Main class and initialized in the Main class, to be accessible at all controllers or other classes. thats my style :D
public static settings Settings;
Main.settings.( Something)
Happy coding..

Stuck with event handling Java FX - Controller - Scene builder

I want to use Netbeans Java FX with Scene builder for a measurement application. I have designed a scene with controls. I can handle the events from the UI-controls within the '...Controller.java'.
The 'controller' is the standard piece of code that is referenced in the XML file and gets initialized by the system with:
public void initialize(URL url, ResourceBundle rb) { ..
My problem: how do I access my central, persisting, 'model' objects from within the controller? Or, to be more exact, from the event handlers created within the controller initialize function.
The 'model' object would be created within the application object.
The solution must be trivial, but I have not found a way to
either access the Application from the controller
or access the controller from within the Application.
What am I missing?
(the next question would be how to access the tree of panes within the object hierarchy created by screen builder, e.g. for graphics manipulation on output. Since the objects are not created by own code I can not store references to some of them. Ok, they could perhaps be found and referenced by tree-walking, but there must be a better way!)
Thanks for all insights!
I have used the 2nd approach (access the controller from within the Application) for awhile ago similar to following. In Application class:
//..
private FooController fooController;
private Pane fooPage;
private Model myModel;
#Override
public void start(Stage stage) {
//..
myModel = new Model();
getFooController().updateModel(myModel);
//..
Button button = new Button("Update model with new one");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Model myNewModel = new Model();
getFooController().updateModel(myNewModel);
}
}
// create scene, add fooPage to it and show.
}
private FooController getFooController() {
if (fooController == null) {
FXMLLoader fxmlLoader = new FXMLLoader();
fooPage = fxmlLoader.load(getClass().getResource("foo.fxml").openStream());
fooController = (FooController) fxmlLoader.getController();
}
return fooController;
}
Actually the first and second parts of your question is answered JavaFX 2.0 + FXML. Updating scene values from a different Task to the similar question of yours.

Resources