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

JavaFX 16. FileChooser

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
package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Sample.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. Sample.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?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.SampleController">
   <children>
      <Button fx:id="btn1" layoutX="56.0" layoutY="117.0" mnemonicParsing="false" onAction="#Button1Action" text="Select One File" />
      <Button fx:id="btn2" layoutX="53.0" layoutY="173.0" mnemonicParsing="false" onAction="#Button2Action" text="Select Multi File" />
      <ListView fx:id="listView" layoutX="218.0" layoutY="21.0" prefHeight="449.0" prefWidth="258.0" />
   </children>
</AnchorPane>
 
cs


3. SampleController.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
package application;
 
import java.io.File;
import java.util.List;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
 
public class SampleController {
    @FXML
    private Button btn1;
    @FXML
    private Button btn2;
    @FXML
    private ListView listView;
    
    public void Button1Action(ActionEvent event){
        FileChooser fc = new FileChooser();
        fc.setInitialDirectory(new File ("C:/Temp"));
        fc.getExtensionFilters().addAll(new ExtensionFilter("png files","*.png"));
        File selectedFile = fc.showOpenDialog(null);
        if(selectedFile != null){
            listView.getItems().add(selectedFile.getAbsolutePath());
        } else {
            System.out.println("File is not valid");
        }
    }
    
    public void Button2Action(ActionEvent event){
        FileChooser fc = new FileChooser();
        fc.setInitialDirectory(new File ("C:/Temp"));
        fc.getExtensionFilters().addAll(new ExtensionFilter("png files","*.png"));
        List<File> selectedFiles = fc.showOpenMultipleDialog(null);
        if(selectedFiles != null){
            for (int i = 0; i < selectedFiles.size(); i++) {
                listView.getItems().add(selectedFiles.get(i).getAbsolutePath());
            }
            
        } else {
            System.out.println("File is not valid");
        }
    }
}
 
cs



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

JavaFX 18. Binding, ProgressBar and ProgressIndicator  (0) 2016.08.25
JavaFX 17. Properties  (0) 2016.08.25
JavaFX 15. TreeView Events  (0) 2016.08.25
JavaFx 14. TreeView 사용하기  (0) 2016.08.17
JavaFx 13. ListView 사용하기  (0) 2016.08.17
COMMENT