Java GUI Text and HTML File Loading from File System using JFileChooser

How the app works:

You can load a text or html file from file system running the code or using this simple jar application. Only load button under file menu is implemented to keep it simple. You can also use file compression software’s such as 7-zip to see and extract the contents of the jar file.

Download the project and jar file from this github repository. [ TODO ]

file loader application
file loader application
jfilechooser
jfilechooser
loaded text
loaded text

Directory Structure:

please note the package statement. This is only valid if the code is inside a folder name “gui” and the “gui” folder is inside “javacode” folder. Use Dr. Java for easily running this code ( create a class with this class name and delete the package statement  ) otherwise use intellij idea.

directory structure
directory structure

Code File Loader Application:

// if you do not use a directory structure like mine 
// then delete the line below or modify it to your need
package javacode.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URI;

/**
 * Created by asif on 8/16/2015.
 *
 * Simple File Loader Application using JFileChooser
 * Events are handled for only load button for simplicity
 * You can add more based on necessity
 */
public class FileLoaderApplication {

    JFrame jFrame;
    MenuBar menuBar;
    Menu file, edit, help;
    MenuItem loadItem, newItem, closeItem, exitItem, cutItem, copyItem, pasteItem, divideItem;
    TextArea textArea;
    JFileChooser jFileChooser;

    public FileLoaderApplication() {
        jFrame = new JFrame("Menu window");

        textArea = new TextArea();
        menuBar = new MenuBar();
        jFileChooser = new JFileChooser();

        file = new Menu("File");
        edit = new Menu("Edit");
        help = new Menu("Help");

        loadItem = new MenuItem("Load");
        newItem = new MenuItem("New");
        closeItem = new MenuItem("Close");
        exitItem = new MenuItem("Exit");
        cutItem = new MenuItem("Cut");
        copyItem = new MenuItem("Copy");
        pasteItem = new MenuItem("Paste");
        divideItem = new MenuItem("-------");
    }

    public static void main(String [] args){
        FileLoaderApplication t = new FileLoaderApplication();
        t.showFrame();
    }

    public void showFrame(){
        file.add(loadItem);
        loadItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    // load a file from file system
                    jFileChooser.showOpenDialog(jFrame);
                    FileReader fr = new FileReader(jFileChooser.getSelectedFile());

//                    FileReader fr = new FileReader("javacode/gui/in.txt");

                    // read bytes from the file
                    // use BufferedReader for faster reading
                    int s = fr.read();
                    String a = "";
                    while (s != -1) {
                        a += (char) s;
                        s = fr.read();
                    }

                    //a = a.replaceAll("\\s+", " ");

                    textArea.setText(a);

                } catch (Exception e1) {
                    textArea.setText("Error Loading File");
                    e1.printStackTrace();
                }
            }
        });

        file.add(newItem);
        file.add(closeItem);
        file.add(divideItem);
        file.add(exitItem);

        edit.add(cutItem);
        edit.add(copyItem);
        edit.add(pasteItem);

        menuBar.add(file);
        menuBar.add(edit);
        menuBar.add(help);

        textArea.setPreferredSize(new Dimension(380, 300));
        textArea.repaint();
        jFrame.add(textArea);

        jFrame.setLayout(new FlowLayout());
        jFrame.setSize(new Dimension(400, 400));

        jFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        jFrame.setVisible(true);

        jFrame.setMenuBar(menuBar);
    }

}

 

Creating the jar File:

Read below only if your directory structure looks like mine or you have other unnecessary files in your project.

Since I am not working in the default directory and there are other classes in my project. So some things are needed to be changed. First see my tutorial on creating a jar artifact in intellij idea.
Now following that tutorial you will be able to create a working jar. But it will also have other useless files which in turns increase the jar size.  One way to fix that problem is shown below.

Go to File->Project Structure->Modules->Sources
Now right-click and select exclude for any module you don’t want the jar to get compiled with.
fix project structure
fix project structure

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s