17.6 java FX 속성 감시와 바인딩

17.6.1 속성 감시



Appmain.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sec06.exam01_property_listener;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
public class AppMain extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = (Parent)FXMLLoader.load(getClass().getResource("root.fxml"));
        Scene scene = new Scene(root);
        
        primaryStage.setTitle("AppMain");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
cs


root.fxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
 
<BorderPane xmlns:fx="http://javafx.com/fxml"
                fx:controller="sec06.exam01_property_listener.RootController"
                prefHeight="250.0" prefWidth="350.0" >
   <center>
      <Label fx:id="label" text="JavaFX" >
         <font>
            <Font size="0" /> // Label의 기본 폰트 크기는 0
         </font>
      </Label>
   </center>
   <bottom>
      <Slider fx:id="slider" />
   </bottom>   
</BorderPane>
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package sec06.exam01_property_listener;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.text.Font;
 
public class RootController implements Initializable {
    @FXML private Slider slider;
    @FXML private Label label;
 
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        slider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<extends Number> observable, Number oldValue, Number newValue) {
                label.setFont(new Font(newValue.doubleValue())); // Label의 폰트 변경, Silder의 변경된 Value



            }
        });
    }
}
cs


17.6.2 속성 바인딩



RootController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sec06.exam02_property_bind;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
 
public class RootController implements Initializable {
    @FXML private TextArea textArea1;
    @FXML private TextArea textArea2;
 
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Bindings.bindBidirectional(textArea1.textProperty(), textArea2.textProperty());
    }
}
cs

root.fxml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
 
 
<VBox xmlns:fx="http://javafx.com/fxml" 
        fx:controller="sec06.exam02_property_bind.RootController"
        prefHeight="200.0" prefWidth="300.0" spacing="10.0" >
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
   <children>
      <Label text="textArea1" />
      <TextArea fx:id="textArea1"/>
      <Label text="textArea2" />
      <TextArea fx:id="textArea2"/>
   </children>
</VBox>
cs


17.6.3 Bindings 클래스



1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
 
<AnchorPane xmlns:fx="http://javafx.com/fxml" 
                fx:id="root" 
                fx:controller="sec06.exam03_bindings.RootController"
                prefHeight="200.0" prefWidth="300.0" >
   <children>
      <Circle fx:id="circle" fill="blue" radius="50.0" stroke="BLACK" />
   </children>
</AnchorPane>
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package sec06.exam03_bindings;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Circle;
 
public class RootController implements Initializable {
    @FXML private AnchorPane root;
    @FXML private Circle circle;
 
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        circle.centerXProperty().bind(Bindings.divide(root.widthProperty(), 2)); // 단방향 바인딩
        circle.centerYProperty().bind(Bindings.divide(root.heightProperty(), 2));    
    }
}
cs




Posted by 너래쟁이
: