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

JavaFX 19. Bidirectional Binding and using Slider

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 = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root);
            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
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <Slider fx:id="slider" layoutX="27.0" layoutY="31.0" prefHeight="55.0" prefWidth="436.0" />
      <TextField fx:id="field" layoutX="27.0" layoutY="101.0" prefHeight="29.0" prefWidth="436.0" />
   </children>
</AnchorPane>
 
cs


3. 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
package application;
 
import java.net.URL;
import java.text.NumberFormat;
import java.util.ResourceBundle;
 
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
 
public class MainController implements Initializable {
 
    @FXML
    private Slider slider;
    
    @FXML
    private TextField field;
    
    private static final double INIT_VALUE = 50;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        slider.setValue(INIT_VALUE);
        field.setText(new Double(INIT_VALUE).toString());
        field.textProperty().bindBidirectional(slider.valueProperty(), NumberFormat.getNumberInstance());
    }
 
}
 
cs


012


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

JavaFX 21. CheckBox  (0) 2016.08.25
JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem  (0) 2016.08.25
JavaFX 18. Binding, ProgressBar and ProgressIndicator  (0) 2016.08.25
JavaFX 17. Properties  (0) 2016.08.25
JavaFX 16. FileChooser  (0) 2016.08.25
COMMENT