━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 18. Binding, ProgressBar and ProgressIndicator

Binding 은 두 컨트롤의 properties 를 연결하는 것이다. 바인드 된 속성들은 하나의 속성값이 변화하면 자동으로 다른 속성값도 변화한다. 


단방향 바인드 - bind()

양방향 바인드 - bindBidirectional()

바인드 해제 - unbind()



1. Main.java


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
28
29
package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = new FXMLLoader().load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.layout.AnchorPane?>
 
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <Button layoutX="50.0" layoutY="76.0" mnemonicParsing="false" onAction="#BtnClick" text="- Button" />
      <Label fx:id="lblStatus" alignment="CENTER" layoutX="91.0" layoutY="14.0" prefHeight="62.0" prefWidth="121.0" text="Label" />
      <Button layoutX="169.0" layoutY="76.0" mnemonicParsing="false" onAction="#Btn2Click" text="+ Button" />
      <ProgressBar fx:id="pb" layoutX="52.0" layoutY="117.0" prefWidth="200.0" progress="0.5" />
      <ProgressIndicator fx:id="pi" layoutX="90.0" layoutY="165.0" prefHeight="104.0" prefWidth="121.0" progress="0.5" />
   </children>
</AnchorPane>
 
cs


3. MyNumber.java


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
28
29
30
package application;
 
import javafx.beans.property.*;
 
public class MyNumber {
    
    private DoubleProperty number;
 
    public double getNumber() {
        if(number != null){
            return number.get();
        }else{
            return 0;
        }
        
    }
 
    public void setNumber(double number) {
        this.numberProperty().set(number); 
    }
    
    public final DoubleProperty numberProperty(){
        if(number == null){
            number = new SimpleDoubleProperty(0);
        }
        return number;
    }
    
}
 
cs


4. MainController.java


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
 
public class MainController implements Initializable {
 
    final MyNumber myNum = new MyNumber();
    
    @FXML
    private Label lblStatus;
    
    @FXML
    private ProgressBar pb;
    
    @FXML
    private ProgressIndicator pi;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        myNum.setNumber(0);
        myNum.numberProperty().addListener(new ChangeListener<Object>() {
 
            @Override
            public void changed(ObservableValue<extends Object> observable, Object oldValue, Object newValue) {
                lblStatus.setText(new Double(myNum.getNumber()).toString());
                pb.progressProperty().bind(myNum.numberProperty());
                pi.progressProperty().bind(myNum.numberProperty());
            }
        });
        
    }
 
    public void BtnClick(ActionEvent event) {
        myNum.setNumber(myNum.getNumber() - 0.1);
    }
    
    public void Btn2Click(ActionEvent event) {
        myNum.setNumber(myNum.getNumber() + 0.1);
    }
}
 
cs




'Java > JavaFx' 카테고리의 다른 글

JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem  (0) 2016.08.25
JavaFX 19. Bidirectional Binding and using Slider  (0) 2016.08.25
JavaFX 17. Properties  (0) 2016.08.25
JavaFX 16. FileChooser  (0) 2016.08.25
JavaFX 15. TreeView Events  (0) 2016.08.25
COMMENT