Skip to main content

Initialization Concepts

3 replies [Last post]
cdouglass
Offline
Joined: 2005-08-24
Points: 0

I'm having some difficulty with the concepts in JavaFX, particularly of initialization.

Suppose I want to track the x, y location of the frame window. I start with something like:

<br />
var model = Model {<br />
  public attribute frameX: Integer;<br />
  public attribute frameY: Integer;<br />
}</p>
<p>var frame = Frame {<br />
  x: bind model.frameX with inverse;<br />
  y: bind model.frameY with Inverse;<br />
  content: ...<br />
}</p>
<p>

Ignoring for the moment that I need to save these values when they change (perhaps using "replace" semantics) how do I load them at initialization? Where, in this declarative script, do I place functional code?

In Flex there is an "onCreationComplete" tag where I would code this, but I don't see anything similar in JavaFX.

Thanks.

Chas Douglass

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cdouglass
Offline
Joined: 2005-08-24
Points: 0

Ok, some more research reveals "postinit", but I'm still not understanding how this stuff works.

My first thought is that it would be an attribute I could modify, so changing my above code might work:

[code]
var frame = Frame {
x: bind model.frameX with inverse;
y: bind model.frameY with Inverse;
postinit: function() {
// retrieve prefs here
}
content: ...
}
[/code]

But the compiler gets confused by the "keyword" "postinit".

I can get it working with the following circumlocution, but surely there is a more direct/elegant/easy path?

[code]
class MyFrame extends Frame {
public static function initer() {
return MyFrame {
x: bind model.frameX with inverse;
y: bind model.frameY with inverse;
content: ...
}
}
postinit {
// load prefs here
}
}

var frame = MyFrame.initer();

[/code]

michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

First of all, I would like to rewrite your code example a little, just to make it more clear, what is happening.

[code]
class Model {
public attribute frameX: Integer;
public attribute frameY: Integer;
};

var model = Model {};
[/code]

This will define a class Model and create an instance of that class, which is stored in the local variable model.

To initialize the attributes, there are two main possibilities. The first one is to define default-values in the class-definition:

[code]
class Model {
public attribute frameX: Integer = 100;
public attribute frameY: Integer = 100;
};
[/code]

The second option is to set the attributes during instanciation in what is called an object-literal:

[code]
var model = Model {frameX: 200, frameY: 200}; // this will overwrite the default values
[/code]

There are other options, to set the value, but I would go with one of the solutions above. For example you can simply change the attributes of the local variable model.

[code]
var model = Model {};
model.frameX = 300;
model.frameY = 300;
[/code]

And, as you already found out, there is the postinit-construct to help during initialization. It is defined in the context of a class.

[code]
class Model {
public attribute frameX: Integer = 100;
public attribute frameY: Integer = 100;
postinit {
// do complex initialization here
};
};
[/code]

The code within the postinit-construct is executed everytime a new Model is instanciated. You can use it for complex initialization, but I wouldn't use it to set frameX and frameY. Maybe it helps to think of it as something like a constructor in Java, although it is different (it does not take parameters; it is not even aware of which attributes have been changed in the object-literal.)

cdouglass
Offline
Joined: 2005-08-24
Points: 0

Thanks that was very helpful.

I'm thinking of the 3rd option (postinit), since what I want to do is retrieve preferences stored between sessions of window size and location.

Chas Douglass