event

A simple application to Load – Save – Edit a file

This is an example on how to create a simple GUI application that Loads, Edits and Saves a file in Java. You might find it useful if you wish to add such a functionality built into your app. For example if you have an application that handles Files, and you want to add a “Quick Edit” option.

Basically, to create this simple application, one should follow these steps:

  • Create a new JFrame.
  • Add a Container to it.
  • Add a JEditorPane
  • Use the setEditorKit function to the JEditorPane and give as an argument a new HTMLEditorKit so that your editor can handle HTML formatted documents.
  • Use the classic file handling mechanisms like FileReader to open and read the file you want
  • Use JTextComponent.read function in order to load the file on the screen.
  • Use JButton components and bundle them with the main actions e.g load, save ,edit.

Let’s take a look at the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.JTextComponent;
import javax.swing.text.html.HTMLEditorKit;

public class Main {

    public static void main(String args[]) {

  final String inputFilePath = "C:/Users/nikos7/Desktop/output.txt";

  JFrame jFrame = new JFrame("Load ,Edit and Save file");

  Container content = jFrame.getContentPane();

  final JEditorPane edPane = new JEditorPane();

  JScrollPane sPne = new JScrollPane(edPane);

  content.add(sPne, BorderLayout.CENTER);

  edPane.setEditorKit(new HTMLEditorKit());

  JPanel jPanel = new JPanel();

  Action Load = new AbstractAction() {

@Override

public void actionPerformed(ActionEvent event) {

    try {

  load(edPane, inputFilePath);

    } catch (Exception e1) {

  e1.printStackTrace();

    }

}

  };

  Load.putValue(Action.NAME, "Load");

  JButton loadButton = new JButton(Load);

  jPanel.add(loadButton);

  Action absActionSave = new AbstractAction() {

@Override

public void actionPerformed(ActionEvent event) {

    try {

  save(edPane, inputFilePath);

    } catch (Exception e1) {

  e1.printStackTrace();

    }

}

  };

  absActionSave.putValue(Action.NAME, "Save");

  JButton jButton = new JButton(absActionSave);

  jPanel.add(jButton);

  Action absActionClear = new AbstractAction() {

@Override

public void actionPerformed(ActionEvent event) {

    edPane.setText("");

}

  };

  absActionClear.putValue(Action.NAME, "Clear");

  JButton clearButton = new JButton(absActionClear);

  jPanel.add(clearButton);

  content.add(jPanel, BorderLayout.SOUTH);

  jFrame.setSize(800, 600);

  jFrame.setVisible(true);
    }

    public static void save(JTextComponent text, String inputFile) throws Exception {

  FileWriter writer = null;

  writer = new FileWriter(inputFile);

  text.write(writer);

  writer.close();
    }

    public static void load(JTextComponent text, String inputFile) throws Exception {

  FileReader inputReader = null;

  inputReader = new FileReader(inputFile);

  text.read(inputReader, inputFile);

  inputReader.close();

    }
}

 
This was an example on how to create an application that Loads, Edits and Saves a file in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button