• Sony Xperia Z1

    The best of Sony for the best of you.

  • Acer Aspire S7

    Premium Ultrabook.

  • Apple iOS 7

    The mobile OS from a whole new perspective.

  • Microsoft Surface Pro

    Stellar. Performance.

  • Samsung Galaxy S4 Zoom

    The only smartphone with 10x optical zoom.


Sunday, October 13, 2013

Simple Java Notepad Application

Problem:
Using your skills in developing a Graphical User Interface using both AWT or Swing package, event handling, and IO manipulation, create a simple notepad application in a form of an executable JAR file.

Sample output:


/**SAMPLE CODE BEGINS HERE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner.*;

public class MyNotepad implements ActionListener
{
//containers
private JFrame f;
private JPanel p;
//components
private JTextArea a;
private JButton bLoad, bSave, bClear, bClose;
public MyNotepad()
{
//containers
f = new JFrame("Je-Ar's Simple Notepad");
p = new JPanel();
//components
a = new JTextArea();
bLoad = new JButton("Load noname.txt");
bSave = new JButton("Save noname.txt");
bClear = new JButton("Clear noname.txt");
bClose = new JButton("Close noname.txt");
}
public void launchFrame()
{
//grid layout for panel p
p.setLayout(new GridLayout(1,4));
p.add(bLoad);
p.add(bSave);
p.add(bClear);
p.add(bClose);
//border layout for frame f
f.add(a, BorderLayout.CENTER);
f.add(p, BorderLayout.SOUTH);
f.setSize(600,400);
f.setVisible(true);
//register the event handlers
bLoad.addActionListener(this);
bSave.addActionListener(this);
bClear.addActionListener(this);
bClose.addActionListener(this);
f.addWindowListener(new MyCloseButtonHandler());
}
//event handler
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
if (ae.getSource() == bLoad)
{
try
{
File myFile = new File("noname.txt");
BufferedReader in = new BufferedReader(new FileReader(myFile));
String str;
while ((str = in.readLine()) != null)
{
a.setText(str);
}
in.close();
}
catch (Exception ex) 
                                System.out.println(ex.getMessage());
}
}
else if (source == bSave)
{
try 
{
File myFile2 = new File("noname.txt");
PrintWriter output = new PrintWriter(new FileWriter(myFile2));
output.write(a.getText());
output.close();
}
catch (Exception ex) 
                                System.out.println(ex.getMessage());
}
}
else if (source == bClear)
{
a.setText(null);
}
else if (source == bClose)
{
System.exit(0);
}
else {}
}
private class MyCloseButtonHandler extends WindowAdapter
{
public void windowClosing(WindowEvent ae)
{
System.exit(0);
}
}

public static void main(String args[])
{
MyNotepad mn = new MyNotepad();
mn.launchFrame();
}
}

END OF SAMPLE CODE */

To compile via command prompt, type in: javac MyNotepad.java
To run via command prompt, type in: java MyNotepad

To create a JAR file with a manifest, first create a manifest file which is in .txt format. For this source code, type in to notepad: Main-Class: MyNotepad <enter>. Remember that the <enter> is important for it to be read by Java.

Next, in the command prompt, type in: jar cvfm MyNotepad.jar manif.txt *.


Numerous lines of codes will appear but don't worry, Java is making the action for finalization which is running your JAR file.
To run the executable JAR file, type in: java -jar MyNotepad.jar

And kaboom. You've just created your simple notepad application. Just posted this because I got nerve-cracks on coding the extended version of this program. This actually isn't finished. What if the user entered a file name via command line? The default file name noname.txt would no longer be used but the inputted file name of the user instead. It may sound strange and confusing but it's a matter of playing with the codes, but for now, I'll just share this simple code with you guys. Hope this helps!

0 comments:

Post a Comment