JavaFX in simple and simple (VI.) bindings and triggers

Source: Internet
Author: User
Tags bind

Think about in Java, how do we use the observer pattern to make the view reflect the changes in the model? Observers, observers, registering listeners, events, callbacks, getting data, etc. are you having trouble with them?

With the binding, we can directly bind an attribute of the observer to one of the attributes of the observer, synchronizing the changes, saving a number of steps in the middle, and in the following example, I bound the transparency of a circle and the value of a text to a value of a slider. See what happens when you run in your browser (note: Java plug-ins are required, otherwise no effect is seen)

A slider

var slider = SwingSlider {
minimum: 0
maximum: 100
value: 80
vertical: false

};

//主窗体
Stage {
title: "MyApp"
scene: Scene {
width: 200
height: 200
content: [
VBox{
spacing: 10
content: [

//加入滑块
slider,

//加入一个圆
Circle {
centerX: 100
centerY: 50
radius: 40
fill: Color.RED
//将透明度和滑块的值绑定
opacity: bind {
slider.value / 100.0
}
},

//加入一个文本
Text {
font: Font {
size: 24
}
x: 10,
y: 30
// 将文本内容和滑块的值绑定
content: bind "{slider.value}"
}
]
}
]

} 
};

Bind can be binding not only to variables, but also to expressions, functions, and so on, and can be bound in both directions.

As for triggers, personal sensations are similar to a Setter, and when you assign a variable, do something extra, because when you create an object in a literal way it is impossible to invoke any function, setting this language feature may be more of a consideration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.