JAVA chapter17. javaFX. 


17.9 JavaFX 다이얼로그

17.9.1 FileChooser, DirectoryChooser

17.9.2 Popup




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.scene.control.*?>
<?import javafx.geometry.*?>
 
<HBox xmlns:fx="http://javafx.com/fxml" 
     fx:controller="sec09.exam01_dialog.RootController"
    alignment="TOP_LEFT" spacing="10.0" >
   <children>
      <Button text="Open FileChooser" onAction="#handleOpenFileChooser"/>
      <Button text="Save FileChooser" onAction="#handleSaveFileChooser"/>      
      <Button text="DirectoryChooser" onAction="#handleDirectoryChooser"/>
      <Button text="Popup" onAction="#handlePopup"/>
      <Button text="Custom" onAction="#handleCustom"/>
   </children>
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
</HBox>
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sec09.exam01_dialog;
 
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Modality;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
 
public class RootController implements Initializable {
    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }
    
    private Stage primaryStage;    
    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }    
    
    public void handleOpenFileChooser(ActionEvent e) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.getExtensionFilters().addAll(
            new ExtensionFilter("Text Files""*.txt"),
            new ExtensionFilter("Image Files""*.png""*.jpg""*.gif"),
            new ExtensionFilter("Audio Files""*.wav""*.mp3""*.aac"),
            new ExtensionFilter("All Files""*.*"));
        File selectedFile = fileChooser.showOpenDialog(primaryStage);
        if (selectedFile != null) {
            System.out.println(selectedFile.getPath());
        }
    }
    
    public void handleSaveFileChooser(ActionEvent e) {    
         FileChooser fileChooser = new FileChooser();
         fileChooser.getExtensionFilters().add(new ExtensionFilter("All Files""*.*"));
         File selectedFile = fileChooser.showSaveDialog(primaryStage);
         if (selectedFile != null) {
             System.out.println(selectedFile.getPath());
         }        
    }
    
    public void handleDirectoryChooser(ActionEvent e) {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        File selectedDir = directoryChooser.showDialog(primaryStage);
        if (selectedDir != null) {
             System.out.println(selectedDir.getPath());
         }    
    }
    
    public void handlePopup(ActionEvent e) throws Exception {
        Popup popup = new Popup();
        
        Parent parent = FXMLLoader.load(getClass().getResource("popup.fxml"));
        ImageView imageView = (ImageView) parent.lookup("#imgMessage");
        imageView.setImage(new Image(getClass().getResource("images/dialog-info.png").toString()));
        imageView.setOnMouseClicked(event->popup.hide());
        Label lblMessage = (Label)parent.lookup("#lblMessage");
        lblMessage.setText("메시지가 왔습니다.");
        
        popup.getContent().add(parent);
        popup.setAutoHide(true);    
        popup.show(primaryStage);
    }
    
    public void handleCustom(ActionEvent e) throws Exception {
        Stage dialog = new Stage(StageStyle.UTILITY);
        dialog.initModality(Modality.WINDOW_MODAL);
        dialog.initOwner(primaryStage);
        dialog.setTitle("확인");
        
        Parent parent = FXMLLoader.load(getClass().getResource("custom_dialog.fxml"));
        Label txtTitle = (Label) parent.lookup("#txtTitle");
        txtTitle.setText("확인하셨습니까?");
        Button btnOk = (Button) parent.lookup("#btnOk");
        btnOk.setOnAction(event->dialog.close());    
        Scene scene = new Scene(parent);
        
        dialog.setScene(scene);
        dialog.setResizable(false);
        dialog.show();
    }
}
cs





17.9.3 커스텀 다이얼로그




17.9.4 컨트롤러에서 primaryStage 사용



Posted by 너래쟁이
: