Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Program 1
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dataflair;
import java.awt.Color;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author admin
*/
public class MyNotePad extends javax.swing.JFrame {
String opensave=null;
public MyNotePad() {
initComponents();
FC.setVisible(false);
CC.setVisible(false);
btnColor.setVisible(false);
}
/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
textbox = new javax.swing.JTextPane();
FC = new javax.swing.JFileChooser();
CC = new javax.swing.JColorChooser();
btnColor = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem18 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
jMenuItem7 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem8 = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JPopupMenu.Separator();
jMenuItem9 = new javax.swing.JMenuItem();
jMenuItem10 = new javax.swing.JMenuItem();
jMenuItem11 = new javax.swing.JMenuItem();
jSeparator4 = new javax.swing.JPopupMenu.Separator();
jMenuItem12 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
jMenuItem14 = new javax.swing.JMenuItem();
jMenuItem15 = new javax.swing.JMenuItem();
jMenuItem19 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
jMenu7 = new javax.swing.JMenu();
jMenuItem16 = new javax.swing.JMenuItem();
jMenuItem17 = new javax.swing.JMenuItem();
jMenu5 = new javax.swing.JMenu();
jMenu6 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Untitled-NotePad");
textbox.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jScrollPane1.setViewportView(textbox);
FC.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
FC.setDialogTitle("Save Dialog");
FC.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
FCMouseClicked(evt);
}
});
FC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FCActionPerformed(evt);
}
});
CC.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
CCMouseClicked(evt);
}
});
btnColor.setText("Apply");
btnColor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnColorActionPerformed(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.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(CC, javax.swing.GroupLayout.PREFERRED_SIZE, 520, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(FC, javax.swing.GroupLayout.PREFERRED_SIZE, 532, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(273, 273, 273)
.addComponent(btnColor, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1569, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(421, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(FC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(CC, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnColor)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 800, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 2411, Short.MAX_VALUE))
);
jMenu1.setText("File");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("New");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setText("New Window");
jMenu1.add(jMenuItem2);
jMenuItem18.setText("Open");
jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem18ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem18);
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem3.setText("Save");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem3);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItem4.setText("Save As");
jMenu1.add(jMenuItem4);
jMenu1.add(jSeparator1);
jMenuItem5.setText("Page Set UP");
jMenu1.add(jMenuItem5);
jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem6.setText("Print");
jMenu1.add(jMenuItem6);
jMenu1.add(jSeparator2);
jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItem7.setText("Exit");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem7ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem7);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem8.setText("Undo");
jMenu2.add(jMenuItem8);
jMenu2.add(jSeparator3);
jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem9.setText("Cut");
jMenu2.add(jMenuItem9);
jMenuItem10.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem10.setText("Copy");
jMenu2.add(jMenuItem10);
jMenuItem11.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem11.setText("Paste");
jMenu2.add(jMenuItem11);
jMenu2.add(jSeparator4);
jMenuItem12.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem12.setText("Delete");
jMenu2.add(jMenuItem12);
jMenuBar1.add(jMenu2);
jMenu3.setText("Format");
jCheckBoxMenuItem1.setSelected(true);
jCheckBoxMenuItem1.setText("Word Wrap");
jMenu3.add(jCheckBoxMenuItem1);
jMenuItem14.setText("Font...");
jMenu3.add(jMenuItem14);
jMenuItem15.setText("Show Color");
jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem15ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem15);
jMenuItem19.setText("Hide Color ");
jMenuItem19.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem19ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem19);
jMenuBar1.add(jMenu3);
jMenu4.setText("View");
jMenu7.setText("Zoom");
jMenuItem16.setText("Zoom In");
jMenu7.add(jMenuItem16);
jMenuItem17.setText("ZoomOut");
jMenu7.add(jMenuItem17);
jMenu4.add(jMenu7);
jMenuBar1.add(jMenu4);
jMenu5.setText("Help");
jMenuBar1.add(jMenu5);
jMenu6.setText("Abut Us");
jMenuBar1.add(jMenu6);
setJMenuBar(jMenuBar1);
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.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
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))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
// TODO add your handling code here:
int n;
n=JOptionPane.showConfirmDialog(this, "Are you sure want to exit","Exit Window",JOptionPane.OK_CANCEL_OPTION);
if(n==0)
System.exit(0);
}//GEN-LAST:event_jMenuItem7ActionPerformed
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
// TODO add your handling code here:
//System.out.println("This is save choice");
opensave="save";
FC.setVisible(true);
}//GEN-LAST:event_jMenuItem3ActionPerformed
private void FCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FCActionPerformed
// TODO add your handling code here:
// JOptionPane.showMessageDialog(this, "This is save button action...");
if(opensave.equals("save"))
{
// For Save file in hard disk
try
{
File filename;
filename=FC.getSelectedFile();
String data=textbox.getText();
FileOutputStream fos=new FileOutputStream(filename);
byte b[]=data.getBytes();
fos.write(b);
fos.close();
// JOptionPane.showMessageDialog(this, filename);
FC.setVisible(false);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
else
{
// For open file from hard disk
try
{
File filename;
filename=FC.getSelectedFile();
JOptionPane.showMessageDialog(this, filename);
FileInputStream fis=new FileInputStream(filename);
int length;
length=(int)filename.length();
byte b[]=new byte[length];
fis.read(b);
String str=new String(b);
textbox.setText(str);
fis.close();
FC.setVisible(false);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
}//GEN-LAST:event_FCActionPerformed
private void FCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FCMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_FCMouseClicked
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenuItem1ActionPerformed
private void jMenuItem18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem18ActionPerformed
// TODO add your handling code here:
opensave="open";
FC.setDialogType(JFileChooser.OPEN_DIALOG);
FC.setVisible(true);
}//GEN-LAST:event_jMenuItem18ActionPerformed
private void jMenuItem15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem15ActionPerformed
// TODO add your handling code here:
CC.setVisible(true);
btnColor.setVisible(true);
}//GEN-LAST:event_jMenuItem15ActionPerformed
private void CCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CCMouseClicked
// TODO add your handling code here:
JOptionPane.showMessageDialog(this, "Color code click event");
}//GEN-LAST:event_CCMouseClicked
private void jMenuItem19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem19ActionPerformed
// TODO add your handling code here:
CC.setVisible(false);
btnColor.setVisible(false);
}//GEN-LAST:event_jMenuItem19ActionPerformed
private void btnColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnColorActionPerformed
// TODO add your handling code here:
Color C;
C=CC.getColor();
textbox.setForeground(C);
}//GEN-LAST:event_btnColorActionPerformed
/**
* @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(MyNotePad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyNotePad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyNotePad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyNotePad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyNotePad().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JColorChooser CC;
private javax.swing.JFileChooser FC;
private javax.swing.JButton btnColor;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenu jMenu6;
private javax.swing.JMenu jMenu7;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem10;
private javax.swing.JMenuItem jMenuItem11;
private javax.swing.JMenuItem jMenuItem12;
private javax.swing.JMenuItem jMenuItem14;
private javax.swing.JMenuItem jMenuItem15;
private javax.swing.JMenuItem jMenuItem16;
private javax.swing.JMenuItem jMenuItem17;
private javax.swing.JMenuItem jMenuItem18;
private javax.swing.JMenuItem jMenuItem19;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JMenuItem jMenuItem9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JPopupMenu.Separator jSeparator3;
private javax.swing.JPopupMenu.Separator jSeparator4;
private javax.swing.JTextPane textbox;
// End of variables declaration//GEN-END:variables
}