Monday, 19 October 2015

Filled Under:

Tutorial On File Chooser In Java Fx

In our previous article we have also discussed about jfilechooser the reason of writing again an article on filechooser in javafx is that in javafx it has some new features that we are going to   discussed in this article.

SAVE FILE CHOOSER IN JAVAFX:

Below program shows how to create a simple save file chooser this used when want to save a particular file using filechooser. 

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class mybej extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start( Stage primaryStage) {
Group root = new Group();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save File Chooser");
fileChooser.showSaveDialog(primaryStage);
primaryStage.setScene(new Scene(root, 500, 400));

}

}


OPEN FILE CHOOSER IN JAVAFX:

This program can be used to create a open file chooser that can be useful to open a particular file.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class mybej extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start( Stage primaryStage) {
Group root = new Group();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open File Chooser");
fileChooser.showOpenDialog(primaryStage);
primaryStage.setScene(new Scene(root, 500, 400));

}

}



HOW TO SET INITIAL DIRECTORY OF FILE CHOOSER IN JAVAFX:

JavaFx provides a new option by using this option we can set the initial directory of filechooser.This can be done by simple adding below code in your code. 
fileChooser.setInitialDirectory(new File("C:"));

HOW TO ADD EXTENSION FILTER  IN FILECHOOSER IN JAVAFX:

Some we want that only a particular type of file is should be shown by file chooser in javafx we can do this by simply adding this code in this program we have added jpg extion filter you can change it according to your need.
 fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("PNG", "*.png"),
new FileChooser.ExtensionFilter("JAVA", "*.java")    
);




0 comments:

Post a Comment