JFileChooser java example

Problem:

Create java gui app that allows the user to pick picture
and text files only and displays them respectively.




Solution:

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;

public class JFileChooserapp extends JFrame {

 
 private JMenuBar bar;
 private JMenu fileMenu;
 private JMenuItem openMenuItem;
 private JEditorPane contentArea;
 private JScrollPane scrollPane;
 private File selectedFile;
 private String[] okExtensions = {"png", "gif", "tiff", "jpg", "txt"};
 
 public JFileChooserapp() {
  super("JFileChooser Demo");
  
  bar = new JMenuBar();
  setJMenuBar(bar);
  
  fileMenu = new JMenu("File");
  fileMenu.setMnemonic('f');
  
  openMenuItem = new JMenuItem("Open");
  openMenuItem.setMnemonic('o');
  openMenuItem.addActionListener(new ActionListener() {
   
   @Override 
   public void actionPerformed(ActionEvent e) {
    selectedFile = getFile();
    if(selectedFile !=null)
     displayFile(selectedFile);
   }

  });
  
  fileMenu.add(openMenuItem);
  bar.add(fileMenu);
  setJMenuBar(bar);
  
  contentArea = new JEditorPane();
  contentArea.setEditable(false);
  scrollPane = new JScrollPane(contentArea);
  
  Container contentPane = getContentPane();
  contentPane.add(scrollPane);
  
  setExtendedState(JFrame.MAXIMIZED_BOTH);
  setVisible(true);
  
  
 }
 
 private File getFile() {
  
  File output = null;
  
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  fileChooser.setFileFilter(new FileFilter() {

   @Override
   public String getDescription() {
   String description = "";
   for (String ext : okExtensions) 
    description += "*." + ext + " ";
   return description;
   }
   
   @Override
   public boolean accept(File f) {
    for(String ext: okExtensions)  
     if (f.getName().toLowerCase().endsWith(ext)) 
     return true;
    return false;
   }

   
  
  });
  
  int result = fileChooser.showOpenDialog(this);
  if (result == JFileChooser.APPROVE_OPTION) {
    output = fileChooser.getSelectedFile();
    if ( output == null || !output.exists()) output = null;
  }
  return output;
  }

 private void displayFile(File file) {
 
  String fileName = file.getName();
  int lastIndexOfDot = fileName.lastIndexOf('.');
  String extension  = fileName.substring(lastIndexOfDot+1);
  boolean isImage = true;
  if (extension.equalsIgnoreCase("txt")) isImage= false;
  
  URL fileUrl = null;
  try {
   fileUrl = file.toURI().toURL();
  } catch (MalformedURLException e) {
   return;
  }
  
  if (isImage) {
   contentArea.setContentType("text/html");
   contentArea.setText("<html><title>Image</title><img src=" + 
   fileUrl + " /></html>");
   
  } else {
   try {
    contentArea.setContentType("text/plain");
    contentArea.setPage(fileUrl);
   } catch (IOException e) {
    return;
   }
  }
  
  
  
 }
 
 
 
 public static void main(String[] args) {
  JFileChooserapp demo = new JFileChooserapp();
  demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
}


1 comment :

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact