Created
May 4, 2018 18:10
-
-
Save sharifulislam52/c6cdc0332410f1edf6ff658307697987 to your computer and use it in GitHub Desktop.
javafx : File Chooser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.stage.Stage; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); | |
Scene scene = new Scene(root,600,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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.ListView?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<AnchorPane minHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="application.MainController"> | |
<children> | |
<Button fx:id="btn1" layoutX="108.0" layoutY="82.0" mnemonicParsing="false" onAction="#Button1Action" text="Select One File" /> | |
<Button fx:id="btn2" layoutX="108.0" layoutY="175.0" mnemonicParsing="false" onAction="#Button2Action" text="Select Multi Files" /> | |
<ListView fx:id="listview" layoutX="259.0" layoutY="32.0" prefHeight="342.0" prefWidth="293.0" /> | |
</children> | |
</AnchorPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 MainController { | |
@FXML | |
public Button btn1, btn2; | |
@FXML | |
public ListView listview; | |
/** | |
* select single pdf file | |
*/ | |
public void Button1Action(ActionEvent event) { | |
FileChooser fc = new FileChooser(); | |
// if we want to open fixed location | |
//fc.setInitialDirectory(new File("D:\\\\Books")); | |
// if we want to fixed file extension | |
fc.getExtensionFilters().addAll(new ExtensionFilter("PDF Files", "*.pdf")); | |
File selectedFile = fc.showOpenDialog(null); | |
if(selectedFile != null) { | |
listview.getItems().add(selectedFile.getAbsolutePath()); | |
}else { | |
System.out.println("File is not valid!"); | |
} | |
} | |
/** | |
* Select multiple files | |
*/ | |
public void Button2Action(ActionEvent event) { | |
FileChooser fc = new FileChooser(); | |
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!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing these amazing lines of File-Choosing code. It was truly useful for my project. Thanks again.