You are on page 1of 16

Java_Notepad.

java:

package java_notepad;
import javax.swing.JFrame;
/*
* @author Deepak Sharma
*/
public class Java_notepad {

public static void main(String[] args) {


// TODO code application logic here
Main ob = new Main();
ob.setVisible(true);
ob.setResizable(false);
ob.setTitle(“Notepad”);
ob.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
}
}

Page 22 of 47
Main.java:

package java_notepad;
import java.awt.FileDialog;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/*
* @author Deepak Sharma
*/
public class Main extends javax.swing.JFrame {
/**
* Creates new form Main
*/
String filename;
Clipboard clipboard = getToolkit().getSystemClipboard();
public Main() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


jScrollPane1 = new javax.swing.JScrollPane();
TextArea = new javax.swing.JTextArea();

Page 23 of 47
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
NewFileMenuItem = new javax.swing.JMenuItem();
OpenFileMenuItem = new javax.swing.JMenuItem();
SaveFileMenuItem = new javax.swing.JMenuItem();
ExitMenuItem = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
CutMenuItem = new javax.swing.JMenuItem();
CopyMenuItem = new javax.swing.JMenuItem();
PasteMenuItem = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
WordWrapMenuItem = new javax.swing.JCheckBoxMenuItem();
jMenu4 = new javax.swing.JMenu();
AboutMenuItem = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});

TextArea.setColumns(20);
TextArea.setRows(5);
jScrollPane1.setViewportView(TextArea);

javax.swing.GroupLayout jPanel1Layout = new


javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
589, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);

Page 24 of 47
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377,
Short.MAX_VALUE)
);

jMenu1.setText("File");

NewFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.even
t.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_DOWN_MASK));
NewFileMenuItem.setText("New");
NewFileMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NewFileMenuItemActionPerformed(evt);
}
});
jMenu1.add(NewFileMenuItem);

OpenFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.eve
nt.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_DOWN_MASK));
OpenFileMenuItem.setText("Open");
OpenFileMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenFileMenuItemActionPerformed(evt);
}
});
jMenu1.add(OpenFileMenuItem);

SaveFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.even
t.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_DOWN_MASK));
SaveFileMenuItem.setText("Save");
SaveFileMenuItem.addMenuKeyListener(new
javax.swing.event.MenuKeyListener() {
public void menuKeyPressed(javax.swing.event.MenuKeyEvent evt) {
SaveFileMenuItemMenuKeyPressed(evt);
}

Page 25 of 47
public void menuKeyReleased(javax.swing.event.MenuKeyEvent evt) {
}
public void menuKeyTyped(javax.swing.event.MenuKeyEvent evt) {
}
});
SaveFileMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveFileMenuItemActionPerformed(evt);
}
});
jMenu1.add(SaveFileMenuItem);

ExitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.Ke
yEvent.VK_ESCAPE, 0));
ExitMenuItem.setText("Exit");
ExitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitMenuItemActionPerformed(evt);
}
});
jMenu1.add(ExitMenuItem);

jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");

CutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.Ke
yEvent.VK_X, java.awt.event.InputEvent.CTRL_DOWN_MASK));
CutMenuItem.setText("Cut");
CutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CutMenuItemActionPerformed(evt);
}
});
jMenu2.add(CutMenuItem);

Page 26 of 47
CopyMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.K
eyEvent.VK_C, java.awt.event.InputEvent.CTRL_DOWN_MASK));
CopyMenuItem.setText("Copy");
CopyMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CopyMenuItemActionPerformed(evt);
}
});
jMenu2.add(CopyMenuItem);

PasteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.K
eyEvent.VK_V, java.awt.event.InputEvent.CTRL_DOWN_MASK));
PasteMenuItem.setText("Paste");
PasteMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PasteMenuItemActionPerformed(evt);
}
});
jMenu2.add(PasteMenuItem);

jMenuBar1.add(jMenu2);

jMenu3.setText("Format");
jMenu3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu3ActionPerformed(evt);
}
});

WordWrapMenuItem.setSelected(false);
WordWrapMenuItem.setText("Word Wrap");
WordWrapMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
WordWrapMenuItemActionPerformed(evt);
}
});
jMenu3.add(WordWrapMenuItem);

Page 27 of 47
jMenuBar1.add(jMenu3);

jMenu4.setText("Help");

AboutMenuItem.setText("About");
AboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AboutMenuItemActionPerformed(evt);
}
});
jMenu4.add(AboutMenuItem);

jMenuBar1.add(jMenu4);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

private void SaveFileMenuItemMenuKeyPressed(javax.swing.event.MenuKeyEvent


evt) {
// TODO add your handling code here:private void save() {

Page 28 of 47
private void NewFileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TextArea.setText("");
setTitle("Untitled -Notepad");

private void OpenFileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
FileDialog filedialog = new FileDialog(Main.this,"Open File", FileDialog.LOAD);
filedialog.setVisible(true);

if(filedialog.getFile()!= null){
filename = filedialog.getDirectory() + filedialog.getFile();
setTitle(filename);
}
try{
try (BufferedReader reader = new BufferedReader(new FileReader(filename)))
{
StringBuilder sb = new StringBuilder();

String line = null;


while((line = reader.readLine()) != null){
sb.append(line).append("/n");
TextArea.setText(sb.toString());
}
}
}catch(IOException e){
System.out.println("File Not Found");
}
}

private void SaveFileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
PrintWriter fout = null;
FileDialog filedialog = new FileDialog(Main.this,"Save File", FileDialog.SAVE);
filedialog.setVisible(true);

if(filedialog.getFile()!= null){

Page 29 of 47
filename = filedialog.getDirectory() + filedialog.getFile();
setTitle(filename);

}
try{
fout = new PrintWriter(new FileWriter("test.txt"));
//FileWriter filewriter = new FileWriter(filename);
//filewriter
fout.write(TextArea.getText());
setTitle(filename);
//filewriter.close();

}catch(IOException e){
System.out.println("File Not Found");
}
}

private void CutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String cutString = TextArea.getSelectedText();
StringSelection cutSelection = new StringSelection(cutString);
clipboard.setContents(cutSelection, cutSelection);
TextArea.replaceRange("", TextArea.getSelectionStart(),
TextArea.getSelectionEnd());

private void CopyMenuItemActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String copyText = TextArea.getSelectedText();
StringSelection copySelection = new StringSelection(copyText);
clipboard.setContents(copySelection, copySelection);
}

private void PasteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
try{
Transferable pasteText = clipboard.getContents(Main.this);
String sel = (String) pasteText.getTransferData(DataFlavor.stringFlavor);

Page 30 of 47
TextArea.replaceRange(sel, TextArea.getSelectionStart(),
TextArea.getSelectionEnd());
}catch(UnsupportedFlavorException | IOException e){
System.out.println("Didn't Work !");
}
}

private void AboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
About ob1= new About();
ob1.setVisible(true);

private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

private void WordWrapMenuItemActionPerformed(java.awt.event.ActionEvent


evt) {
// TODO add your handling code here:
TextArea.setLineWrap(true);
TextArea.setWrapStyleWord(true);
if(WordWrapMenuItem.isSelected()){
TextArea.setLineWrap(true);
TextArea.setWrapStyleWord(true);
}else{
TextArea.setLineWrap(false);
TextArea.setWrapStyleWord(false);
}
}

private void formWindowClosing(java.awt.event.WindowEvent evt) {


// TODO add your handling code here:

System.exit(0);

Page 31 of 47
private void ExitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look
and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

Page 32 of 47
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(() -> {
new Main().setVisible(true);
});
}

// Variables declaration - do not modify


private javax.swing.JMenuItem AboutMenuItem;
private javax.swing.JMenuItem CopyMenuItem;
private javax.swing.JMenuItem CutMenuItem;
private javax.swing.JMenuItem ExitMenuItem;
private javax.swing.JMenuItem NewFileMenuItem;
private javax.swing.JMenuItem OpenFileMenuItem;
private javax.swing.JMenuItem PasteMenuItem;
private javax.swing.JMenuItem SaveFileMenuItem;
private javax.swing.JTextArea TextArea;
private javax.swing.JCheckBoxMenuItem WordWrapMenuItem;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}

Page 33 of 47
About.java

package java_notepad;

/**
*
* @author Deepak Sharma
*/
public class About extends javax.swing.JFrame {

/**
* Creates new form About
*/
public About() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();

jLabel1.setText("jLabel1");

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setLocationByPlatform(true);

Page 34 of 47
jLabel2.setIcon(new javax.swing.ImageIcon("D:\\Java_notepad\\icons\\letter-
i.png")); // NOI18N

jLabel3.setIcon(new
javax.swing.ImageIcon("D:\\Java_notepad\\icons\\about.png")); // NOI18N

jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new


javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(161, 161, 161)
.addComponent(jButton1)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()

Page 35 of 47
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(17, 17, 17)
.addComponent(jLabel2)))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addContainerGap(12, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
System.exit(0);
}

/**
* @param args the command line arguments
*/

Page 36 of 47
public static void main(String args[]) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(About.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(() -> {
new About().setVisible(true);
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

Page 37 of 47

You might also like