Wednesday, 21 October 2015

How To Open And Save File In Java Fx

Before reading this article you should read Tutorial on file chooser in javafx,Tutorial on context menu in javafx. In this tutorial we will see how to use file chooser to open and save  image files in java fx.following are the explanations of methods used in these tutorials.

imageIo.write(RenderedImage im,String format,File outputlocation)

Writes an image using an  ImageWriter that supports the given specified format of a image. If there is already a File present, its will be over written.
im: Name of image that is to be written.

format: In this parameter you have to specify format in which you want to write a image.

outputlocation: Here you have to specify the location of image where you have to write.

HOW TO SAVE IMAGE FROM JAVAFX APPLICATION USING FILE CHOOSER:

import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class mybej extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start( final Stage primaryStage) {
final Group root = new Group();
final Image pic = new Image ("fac.jpg");
ImageView iv = new ImageView();
iv.setImage(pic);

final FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save File Chooser");
final Scene scene = new Scene(root, 600, 350,Color.WHITE);
final ContextMenu cm = new ContextMenu(); 
MenuItem cmItem2 = new MenuItem("Save Image");
cm.getItems().add(cmItem2);
cmItem2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {

fileChooser.setTitle("Save Image");

File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
try {
ImageIO.write(SwingFXUtils.fromFXImage(pic, null), "png", file);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
if (e.getButton() == MouseButton.SECONDARY)  
cm.show(root, e.getX(),e.getY());
}
}); 
primaryStage.setScene(scene);
root.getChildren().addAll(iv);
primaryStage.show();
}

}

OUTPUT:



HOW TO OPEN IMAGE FILE USING FILE CHOOSER IN JAVA FX:


import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public final class mybej extends Application {

private Desktop desktop = Desktop.getDesktop();

@Override
public void start(final Stage stage) {
stage.setTitle("File Chooser Sample");
final FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(stage);

if (file != null) {
openFile(file);
}


Group root = new Group();

stage.setScene(new Scene(root));

}

public static void main(String[] args) {
Application.launch(args);
}

private void openFile(File file) {
try {
desktop.open(file);
} catch (IOException ex) {
System.out.print(ex);
}
}
}

No comments:

Post a Comment