JavaFx:How to choose single file and multiple files in Java Fx and display in a list view?
For creating a file chooser you should have a structure as provided below in JavaFx project.
We have created this file chooser to select single or multiple files from a specific directory and display the file names in a list view. You may pass the directory of the file or name of the file to a Java class.
Main.java
package application;
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("/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);
}
}
MainController.java
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 MainController {
@FXML
private Button btn1;
@FXML
private Button btn2;
@FXML
private ListView listview;
public void buttonaction1(ActionEvent event){
FileChooser fc =new FileChooser();
//Open files of a default directory
fc.setInitialDirectory(new File("C:\\Users\\sridhar mohanty\\Desktop"));
//Filter out a specific format
fc.getExtensionFilters().addAll(new ExtensionFilter("Microsoft Word Files","*.doc"));
File SelectedFile=fc.showOpenDialog(null);
if(SelectedFile!=null){
listview.getItems().add(SelectedFile.getName());
//Display absolute path
//listview.getItems().add(SelectedFile.getAbsolutePath());
}else{
System.out.println("File is not valid");
}
}
public void buttonaction2(ActionEvent event){
FileChooser fc =new FileChooser();
//Open files of a default directory
fc.setInitialDirectory(new File("C:\\Users\\sridhar mohanty\\Desktop"));
//Filter out a specific format
fc.getExtensionFilters().addAll(new ExtensionFilter("Microsoft Word Files","*.doc"));
List<File> SelectedFiles=fc.showOpenMultipleDialog(null);
if(SelectedFiles!=null){
for(int i=0;i<SelectedFiles.size();i++)
listview.getItems().add(SelectedFiles.get(i).getName());
}else{
System.out.println("File is not valid");
}
}
}
Main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<Button fx:id="btn1" layoutX="41.0" layoutY="138.0" mnemonicParsing="false" onAction="#buttonaction1" text="Select File" />
<Button fx:id="btn2" layoutX="18.0" layoutY="259.0" mnemonicParsing="false" onAction="#buttonaction2" text="Select Multiple File" />
<ListView fx:id="listview" layoutX="168.0" layoutY="66.0" prefHeight="282.0" prefWidth="200.0" />
</children>
</AnchorPane>
No comments:
Post a Comment