How to have constructor with arguments for controller?

I would like to have the controller have arguments so that they can be a precondition for the object being constructed and not in setters to be added later on. This will reduce errors.
Unfortunately, fxml only seems to take the no-arg constructor. Is there another way?
Edited by: likejiujitsu on Apr 20, 2013 10:30 AM

Here's perhaps a more realistic example. I have a Main.fxml file with a MainController. The event handler for a button in the Main.fxml file is going to load another fxml file (Dialog.fxml) and display the content in a dialog. The controller associated with Dialog.fxml (DialogController) is going to be initialized by passing a String into the constructor; that String value is retrieved from a text field in Main.fxml.
Main.java:
package example;
import java.io.IOException;
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 IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
    primaryStage.setScene(new Scene(root, 400, 200));
    primaryStage.show();
  public static void main(String[] args) {
    launch(args);
}Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://javafx.com/fxml" spacing="10"
     fx:controller="example.MainController" fx:id="mainRoot">
     <HBox>
          <Label text="Enter text:" />
          <TextField fx:id="textField" />
     </HBox>
     <Button text="Show Dialog" onAction="#showDialog" />
     <!-- TODO Add Nodes -->
</VBox>MainController.java:
package example;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;
public class MainController {
  @FXML
  private TextField textField;
  @FXML
  private Parent mainRoot;
  private final FXMLLoader dialogLoader;
  public MainController() {
    dialogLoader = new FXMLLoader(
        MainController.class.getResource("Dialog.fxml"));
    dialogLoader.setControllerFactory(new Callback<Class<?>, Object>() {
      @Override
      public Object call(Class<?> cls) {
        if (cls == DialogController.class) {
          return new DialogController(textField.getText());
        } else {
          try {
            return cls.newInstance();
          } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
  @FXML
  private void showDialog() throws IOException {
    dialogLoader.setController(null);
    dialogLoader.setRoot(null);
    Parent dialogRoot = (Parent) dialogLoader.load();
    Scene scene = new Scene(dialogRoot, 300, 150);
    Stage dialog = new Stage();
    dialog.initModality(Modality.APPLICATION_MODAL);
    dialog.initOwner(mainRoot.getScene().getWindow());
    dialog.setScene(scene);
    dialog.show();
}Dialog.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="example.DialogController"
     fx:id="dialogRoot">
     <Label fx:id="label" />
     <HBox>
          <Button text="Click Me" onAction="#click" />
          <Button text="Close" onAction="#close" />
     </HBox>
</VBox>DialogController.java
package example;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Label;
public class DialogController {
  private final String text;
  @FXML
  private Label label;
  @FXML
  private Parent dialogRoot;
  public DialogController(String text) {
    this.text = text;
  public void initialize() {
    label.setText(text);
  @FXML
  private void click() {
    System.out.println("Text is " + text);
  @FXML
  private void close() {
    dialogRoot.getScene().getWindow().hide();
}Edited by: James_D on Apr 22, 2013 8:00 PM

Similar Messages

  • I have been with google for while now but I still can't access to google wallet, Would you please show me how? thanks

    I HAVE BEEN WITH GOOGLE FOR WHILE NOW BUT I STILL CAN'T ACCESS TO MY GOOGLE WALLET, BOTH ACCOUNTS, PERSONAL AS WELL AS BUSINESS. WOULD YOU PLEASE SHOW ME HOW? THANKS

    Seems like that would be a better question for a Google forum since it it Google's app. Here is a link about it from Google:
    https://www.google.com/wallet/
    If you need further help, I would suggest you post to a Google forum.
    GB

  • Why Do We Need Constructor With Arguments?

    I understand that constructor is used to create instances.
    We can have constructors with 0 argument or with one/multiple arguments. Why do we need a contructor with arguments? What is the purpose of doing it?
    Thank you for your help.

    There are three general ways to provide constructors, like you said:
    1) Default constructor (no arguments)
    2) One or more constructors with arguments
    3) One or more constructors with arguments as well as a default constructor
    From a design standpoint, the idea is to give as much information needed for constructing an object up front. Look at the standard Java API for some examples (and because I couldn't find a standard class with just a default constructor, one made up):
    public class Foo {
      private long createTime;
      public Foo() {
        createTime = System.currentTimeMillis();
      public String toString() {
        return "This object was created at " + createTime;
    }This code has no reason to take any arguments. It does all construction on its own and already has all the information it needs. On the other hand, look at BufferedReader. There's no possible way this object can ever be constructed without an argument. What's it buffering? It doesn't make sense to ever instantiate new BufferedReader(). It'd be useless. So BufferedReader has no default (no-arg) constructor.
    Sometimes (very often in Java) you want to do both. Let something be instantiated with no arguments, but also provide the option of creating something with more specific details. A good example is ArrayList, which has three constructors:
    public ArrayList() --> Construct an empty ArrayList with the default initial capacity;
    public ArrayList(Collection c) --> Construct an ArrayList populated with the contents of another collection;
    public ArrayList(int capacity) --> Construct an empty ArrayList with a specific initial capacity.
    Sometimes it makes sense just to use the default. Sometimes you want more control over the behavior. Those are the times to use multiple constructors.
    Hope this helps!

  • What a horrible way to treat customers. I was due for updates on my phones and thought I should maybe check some other prices but have been with Verizon for 20 yrs so ended up there. I got 2 new IPhones 5s and the wife couldn't decide what she wanted so s

    What a horrible way to treat customers. I was due for updates on my phones and thought I should maybe check some other prices but have been with Verizon for 20 yrs so ended up there. I got 2 new IPhones 5s and the wife couldn’t decide what she wanted so she stayed with old phone but this locked me into a new 2 yr contract . Within a wk one of the new iPhones started turning on and of like 10 –15 times a day. Wk 2 it turned off and would not turn on again . It was my sons who was in college at the time so we talked and he took it in to a Verizon store where he was told I need to come in since the account was in my name. He was away from home , Verizon could see on account it was his phone they said it was not abused, they could not even turn it on , they sold me the phone give him another phone maybe even a loaner till he gets another. So now I need to go to store and explain to 3 different people this is my phone and my son has it and I need a new one. what a waste of time after about 2 hrs and talking to different people and yes they said there were notes on his account from other person he had talked to from different store. So I finally walk out with a receipt in hand and being told I would have a new phone in a couple days. As I sat in my vehicle thinking this is stupid I looked at my receipt and noticed it said droid on it so back into Verizon I went. The salesman said that’s what your son has on his account. My son had activated a friends old phone so he has one since VERIZON REFUSED to give him one. Another hr 3 people and yes they can see he had a new iPhone and notes on it from other store. Sometimes sorry just doesn't do it. I was now late for a appointment . Now I walk out and have been told I will get a new iPhone in the mail in about 2 wk Yes 2 wks   again I bought it there just give me a new one and you send old one back. I will also get a new droid that I have to send back because they said they cant cancel it  . In about 3 days I get the Droid and sent it back Verizon mistake and a waste of my time . After waiting over 2 wks and not receiving a new iPhone back to Verizon I went . I am now bitter at Verizon after doing 20 yrs of business with them. 3 people 1 hr later I was told it got delivered to my post office . I got the tracking num called the post office and they say o yes that was the droid. Go back in to Verizon another hr of explaining I walk out being told I will get a new iPhone in a couple of days, We’ll see . Once again I got the phone in your store just give me a new one . Do you realize how much of my time you have wasted ? do you care? O what is Your Policy ? How many Billion did you make last year ? I am Locked into a new 2 yr contract . Why don’t you just release me ? After 20 yrs do you think I will ever renew my contract ?

    Simple process. If an iPhone go to the Apple Store and not Verizon
    the phone from Verizon will be a refurbished device and not new unless under the 14 day worry free guarantee
    good luck

  • I need advise and help with this problem . First , I have been with Mac for many years ( 14 to be exact ) I do have some knowledge and understanding of Apple product . At the present time I'm having lots of problems with the router so I was looking in to

    I need advise and help with this problem .
    First , I have been with Mac for many years ( 14 to be exact ) I do have some knowledge and understanding of Apple product .
    At the present time I'm having lots of problems with the router so I was looking in to some info , and come across one web site regarding : port forwarding , IP addresses .
    In my frustration , amongst lots of open web pages tutorials and other useless information , I come across innocent looking link and software to installed called Genieo , which suppose to help with any router .
    Software ask for permission to install , and about 30 % in , my instinct was telling me , there is something not right . I stop installation . Delete everything , look for any
    trace in Spotlight , Library . Nothing could be find .
    Now , every time I open Safari , Firefox or Chrome , it will open in my home page , but when I start looking for something in steed of Google page , there is
    ''search.genieo.com'' page acting like a Google . I try again to get raid of this but I can not find solution .
    With more research , again using genieo.com search eng. there is lots of articles and warnings . From that I learn do not use uninstall software , because doing this will install more things where it come from.
    I do have AppleCare support but its to late to phone them , so maybe there some people with knowledge , how to get this of my computer
    Any help is welcome , English is my learned language , you may notice this , so I'm not that quick with the respond

    Genieo definitely doesn't help with your router. It's just adware, and has no benefit to you at all. They scammed you so that they could display their ads on your computer.
    To remove it, see:
    http://www.thesafemac.com/arg-genieo/
    Do not use the Genieo uninstaller!

  • You have specified invalid arguments for the program

    Can someone please correct this command line for me.
    i always have errror like this
    $FNDCPPUR apps/apps 0 Y ENTITY=ALL AGE=10
    You have specified invalid arguments for the program
    and nothing happens
    Thanks in advance!

    To submit the concurrent program from the OS use CONCSUB.
    Refer to the following notes for CONCSUB syntax:
    Note: 457519.1 - How to Submit a Concurrent Request Using CONCSUB Syntax
    https://metalink.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=457519.1
    Note: 146856.1 - How to Run a Concurrent Job through Operating System
    https://metalink.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=146856.1

  • I have problem with fonts for my site, i have used "Lucida sans unicode " family for certain texts. it shows perfect in mozilla 3.5 and mozilla 4. But the font not supporting in mozilla 5.0? please help me

    i have problem with fonts for my site, i have used "Lucida sans unicode " family for certain texts. it shows perfect in mozilla 3.5 and mozilla 4. But the font not supporting in mozilla 5.0? please help me

    i have problem with fonts for my site, i have used "Lucida sans unicode " family for certain texts. it shows perfect in mozilla 3.5 and mozilla 4. But the font not supporting in mozilla 5.0? please help me

  • I have problems with office for mac  screen resolution, specially with excel

    I have problems with office for mac  screen resolution, specially with excel ?

    For starters, make sure to Check for Updates on any of the Help menus, and make sure the product has all the latest patches. MS did come out with a patch addressing the display issues on Retina Macs. Latest patchlevel is 14.3.2.
    We are talking about Office:Mac 2011, right?

  • How to i lock with password for my contacts folder in ipad3

    how to i lock with password for my contacts folder in ipad3

    You cannot lock specific applications or folders, but you can set a passcode on your iPad:
    Setting a Passcode on an iOS Device

  • [Forum FAQ] How to sync time with a Domain Controller for a standalone server

    As we all known, if a computer belongs to an Active Directory domain, it will sync the time automatically by using the Windows Time service that is available on Domain Controllers.
    While a standalone server will synchronize with its local hardware time and Windows time server. (Figure 1)
    Figure 1.
    Under some circumstances, a standalone server is necessary in a product environment. We can sync the time of this standalone server with the Domain Controller using
    the steps below:
    1. Modified the value of the AnnounceFlags:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config
    Under this entry we can see the default value of AnnounceFlags is 10 (Decimal), we configure the value as 5 (Decimal). (Figure 2)
    Figure 2.
    2. Confirm the value of the registry key below is set to 0:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 
    Figure 3.
    3. Configure the standalone server to synchronize with a specific time source (Domain Controller).
    In our test, we configured our Domain Controller (192.168.10.200) as the time source. Used the following commands:
    w32tm /config /syncfromflags:manual /manualpeerlist:192.168.10.200
    4. Sync the time with the Domain Controller using the command below:
    w32tm /config /update
    From the figure below (Figure 4), you can see the after we did all the steps above, the time on the standalone server was synced with the Domain Controller.
    Figure 4.
    (Note: Peerlist is a separated list of DNS servers, or IP Addresses for the time servers)
    More information:
    Windows Time Service Tools and Settings
    http://technet.microsoft.com/en-us/library/cc773263(WS.10).aspx#w2k3tr_times_tools_dyax
    Please click to vote if the post helps you. This can be beneficial to other community members reading the thread.

    Thank you for the instruction! I am sure it is one of the scenarios that majority of administrators will run into. So I suggest to write a wiki about it and publish it for this month's TechNet Guru in Windows Server section. This month's TechNet Guru can
    be found here:
    Calling All Wise Men! Windows
    Server Gurus Needed! Apply Within! No One Turned Away!
    Thanks for your informative post. :)
    Regards.
    Mahdi Tehrani   |  
      |  
    www.mahditehrani.ir
    Please click on Propose As Answer or to mark this post as
    and helpful for other people.
    This posting is provided AS-IS with no warranties, and confers no rights.
    How to query members of 'Local Administrators' group in all computers?

  • How to have a unique value for each record??

    could any 1 help me out in this...
    I want to have a column name 'Order No' which should be unique.
    How to generate a unique value for each record.??

    could any 1 help me out in this...
    I want to have a column name 'Order No' which should be unique.
    How to generate a unique value for each record.?? If you are using SQL PLUS to create the table try something like
    this:
    CREATE TABLE ORDER_TEST (
    ORD_NO NUMBER (8) NOT NULL PRIMARY KEY,
    ORDERDATE DATE,
    CUSTID NUMBER (8) NOT NULL,
    SHIPDATE DATE,
    TOTAL NUMBER (8,2) CONSTRAINT TOTAL_ZERO CHECK
    (TOTAL >= 0),
    CONSTRAINT ORD_FOREIGN_KEY FOREIGN KEY (CUSTID) REFERENCES
    CUSTOMER (CUSTID),
    CONSTRAINT ORD_UNIQUE UNIQUE (ORD_NO)
    -- or a simpler table example
    DROP TABLE ORDER_TEST;
    CREATE TABLE ORDER_TEST (
    ORD_NO NUMBER (8),
    ORDERDATE DATE,
    CUSTID NUMBER (8) NOT NULL,
    SHIPDATE DATE,
    TOTAL NUMBER (8,2) CONSTRAINT TOTAL_ZERO CHECK
    (TOTAL >= 0),
    CONSTRAINT ORD_UNIQUE UNIQUE (ORD_NO)
    note: ORD_NO can also be a primary key
    If you are doing the INSERT during runtime from a form first
    create a sequence in SQL PLUS to handle the ORD_NO value:
    Create SEQUENCE ORDERNO_UNIQUEVAL_sqnc
    START WITH 000001
    NOMAXVALUE
    NOCACHE;
    and reference it as the ORD_NO parameter in your INSERT
    statement:
    ORDERNO_UNIQUEVAL_sqnc.NEXTVAL
    note: to maintain data integrity you must use the sequence
    everytime you insert a new order to table. To start a new
    sequence drop the sequence and re-create it with whatever "START
    WITH" value you want.
    Hope this helps
    Kevin

  • How to have specific shipping method for only international customers?

    In the old BC we had it set up with individual shipping methods Based on a shipping mentod flat for AU/NZ  and another All other countries not AU?NZ.
    In tests it worked because of the desination shipping address specified a country. (at least that appeared to be the case )
    This seemes to have changed ( when ? ) and now checkout only displays the Australian shipping options to the "other countries customers"  who should only see the shipping option of International (all countries not au and NZ).
    Yes - There are issues with the dashboard shipping method selector, but we do have the shipping methods configured correctly.
    We don't have separate currency prices for other countries or language pages etc, just simply 1 site selling in $AU priced products, but needs to have an international shipping option appear at checkout to those customer countries listing in the international shipping method list, and not see the AU shipping method.
    Currently all AU ? NZ customers see correct AU shipping choices at checkout
    Other countries customers don't see the international shipping choice at checkout, but do see the AU / NZ shipping displayed
    Any one else had this issue? Advise appreciated, not sure how to get this functional for international customers checkout, thanks

    (not an answer but we need this too. Especially one where we can remove the tax from the overseas orders.).. Just subscribing to this topic

  • How to sync iPhone with iTunes for first time without setting up as a new iPhone

    I have an iPhone 5 that I have never sync:ed with iTunes on my Mac, I have just set it up and purchased apps etc from the iPhone itself.
    Now I would like to sync with iTunes for the first time with the iPhone but when I connect it, iTunes asks me if I want to setup the iPhone as a new iPhone (I assume form scratch) or from a backup from other iPhones that have been sync:ed with my Mac.
    Now I dont want to setup this iPhone from scratch and erase everything thats on it, I just want to sync with my Mac, download apps, music etc. How do I do that and get beyoind the first screen that asks about setting up as a new iPhone ?
    /Kjell

    Hi I think I've found a workarround. I'm currently copying all files on my iPhone to the new PC using this program: http://www.getsharepod.com/download/
    After all files are copied I think I can enable sync in the new iTunes and delete the iPhone, then add the copied files in new playlists.
    I'll let you know.

  • Mpeg2 files converted to DV files..how do I "share" with iDVD for burning?

    Newbie question here...thanks for all the great info I have found here already...but need some more. I'm using an iMac with OS X 10.4.7, QT Pro, downloaded MPEG StreamClip, and purchased MPEG Playback as suggested on this board.
    Here's the situation:
    I have analog Hi8 Sony camcorder (CCD-TRV98) with NO USB, firewire, etc. connection...just good ol' SVideo. Was able to "capture" old home movies using analog to digital coverter through my Windows Desktop. Saved files in mpeg formats and avi formats on external hard drive.
    Plugged external hard drive via firewire port on iMac. I can play the movie files in Streamclip and can "convert" to DV files...and I can import them into iMovie fine....but when I try then "share" with iDVD the program encodes for literally hours....i finally gave up for a tiny movie after 2 hours of "encoding"....think this is a problem with my rookie status of using iMovie, or are my files a problem? Does the fact that I'm saving the files off a Desktop and using on Mac matter? If I'm converting the files to DV in Mac, would the Desktop influence even matter? (Are you rolling your eyes at me yet? R-O-O-K-I-E here and I know it, but hey I'm trying!
    Hope I have explained my situation well...if not let me know what I left out. I could really use the help...I have 30+ Hi8 movies that I want to burn to DVD for family and archiving purposes.
    Thanks,
    iMac   Mac OS X (10.4.7)  
    iMac   Mac OS X (10.4.7)   iDVD,

    the combo Streamclip + Apple plug-in is the most recommended here...
    yepp, iMovie "prefers" dv only... relying on the QTengine, it can import some other formats/codecs... as mpeg4, you could be lucky, to make it accept an .avi container with the right codec inside...
    .avi is a PC media container, as .mov in the Apple world: just a "wrapper", containing maning codecs, formats, standards, not-so-standards...
    so, if your Windows app allows export as ".avi, codec dv", it should work... there's no 100% gurantee with that PCMac swapping....
    how can I get iMovie to "share" with iDVD for burning? iDVD will not open the movie made in iMovie....?
    now that a new - and surprising! - topic...
    you have some options, how to share to iDVD:
    * inside iM there's a iDVD button under the pane... (I don't own iM6...)
    * you should safe your project into the Movies folder, then launch iDVD, create new project, in the media bin/movie you should find your movie...
    * create new iDVDproject and drag'n drop project from Finder/Movies into iDVD window....
    I asked you, to tell us your free disk space on internal drive... see my last post... please tell us... , a 2min project should be rendered in a few minutes...
    did you read the turtorials before using the iApps?
    http://www.apple.com/ilife/tutorials
    coming from the PC world, some workflows differ..
    we MacUser say: are easier to accomplish.. ;-))
    Plan B)
    ... in case, you own a miniDV camcorder, playout files from PC to tape, then import as usual into iM/iDVD... digital copies, no loss in quality..
    good luck
    thanks for usage of forum's marker feature ...!

  • How to program Listener with arguments

    Hi, I want to program a generic Listener to respond to different calling programs. How do I pass an argument to the Listener?
    I have
    public class myNavigationBarListener implements NavigationBarButtonClickListener{}
    When I added
    public class myNavigationBarListener (int i)implements NavigationBarButtonClickListener{}
    it won't compile, thanks in advance.

    I have the following class that implements an action listener.
    public class ComboBoxDefaultEditor extends JComboBox implements TableCellEditor, ActionListener, JComboBox.KeySelectionManager {
    Then in the constructor:
    public ComboBoxDefaultEditor(RowSetInfo ComboRowSet, String FieldName) {
    Hope this helps!
    Linda
    null

Maybe you are looking for