How to pass parameters to JavaFX application?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am running my JavaFX application like this:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
Application.launch(MainStage.class);
}
}
MainStage
class extends Appication
. Application.launch
starts my JavaFX window in a special FX-thread, but in my main method I don't even have an instance of my MainStage
class.
How to pass non-String parameter (controller in my case) to MainStage
instance? Is it a flawed design?
java parameters javafx
add a comment |
I am running my JavaFX application like this:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
Application.launch(MainStage.class);
}
}
MainStage
class extends Appication
. Application.launch
starts my JavaFX window in a special FX-thread, but in my main method I don't even have an instance of my MainStage
class.
How to pass non-String parameter (controller in my case) to MainStage
instance? Is it a flawed design?
java parameters javafx
Why can't you simply instantiate the controller inside your application? Btw.MainStage
doesn't seem to be the right naming for your main application asStage
has a different meaning in JavaFX.
– isnot2bad
Jul 7 '14 at 13:38
add a comment |
I am running my JavaFX application like this:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
Application.launch(MainStage.class);
}
}
MainStage
class extends Appication
. Application.launch
starts my JavaFX window in a special FX-thread, but in my main method I don't even have an instance of my MainStage
class.
How to pass non-String parameter (controller in my case) to MainStage
instance? Is it a flawed design?
java parameters javafx
I am running my JavaFX application like this:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
Application.launch(MainStage.class);
}
}
MainStage
class extends Appication
. Application.launch
starts my JavaFX window in a special FX-thread, but in my main method I don't even have an instance of my MainStage
class.
How to pass non-String parameter (controller in my case) to MainStage
instance? Is it a flawed design?
java parameters javafx
java parameters javafx
asked Jul 7 '14 at 13:31
ferrerverckferrerverck
3032515
3032515
Why can't you simply instantiate the controller inside your application? Btw.MainStage
doesn't seem to be the right naming for your main application asStage
has a different meaning in JavaFX.
– isnot2bad
Jul 7 '14 at 13:38
add a comment |
Why can't you simply instantiate the controller inside your application? Btw.MainStage
doesn't seem to be the right naming for your main application asStage
has a different meaning in JavaFX.
– isnot2bad
Jul 7 '14 at 13:38
Why can't you simply instantiate the controller inside your application? Btw.
MainStage
doesn't seem to be the right naming for your main application as Stage
has a different meaning in JavaFX.– isnot2bad
Jul 7 '14 at 13:38
Why can't you simply instantiate the controller inside your application? Btw.
MainStage
doesn't seem to be the right naming for your main application as Stage
has a different meaning in JavaFX.– isnot2bad
Jul 7 '14 at 13:38
add a comment |
8 Answers
8
active
oldest
votes
Usually, there is no need to pass arguments to the main application other than the program arguments passed to your main. The only reason why one wants to do this is to create a reusable Application
. But the Application
does not need to be reusable, because it is the piece of code that assembles your application. Think of the start
method to be the new main
!
So instead of writing a reusable Application
that gets configured in the main
method, the application itself should be the configurator and use reusable components to build up the app in the start
method, e.g.:
public class MyApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
// Just on example how it could be done...
Controller controller = new Controller();
MyMainComponent mainComponent = new MyMainComponent(controller);
mainComponent.showIn(stage);
}
public static void main(String args) {
Application.launch(args);
}
}
"The only reason ... is to create a reusable Application" I think there are other reasons like when you want a non-fx application launchApplication
(for example fx gui added to existing application). While doing so the non-fx application needs to pass information to theApplication
.
– c0der
Oct 26 '18 at 6:27
@c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your ownApplication
class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in theApplication
class. But this is poor design anyway.
– isnot2bad
Nov 25 '18 at 16:26
I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to anApplication
. Maybe you can provide a better answer to this
– c0der
Nov 25 '18 at 17:02
add a comment |
Starting with JavaFX 9 you can trigger the launch of the JavaFX platform "manually" using the public API. The only drawback is that the stop
method is not invoked the way it would be in application started via Application.launch
:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
final MainStage mainStage = new MainStage(controller);
mainStage.init();
Platform.startup(() -> {
// create primary stage
Stage stage = new Stage();
mainStage.start(stage);
});
}
}
The Runnable
passed to Platform.startup
is invoked on the JavaFX application thread.
add a comment |
Here's a nice example I found elsewhere
@Override
public void init () throws Exception
{
super.init ();
Parameters parameters = getParameters ();
Map<String, String> namedParameters = parameters.getNamed ();
List<String> rawArguments = parameters.getRaw ();
List<String> unnamedParameters = parameters.getUnnamed ();
System.out.println ("nnamedParameters -");
for (Map.Entry<String, String> entry : namedParameters.entrySet ())
System.out.println (entry.getKey () + " : " + entry.getValue ());
System.out.println ("nrawArguments -");
for (String raw : rawArguments)
System.out.println (raw);
System.out.println ("nunnamedParameters -");
for (String unnamed : unnamedParameters)
System.out.println (unnamed);
}
add a comment |
Question - I
I don't even have an instance of my MainStage class !
Solution
Your main method doesn't need an instance of MainStage
to call the start() of your MainStage
. This job is done automatically by the JavaFX launcher.
From Docs
The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:
Constructs an instance of the specified Application class
- Calls the init() method
- Calls the start(javafx.stage.Stage) method
- Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
- Calls the stop() method
and
The Java launcher loads and initializes the specified Application
class on the JavaFX Application Thread. If there is no main method in
the Application class, or if the main method calls
Application.launch(), then an instance of the Application is then
constructed on the JavaFX Application Thread.
Question - II
How to pass non-String parameter (controller in my case) to MainStage instance?
Solution
Why do you need to pass non-String parameter to MainStage
? If you need an controller object, just fetch it from the FXML
Example
public class MainEntry extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Pane pane = (Pane) loader.load(getClass().getResourceAsStream("sample.fxml"));
//Get the controller
Controller controller = (Controller)loader.getController();
Scene scene = new Scene(pane, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args) {
launch(args);// or launch(MainEntry.class)
}
}
Could you explain me how thestage
parameter is used? Who initializes it? What do you mean by callingcontroller object
? Thanks and Regards.
– snr
Aug 26 '17 at 17:22
Stage
is initialized by the JavaFX launcher. It is used to show the application window.
– ItachiUchiha
Aug 27 '17 at 18:45
add a comment |
The String
array passed to the main()
method are the parameters of the application, not specifically to the JavaFX module if you arbitrarily choose to use JavaFX.
The easiest solution could be to store the argumets for later use (e.g. static attribute next to the main()
method, and a static getter method to access it).
add a comment |
You can set the Controller in the MainStage class. But you'll have to do it static, otherwise it will be null.
Hava a look at the code:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
MainStage ms = new MainStage();
ms.setController(controller);
Application.launch(MainStage.class, (java.lang.String) null);
}
}
public class MainStage extends Application {
private static Controller controller;
public void start(Stage primaryStage) throws Exception {
System.out.println(controller);
primaryStage.show();
}
public void setController(Controller controller){
this.controller = controller;
}
}
add a comment |
Of course there is a need and possibility to pass parameters to JavaFX application.
I did it to run my JavaFX client from different places, where different network configurations are required (direct or via proxy). Not to make instant changes in code, I implemented several network configurations to be chosen from in application run command with parameter like --configurationIndex=1. The default code value is 0.
List<String> parameters;
int parameterIndex;
String parameter;
parameters =
getParameters().getRaw();
for (parameterIndex = 0;
parameterIndex < parameters.size();
parameterIndex++) {
parameter =
parameters.get(
parameterIndex);
if (parameter.contains("configurationIndex")) {
configurationIndex =
Integer.valueOf(
parameters.get(parameterIndex).
split("=")[1]);
}
}
In Netbeans you can set this parameter for debugging need directly on your project: Project - Properties - Run - Parameters - insert --configurationIndex=1 into field.
add a comment |
case 1 = java standard types - transmit them as java Strings "--name=value" and then convert them to the final destination using the answer of dmolony
for ( Map.Entry<String, String> entry : namedParameters.entrySet ()){
System.out.println (entry.getKey() + " : " + entry.getValue ());
switch( entry.getKey()){
case "media_url": media_url_received = entry.getValue(); break;
}
}
The parameter is created at Application.launch and decoded at init
String args = {"--media_url=" + media_url, "--master_level=" + master_level};
Application.launch( args);
case 2 = If you have to transmit java objects use this workaround (this is for only one javafx Application launch, create a Map of workarounds and send index as strings if you have a complex case)
public static Transfer_param javafx_tp;
and in your class init set the instance of object to a static inside it's own class
Transfer_param.javafx_tp = tp1;
now you can statically find your last object for working with only one JavaFx Applications (remember that if you have a lot of JavaFx applications active you should send a String with a static variable identification inside a Map or array so you do not take a fake object address from your static structures (use the example at case 1 of this answer to transmit --javafx_id=3 ...))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24611789%2fhow-to-pass-parameters-to-javafx-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
Usually, there is no need to pass arguments to the main application other than the program arguments passed to your main. The only reason why one wants to do this is to create a reusable Application
. But the Application
does not need to be reusable, because it is the piece of code that assembles your application. Think of the start
method to be the new main
!
So instead of writing a reusable Application
that gets configured in the main
method, the application itself should be the configurator and use reusable components to build up the app in the start
method, e.g.:
public class MyApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
// Just on example how it could be done...
Controller controller = new Controller();
MyMainComponent mainComponent = new MyMainComponent(controller);
mainComponent.showIn(stage);
}
public static void main(String args) {
Application.launch(args);
}
}
"The only reason ... is to create a reusable Application" I think there are other reasons like when you want a non-fx application launchApplication
(for example fx gui added to existing application). While doing so the non-fx application needs to pass information to theApplication
.
– c0der
Oct 26 '18 at 6:27
@c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your ownApplication
class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in theApplication
class. But this is poor design anyway.
– isnot2bad
Nov 25 '18 at 16:26
I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to anApplication
. Maybe you can provide a better answer to this
– c0der
Nov 25 '18 at 17:02
add a comment |
Usually, there is no need to pass arguments to the main application other than the program arguments passed to your main. The only reason why one wants to do this is to create a reusable Application
. But the Application
does not need to be reusable, because it is the piece of code that assembles your application. Think of the start
method to be the new main
!
So instead of writing a reusable Application
that gets configured in the main
method, the application itself should be the configurator and use reusable components to build up the app in the start
method, e.g.:
public class MyApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
// Just on example how it could be done...
Controller controller = new Controller();
MyMainComponent mainComponent = new MyMainComponent(controller);
mainComponent.showIn(stage);
}
public static void main(String args) {
Application.launch(args);
}
}
"The only reason ... is to create a reusable Application" I think there are other reasons like when you want a non-fx application launchApplication
(for example fx gui added to existing application). While doing so the non-fx application needs to pass information to theApplication
.
– c0der
Oct 26 '18 at 6:27
@c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your ownApplication
class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in theApplication
class. But this is poor design anyway.
– isnot2bad
Nov 25 '18 at 16:26
I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to anApplication
. Maybe you can provide a better answer to this
– c0der
Nov 25 '18 at 17:02
add a comment |
Usually, there is no need to pass arguments to the main application other than the program arguments passed to your main. The only reason why one wants to do this is to create a reusable Application
. But the Application
does not need to be reusable, because it is the piece of code that assembles your application. Think of the start
method to be the new main
!
So instead of writing a reusable Application
that gets configured in the main
method, the application itself should be the configurator and use reusable components to build up the app in the start
method, e.g.:
public class MyApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
// Just on example how it could be done...
Controller controller = new Controller();
MyMainComponent mainComponent = new MyMainComponent(controller);
mainComponent.showIn(stage);
}
public static void main(String args) {
Application.launch(args);
}
}
Usually, there is no need to pass arguments to the main application other than the program arguments passed to your main. The only reason why one wants to do this is to create a reusable Application
. But the Application
does not need to be reusable, because it is the piece of code that assembles your application. Think of the start
method to be the new main
!
So instead of writing a reusable Application
that gets configured in the main
method, the application itself should be the configurator and use reusable components to build up the app in the start
method, e.g.:
public class MyApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
// Just on example how it could be done...
Controller controller = new Controller();
MyMainComponent mainComponent = new MyMainComponent(controller);
mainComponent.showIn(stage);
}
public static void main(String args) {
Application.launch(args);
}
}
answered Jul 10 '14 at 9:29
isnot2badisnot2bad
20.2k22242
20.2k22242
"The only reason ... is to create a reusable Application" I think there are other reasons like when you want a non-fx application launchApplication
(for example fx gui added to existing application). While doing so the non-fx application needs to pass information to theApplication
.
– c0der
Oct 26 '18 at 6:27
@c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your ownApplication
class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in theApplication
class. But this is poor design anyway.
– isnot2bad
Nov 25 '18 at 16:26
I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to anApplication
. Maybe you can provide a better answer to this
– c0der
Nov 25 '18 at 17:02
add a comment |
"The only reason ... is to create a reusable Application" I think there are other reasons like when you want a non-fx application launchApplication
(for example fx gui added to existing application). While doing so the non-fx application needs to pass information to theApplication
.
– c0der
Oct 26 '18 at 6:27
@c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your ownApplication
class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in theApplication
class. But this is poor design anyway.
– isnot2bad
Nov 25 '18 at 16:26
I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to anApplication
. Maybe you can provide a better answer to this
– c0der
Nov 25 '18 at 17:02
"The only reason ... is to create a reusable Application" I think there are other reasons like when you want a non-fx application launch
Application
(for example fx gui added to existing application). While doing so the non-fx application needs to pass information to the Application
.– c0der
Oct 26 '18 at 6:27
"The only reason ... is to create a reusable Application" I think there are other reasons like when you want a non-fx application launch
Application
(for example fx gui added to existing application). While doing so the non-fx application needs to pass information to the Application
.– c0der
Oct 26 '18 at 6:27
@c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your own
Application
class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in the Application
class. But this is poor design anyway.– isnot2bad
Nov 25 '18 at 16:26
@c0der I disagree. There are two ways to start an fx-application from a non-fx Java application: Either as separate process, or in the same VM. The former does not allow to pass anything but strings/streams anyway, so there is no need to have a richer API there. And if you want to start it in the same VM, you can easily write your own
Application
class as 'configuration'-wrapper around the reusable UI-component as described in my answer. Of course this does not work if too much work has to be done in the Application
class. But this is poor design anyway.– isnot2bad
Nov 25 '18 at 16:26
I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to an
Application
. Maybe you can provide a better answer to this– c0der
Nov 25 '18 at 17:02
I am not sure I follow, but I think we agree that there may be a need to pass a reference from a non-fx Java application to an
Application
. Maybe you can provide a better answer to this– c0der
Nov 25 '18 at 17:02
add a comment |
Starting with JavaFX 9 you can trigger the launch of the JavaFX platform "manually" using the public API. The only drawback is that the stop
method is not invoked the way it would be in application started via Application.launch
:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
final MainStage mainStage = new MainStage(controller);
mainStage.init();
Platform.startup(() -> {
// create primary stage
Stage stage = new Stage();
mainStage.start(stage);
});
}
}
The Runnable
passed to Platform.startup
is invoked on the JavaFX application thread.
add a comment |
Starting with JavaFX 9 you can trigger the launch of the JavaFX platform "manually" using the public API. The only drawback is that the stop
method is not invoked the way it would be in application started via Application.launch
:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
final MainStage mainStage = new MainStage(controller);
mainStage.init();
Platform.startup(() -> {
// create primary stage
Stage stage = new Stage();
mainStage.start(stage);
});
}
}
The Runnable
passed to Platform.startup
is invoked on the JavaFX application thread.
add a comment |
Starting with JavaFX 9 you can trigger the launch of the JavaFX platform "manually" using the public API. The only drawback is that the stop
method is not invoked the way it would be in application started via Application.launch
:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
final MainStage mainStage = new MainStage(controller);
mainStage.init();
Platform.startup(() -> {
// create primary stage
Stage stage = new Stage();
mainStage.start(stage);
});
}
}
The Runnable
passed to Platform.startup
is invoked on the JavaFX application thread.
Starting with JavaFX 9 you can trigger the launch of the JavaFX platform "manually" using the public API. The only drawback is that the stop
method is not invoked the way it would be in application started via Application.launch
:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
final MainStage mainStage = new MainStage(controller);
mainStage.init();
Platform.startup(() -> {
// create primary stage
Stage stage = new Stage();
mainStage.start(stage);
});
}
}
The Runnable
passed to Platform.startup
is invoked on the JavaFX application thread.
answered Sep 22 '17 at 17:45
fabianfabian
53.7k115373
53.7k115373
add a comment |
add a comment |
Here's a nice example I found elsewhere
@Override
public void init () throws Exception
{
super.init ();
Parameters parameters = getParameters ();
Map<String, String> namedParameters = parameters.getNamed ();
List<String> rawArguments = parameters.getRaw ();
List<String> unnamedParameters = parameters.getUnnamed ();
System.out.println ("nnamedParameters -");
for (Map.Entry<String, String> entry : namedParameters.entrySet ())
System.out.println (entry.getKey () + " : " + entry.getValue ());
System.out.println ("nrawArguments -");
for (String raw : rawArguments)
System.out.println (raw);
System.out.println ("nunnamedParameters -");
for (String unnamed : unnamedParameters)
System.out.println (unnamed);
}
add a comment |
Here's a nice example I found elsewhere
@Override
public void init () throws Exception
{
super.init ();
Parameters parameters = getParameters ();
Map<String, String> namedParameters = parameters.getNamed ();
List<String> rawArguments = parameters.getRaw ();
List<String> unnamedParameters = parameters.getUnnamed ();
System.out.println ("nnamedParameters -");
for (Map.Entry<String, String> entry : namedParameters.entrySet ())
System.out.println (entry.getKey () + " : " + entry.getValue ());
System.out.println ("nrawArguments -");
for (String raw : rawArguments)
System.out.println (raw);
System.out.println ("nunnamedParameters -");
for (String unnamed : unnamedParameters)
System.out.println (unnamed);
}
add a comment |
Here's a nice example I found elsewhere
@Override
public void init () throws Exception
{
super.init ();
Parameters parameters = getParameters ();
Map<String, String> namedParameters = parameters.getNamed ();
List<String> rawArguments = parameters.getRaw ();
List<String> unnamedParameters = parameters.getUnnamed ();
System.out.println ("nnamedParameters -");
for (Map.Entry<String, String> entry : namedParameters.entrySet ())
System.out.println (entry.getKey () + " : " + entry.getValue ());
System.out.println ("nrawArguments -");
for (String raw : rawArguments)
System.out.println (raw);
System.out.println ("nunnamedParameters -");
for (String unnamed : unnamedParameters)
System.out.println (unnamed);
}
Here's a nice example I found elsewhere
@Override
public void init () throws Exception
{
super.init ();
Parameters parameters = getParameters ();
Map<String, String> namedParameters = parameters.getNamed ();
List<String> rawArguments = parameters.getRaw ();
List<String> unnamedParameters = parameters.getUnnamed ();
System.out.println ("nnamedParameters -");
for (Map.Entry<String, String> entry : namedParameters.entrySet ())
System.out.println (entry.getKey () + " : " + entry.getValue ());
System.out.println ("nrawArguments -");
for (String raw : rawArguments)
System.out.println (raw);
System.out.println ("nunnamedParameters -");
for (String unnamed : unnamedParameters)
System.out.println (unnamed);
}
answered Apr 8 '15 at 5:13
dmolonydmolony
884723
884723
add a comment |
add a comment |
Question - I
I don't even have an instance of my MainStage class !
Solution
Your main method doesn't need an instance of MainStage
to call the start() of your MainStage
. This job is done automatically by the JavaFX launcher.
From Docs
The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:
Constructs an instance of the specified Application class
- Calls the init() method
- Calls the start(javafx.stage.Stage) method
- Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
- Calls the stop() method
and
The Java launcher loads and initializes the specified Application
class on the JavaFX Application Thread. If there is no main method in
the Application class, or if the main method calls
Application.launch(), then an instance of the Application is then
constructed on the JavaFX Application Thread.
Question - II
How to pass non-String parameter (controller in my case) to MainStage instance?
Solution
Why do you need to pass non-String parameter to MainStage
? If you need an controller object, just fetch it from the FXML
Example
public class MainEntry extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Pane pane = (Pane) loader.load(getClass().getResourceAsStream("sample.fxml"));
//Get the controller
Controller controller = (Controller)loader.getController();
Scene scene = new Scene(pane, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args) {
launch(args);// or launch(MainEntry.class)
}
}
Could you explain me how thestage
parameter is used? Who initializes it? What do you mean by callingcontroller object
? Thanks and Regards.
– snr
Aug 26 '17 at 17:22
Stage
is initialized by the JavaFX launcher. It is used to show the application window.
– ItachiUchiha
Aug 27 '17 at 18:45
add a comment |
Question - I
I don't even have an instance of my MainStage class !
Solution
Your main method doesn't need an instance of MainStage
to call the start() of your MainStage
. This job is done automatically by the JavaFX launcher.
From Docs
The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:
Constructs an instance of the specified Application class
- Calls the init() method
- Calls the start(javafx.stage.Stage) method
- Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
- Calls the stop() method
and
The Java launcher loads and initializes the specified Application
class on the JavaFX Application Thread. If there is no main method in
the Application class, or if the main method calls
Application.launch(), then an instance of the Application is then
constructed on the JavaFX Application Thread.
Question - II
How to pass non-String parameter (controller in my case) to MainStage instance?
Solution
Why do you need to pass non-String parameter to MainStage
? If you need an controller object, just fetch it from the FXML
Example
public class MainEntry extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Pane pane = (Pane) loader.load(getClass().getResourceAsStream("sample.fxml"));
//Get the controller
Controller controller = (Controller)loader.getController();
Scene scene = new Scene(pane, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args) {
launch(args);// or launch(MainEntry.class)
}
}
Could you explain me how thestage
parameter is used? Who initializes it? What do you mean by callingcontroller object
? Thanks and Regards.
– snr
Aug 26 '17 at 17:22
Stage
is initialized by the JavaFX launcher. It is used to show the application window.
– ItachiUchiha
Aug 27 '17 at 18:45
add a comment |
Question - I
I don't even have an instance of my MainStage class !
Solution
Your main method doesn't need an instance of MainStage
to call the start() of your MainStage
. This job is done automatically by the JavaFX launcher.
From Docs
The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:
Constructs an instance of the specified Application class
- Calls the init() method
- Calls the start(javafx.stage.Stage) method
- Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
- Calls the stop() method
and
The Java launcher loads and initializes the specified Application
class on the JavaFX Application Thread. If there is no main method in
the Application class, or if the main method calls
Application.launch(), then an instance of the Application is then
constructed on the JavaFX Application Thread.
Question - II
How to pass non-String parameter (controller in my case) to MainStage instance?
Solution
Why do you need to pass non-String parameter to MainStage
? If you need an controller object, just fetch it from the FXML
Example
public class MainEntry extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Pane pane = (Pane) loader.load(getClass().getResourceAsStream("sample.fxml"));
//Get the controller
Controller controller = (Controller)loader.getController();
Scene scene = new Scene(pane, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args) {
launch(args);// or launch(MainEntry.class)
}
}
Question - I
I don't even have an instance of my MainStage class !
Solution
Your main method doesn't need an instance of MainStage
to call the start() of your MainStage
. This job is done automatically by the JavaFX launcher.
From Docs
The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:
Constructs an instance of the specified Application class
- Calls the init() method
- Calls the start(javafx.stage.Stage) method
- Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
- Calls the stop() method
and
The Java launcher loads and initializes the specified Application
class on the JavaFX Application Thread. If there is no main method in
the Application class, or if the main method calls
Application.launch(), then an instance of the Application is then
constructed on the JavaFX Application Thread.
Question - II
How to pass non-String parameter (controller in my case) to MainStage instance?
Solution
Why do you need to pass non-String parameter to MainStage
? If you need an controller object, just fetch it from the FXML
Example
public class MainEntry extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Pane pane = (Pane) loader.load(getClass().getResourceAsStream("sample.fxml"));
//Get the controller
Controller controller = (Controller)loader.getController();
Scene scene = new Scene(pane, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args) {
launch(args);// or launch(MainEntry.class)
}
}
edited May 27 '16 at 18:27
TuringTux
51911220
51911220
answered Jul 7 '14 at 16:46
ItachiUchihaItachiUchiha
28.3k679141
28.3k679141
Could you explain me how thestage
parameter is used? Who initializes it? What do you mean by callingcontroller object
? Thanks and Regards.
– snr
Aug 26 '17 at 17:22
Stage
is initialized by the JavaFX launcher. It is used to show the application window.
– ItachiUchiha
Aug 27 '17 at 18:45
add a comment |
Could you explain me how thestage
parameter is used? Who initializes it? What do you mean by callingcontroller object
? Thanks and Regards.
– snr
Aug 26 '17 at 17:22
Stage
is initialized by the JavaFX launcher. It is used to show the application window.
– ItachiUchiha
Aug 27 '17 at 18:45
Could you explain me how the
stage
parameter is used? Who initializes it? What do you mean by calling controller object
? Thanks and Regards.– snr
Aug 26 '17 at 17:22
Could you explain me how the
stage
parameter is used? Who initializes it? What do you mean by calling controller object
? Thanks and Regards.– snr
Aug 26 '17 at 17:22
Stage
is initialized by the JavaFX launcher. It is used to show the application window.– ItachiUchiha
Aug 27 '17 at 18:45
Stage
is initialized by the JavaFX launcher. It is used to show the application window.– ItachiUchiha
Aug 27 '17 at 18:45
add a comment |
The String
array passed to the main()
method are the parameters of the application, not specifically to the JavaFX module if you arbitrarily choose to use JavaFX.
The easiest solution could be to store the argumets for later use (e.g. static attribute next to the main()
method, and a static getter method to access it).
add a comment |
The String
array passed to the main()
method are the parameters of the application, not specifically to the JavaFX module if you arbitrarily choose to use JavaFX.
The easiest solution could be to store the argumets for later use (e.g. static attribute next to the main()
method, and a static getter method to access it).
add a comment |
The String
array passed to the main()
method are the parameters of the application, not specifically to the JavaFX module if you arbitrarily choose to use JavaFX.
The easiest solution could be to store the argumets for later use (e.g. static attribute next to the main()
method, and a static getter method to access it).
The String
array passed to the main()
method are the parameters of the application, not specifically to the JavaFX module if you arbitrarily choose to use JavaFX.
The easiest solution could be to store the argumets for later use (e.g. static attribute next to the main()
method, and a static getter method to access it).
answered Jul 7 '14 at 13:37
iczaicza
179k25361393
179k25361393
add a comment |
add a comment |
You can set the Controller in the MainStage class. But you'll have to do it static, otherwise it will be null.
Hava a look at the code:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
MainStage ms = new MainStage();
ms.setController(controller);
Application.launch(MainStage.class, (java.lang.String) null);
}
}
public class MainStage extends Application {
private static Controller controller;
public void start(Stage primaryStage) throws Exception {
System.out.println(controller);
primaryStage.show();
}
public void setController(Controller controller){
this.controller = controller;
}
}
add a comment |
You can set the Controller in the MainStage class. But you'll have to do it static, otherwise it will be null.
Hava a look at the code:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
MainStage ms = new MainStage();
ms.setController(controller);
Application.launch(MainStage.class, (java.lang.String) null);
}
}
public class MainStage extends Application {
private static Controller controller;
public void start(Stage primaryStage) throws Exception {
System.out.println(controller);
primaryStage.show();
}
public void setController(Controller controller){
this.controller = controller;
}
}
add a comment |
You can set the Controller in the MainStage class. But you'll have to do it static, otherwise it will be null.
Hava a look at the code:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
MainStage ms = new MainStage();
ms.setController(controller);
Application.launch(MainStage.class, (java.lang.String) null);
}
}
public class MainStage extends Application {
private static Controller controller;
public void start(Stage primaryStage) throws Exception {
System.out.println(controller);
primaryStage.show();
}
public void setController(Controller controller){
this.controller = controller;
}
}
You can set the Controller in the MainStage class. But you'll have to do it static, otherwise it will be null.
Hava a look at the code:
public class MainEntry {
public static void main(String args) {
Controller controller = new Controller();
MainStage ms = new MainStage();
ms.setController(controller);
Application.launch(MainStage.class, (java.lang.String) null);
}
}
public class MainStage extends Application {
private static Controller controller;
public void start(Stage primaryStage) throws Exception {
System.out.println(controller);
primaryStage.show();
}
public void setController(Controller controller){
this.controller = controller;
}
}
answered Oct 10 '15 at 18:57
mjaquemjaque
33937
33937
add a comment |
add a comment |
Of course there is a need and possibility to pass parameters to JavaFX application.
I did it to run my JavaFX client from different places, where different network configurations are required (direct or via proxy). Not to make instant changes in code, I implemented several network configurations to be chosen from in application run command with parameter like --configurationIndex=1. The default code value is 0.
List<String> parameters;
int parameterIndex;
String parameter;
parameters =
getParameters().getRaw();
for (parameterIndex = 0;
parameterIndex < parameters.size();
parameterIndex++) {
parameter =
parameters.get(
parameterIndex);
if (parameter.contains("configurationIndex")) {
configurationIndex =
Integer.valueOf(
parameters.get(parameterIndex).
split("=")[1]);
}
}
In Netbeans you can set this parameter for debugging need directly on your project: Project - Properties - Run - Parameters - insert --configurationIndex=1 into field.
add a comment |
Of course there is a need and possibility to pass parameters to JavaFX application.
I did it to run my JavaFX client from different places, where different network configurations are required (direct or via proxy). Not to make instant changes in code, I implemented several network configurations to be chosen from in application run command with parameter like --configurationIndex=1. The default code value is 0.
List<String> parameters;
int parameterIndex;
String parameter;
parameters =
getParameters().getRaw();
for (parameterIndex = 0;
parameterIndex < parameters.size();
parameterIndex++) {
parameter =
parameters.get(
parameterIndex);
if (parameter.contains("configurationIndex")) {
configurationIndex =
Integer.valueOf(
parameters.get(parameterIndex).
split("=")[1]);
}
}
In Netbeans you can set this parameter for debugging need directly on your project: Project - Properties - Run - Parameters - insert --configurationIndex=1 into field.
add a comment |
Of course there is a need and possibility to pass parameters to JavaFX application.
I did it to run my JavaFX client from different places, where different network configurations are required (direct or via proxy). Not to make instant changes in code, I implemented several network configurations to be chosen from in application run command with parameter like --configurationIndex=1. The default code value is 0.
List<String> parameters;
int parameterIndex;
String parameter;
parameters =
getParameters().getRaw();
for (parameterIndex = 0;
parameterIndex < parameters.size();
parameterIndex++) {
parameter =
parameters.get(
parameterIndex);
if (parameter.contains("configurationIndex")) {
configurationIndex =
Integer.valueOf(
parameters.get(parameterIndex).
split("=")[1]);
}
}
In Netbeans you can set this parameter for debugging need directly on your project: Project - Properties - Run - Parameters - insert --configurationIndex=1 into field.
Of course there is a need and possibility to pass parameters to JavaFX application.
I did it to run my JavaFX client from different places, where different network configurations are required (direct or via proxy). Not to make instant changes in code, I implemented several network configurations to be chosen from in application run command with parameter like --configurationIndex=1. The default code value is 0.
List<String> parameters;
int parameterIndex;
String parameter;
parameters =
getParameters().getRaw();
for (parameterIndex = 0;
parameterIndex < parameters.size();
parameterIndex++) {
parameter =
parameters.get(
parameterIndex);
if (parameter.contains("configurationIndex")) {
configurationIndex =
Integer.valueOf(
parameters.get(parameterIndex).
split("=")[1]);
}
}
In Netbeans you can set this parameter for debugging need directly on your project: Project - Properties - Run - Parameters - insert --configurationIndex=1 into field.
answered Nov 9 '16 at 16:56
ZonZon
6,16744659
6,16744659
add a comment |
add a comment |
case 1 = java standard types - transmit them as java Strings "--name=value" and then convert them to the final destination using the answer of dmolony
for ( Map.Entry<String, String> entry : namedParameters.entrySet ()){
System.out.println (entry.getKey() + " : " + entry.getValue ());
switch( entry.getKey()){
case "media_url": media_url_received = entry.getValue(); break;
}
}
The parameter is created at Application.launch and decoded at init
String args = {"--media_url=" + media_url, "--master_level=" + master_level};
Application.launch( args);
case 2 = If you have to transmit java objects use this workaround (this is for only one javafx Application launch, create a Map of workarounds and send index as strings if you have a complex case)
public static Transfer_param javafx_tp;
and in your class init set the instance of object to a static inside it's own class
Transfer_param.javafx_tp = tp1;
now you can statically find your last object for working with only one JavaFx Applications (remember that if you have a lot of JavaFx applications active you should send a String with a static variable identification inside a Map or array so you do not take a fake object address from your static structures (use the example at case 1 of this answer to transmit --javafx_id=3 ...))
add a comment |
case 1 = java standard types - transmit them as java Strings "--name=value" and then convert them to the final destination using the answer of dmolony
for ( Map.Entry<String, String> entry : namedParameters.entrySet ()){
System.out.println (entry.getKey() + " : " + entry.getValue ());
switch( entry.getKey()){
case "media_url": media_url_received = entry.getValue(); break;
}
}
The parameter is created at Application.launch and decoded at init
String args = {"--media_url=" + media_url, "--master_level=" + master_level};
Application.launch( args);
case 2 = If you have to transmit java objects use this workaround (this is for only one javafx Application launch, create a Map of workarounds and send index as strings if you have a complex case)
public static Transfer_param javafx_tp;
and in your class init set the instance of object to a static inside it's own class
Transfer_param.javafx_tp = tp1;
now you can statically find your last object for working with only one JavaFx Applications (remember that if you have a lot of JavaFx applications active you should send a String with a static variable identification inside a Map or array so you do not take a fake object address from your static structures (use the example at case 1 of this answer to transmit --javafx_id=3 ...))
add a comment |
case 1 = java standard types - transmit them as java Strings "--name=value" and then convert them to the final destination using the answer of dmolony
for ( Map.Entry<String, String> entry : namedParameters.entrySet ()){
System.out.println (entry.getKey() + " : " + entry.getValue ());
switch( entry.getKey()){
case "media_url": media_url_received = entry.getValue(); break;
}
}
The parameter is created at Application.launch and decoded at init
String args = {"--media_url=" + media_url, "--master_level=" + master_level};
Application.launch( args);
case 2 = If you have to transmit java objects use this workaround (this is for only one javafx Application launch, create a Map of workarounds and send index as strings if you have a complex case)
public static Transfer_param javafx_tp;
and in your class init set the instance of object to a static inside it's own class
Transfer_param.javafx_tp = tp1;
now you can statically find your last object for working with only one JavaFx Applications (remember that if you have a lot of JavaFx applications active you should send a String with a static variable identification inside a Map or array so you do not take a fake object address from your static structures (use the example at case 1 of this answer to transmit --javafx_id=3 ...))
case 1 = java standard types - transmit them as java Strings "--name=value" and then convert them to the final destination using the answer of dmolony
for ( Map.Entry<String, String> entry : namedParameters.entrySet ()){
System.out.println (entry.getKey() + " : " + entry.getValue ());
switch( entry.getKey()){
case "media_url": media_url_received = entry.getValue(); break;
}
}
The parameter is created at Application.launch and decoded at init
String args = {"--media_url=" + media_url, "--master_level=" + master_level};
Application.launch( args);
case 2 = If you have to transmit java objects use this workaround (this is for only one javafx Application launch, create a Map of workarounds and send index as strings if you have a complex case)
public static Transfer_param javafx_tp;
and in your class init set the instance of object to a static inside it's own class
Transfer_param.javafx_tp = tp1;
now you can statically find your last object for working with only one JavaFx Applications (remember that if you have a lot of JavaFx applications active you should send a String with a static variable identification inside a Map or array so you do not take a fake object address from your static structures (use the example at case 1 of this answer to transmit --javafx_id=3 ...))
edited Dec 20 '18 at 10:08
answered Dec 20 '18 at 9:58
DorinDorin
197
197
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24611789%2fhow-to-pass-parameters-to-javafx-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why can't you simply instantiate the controller inside your application? Btw.
MainStage
doesn't seem to be the right naming for your main application asStage
has a different meaning in JavaFX.– isnot2bad
Jul 7 '14 at 13:38