Hi...! i'm working on a notepad project but i've got a problem in undo action this is my code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane; 
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.undo.*;

public class Notepad extends javax.swing.JFrame {
  public int pos=0;
  int j=1;
  protected UndoManager undoManager = new UndoManager ();
    private void showDialogReplace(){
        jDialog1.setSize(500, 230);
        jDialog1.setTitle("Replace");
        jDialog1.setLocation(700, 300);
        jDialog1.show();
    }
    public class open{
        String Filename;

        open(){      
        }
        public void openfile(){
            JFileChooser jfc=new JFileChooser();
            jfc.showOpenDialog(jMenu1);
            try
            {
                File file=jfc.getSelectedFile();
                Filename=file.getName();
                FileReader fr=new FileReader(file);
                BufferedReader br=new BufferedReader(fr);
                String line;
                while((line=br.readLine())!=null)
                {
                     txtOpenFile.append(line+"\n");
                }
                 br.close();
                fr.close();
            }catch(IOException ex){
                JOptionPane.showMessageDialog(null,"Error Openning File!");
            }
        }
        public void getTitle(String t)
        {
            Filename=t;
        }
        public String setTitile()
        {
            return Filename;
        }
    }
    public class save{
        public String Filename,Filepath;
        save(){

        }
        PrintWriter pw;
        public void savefile(String F){ 
            try {
                Filename=F;
                System.out.println(Filename);


                if(Filename==null)
                {
                    JFileChooser jfc=new JFileChooser();
                    jfc.showSaveDialog(jMenu1);
                    File file=jfc.getSelectedFile();
                    String filename=file.getName();
                    String filepath=file.getPath();
                    Filename=filename;
                    pw = new PrintWriter(file);
                    String s = txtOpenFile.getText();
                    pw.print(s);
                    pw.close();
                    JOptionPane.showMessageDialog(null,"File "+"'"+filename+"'"+" Successfully Saved In Path:"+"\n"+filepath);
                }
                else
                {
                    File filetemp=new File(Filename);
                    PrintWriter pw=new PrintWriter(filetemp);
                    String s=txtOpenFile.getText();
                    pw.print(s);
                    pw.close();
                    JOptionPane.showMessageDialog(null,"Successfully Saved!");
                }
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "Error Saving File!");
            }

        }
        public String setTitle(){
            return Filename;
        }
    }
    public class SaveAs{

        String Filename;
        SaveAs(){
        }
        PrintWriter pw;
        public void Savefileas()
        {
            try{
            JFileChooser jfc=new JFileChooser();
            jfc.showSaveDialog(jMenu1);
            File file=jfc.getSelectedFile();
            String filename=file.getName();
            String filepath=file.getPath();
            Filename=filename;  
            PrintWriter pw = new PrintWriter(file);
            String s = txtOpenFile.getText();
            pw.print(s);
            pw.close();
           JOptionPane.showMessageDialog(null,"File "+"'"+filename+"'"+" Successfully Saved In Path:"+"\n"+filepath);
        }
        catch (IOException ex) {
            System.out.println("Error!");
        }
    }
        public String setname()
        {
            return Filename;
        }
    } 
    private void Replace(){
            String text=txtOpenFile.getText();
            String txt=txtreplace.getText();
            String rpl=txtreplacement.getText();
            int len=txt.length();
            String newstr=new String();
            boolean find = false;
            for(int i=0;i<=text.length()-1;i++)
            {
                System.out.println(text.substring(i,len));
                System.out.println("LEN"+len);
                System.out.println("I"+i);
                if(txt.equals(text.substring(i,len)))
                {
                    System.out.println(true);
                    txtOpenFile.replaceRange(rpl, i, len);
                    find=true;
                    break;
                }
                else
                {
                    find=false;
                    len++;
                }  
            }
            if(!find) {
                JOptionPane.showMessageDialog(null,txt+"Cannot Found!");
            }
        }
    private void ReplaceAll(){
            String text=txtOpenFile.getText();
            String txt=txtreplace.getText();
            String rpl=txtreplacement.getText();
            int len=txt.length();
            String newstr=new String();
            boolean find = false;
            for(int i=0;i<=text.length()-1;i++)
            {
                System.out.println(text.substring(i,len));
                System.out.println("LEN"+len);
                System.out.println("I"+i);
                if(txt.equals(text.substring(i,len)))
                {
                    System.out.println(true);
                    txtOpenFile.replaceRange(rpl, i, len);
                    find=true;
                    len++;

                }
                else
                {
                    find=false;
                    len++;
                }  
            }
            if(!find) {
                JOptionPane.showMessageDialog(null,txt+"Cannot Found!");
            }
    }
    private void Find(){
       String text=txtOpenFile.getText();
            String txt=txtreplace.getText();
            int len=txt.length();
            String newstr=new String();
            boolean find = false;
            for(int i=0;i<=text.length()-1;i++)
            {
                System.out.println(text.substring(i,len));
                System.out.println("LEN"+len);
                System.out.println("I"+i);
                if(txt.equals(text.substring(i,len)))
                {
                    System.out.println(true);
                    txtOpenFile.select(i, len);
                    find=true;
                    pos=len;
                    break;
                }
                else
                {
                    find=false;
                    len++;
                }  
            }
            if(!find) {
                JOptionPane.showMessageDialog(null,txt+" Cannot Found! ");
            } 
    }
    private void FindNext()
    {
            String text=txtOpenFile.getText();
            String txt=txtreplace.getText();
            int len=txt.length();
            String newstr=new String();
            boolean find = false;

            for(int i=pos;i<=text.length()-1;i++)
                {
                System.out.println(text.substring(i,len));
                System.out.println("LEN"+len);
                System.out.println("I"+i);
                 System.out.println("j"+j);
                 if(j*len>text.length()) JOptionPane.showMessageDialog(null,txt+" Cannot Found!" );
                if(txt.equals(text.substring(i,len*j)))
                {
                    System.out.println(true);
                    txtOpenFile.select(i, len*j);
                    find=true;
                    pos=len;
                    j++;                    
                    break;
                }
                else
                {
                    find=false;
                    len++;
                }  
            }
            if(!find) {
                JOptionPane.showMessageDialog(null,txt+" Cannot Found! ");
            }
    }
    public Notepad() {
        initComponents();
    }
    open classopen=new open();
    save classsave=new save();
    SaveAs saveasclass=new SaveAs();
    File file=new File("UndoTemp");
    @SuppressWarnings("unchecked")
    private void nemuOpenActionPerformed(java.awt.event.ActionEvent evt) {                                         

           classopen.openfile();
           this.setTitle(classopen.setTitile()+"-Notepad");
    }                                        

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

            classsave.savefile(classopen.setTitile());
            this.setTitle(classsave.setTitle()+"-Notepad");

    }                                        

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

    }                                       

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        setExtendedState(MAXIMIZED_BOTH);
        this.setTitle("Newfile"+"-Notepad");
    }                                 

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

    }                                      

    private void menusaveasActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.out.println(saveasclass.setname());
        saveasclass.Savefileas();
        classopen.getTitle(saveasclass.setname());
        this.setTitle(saveasclass.setname()+"Notepad");
    }                                          

    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
       JOptionPane.showConfirmDialog(null,
            "Are You Sure?", "Confirmation", JOptionPane.YES_NO_OPTION);
    }                                  

    private void txtOpenFileKeyPressed(java.awt.event.KeyEvent evt) {                                       
        lblStatus.setText("Writing...");
    }                                      

    private void txtOpenFileKeyReleased(java.awt.event.KeyEvent evt) {                                        
        try {
            Thread.sleep(100);
            lblStatus.setText("");
        } catch (InterruptedException ex) {
            Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                       

    private void menuExitActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.dispose();
    }                                        

    private void menuReplaceActionPerformed(java.awt.event.ActionEvent evt) {                                            
        String Text=txtOpenFile.getText();
        showDialogReplace();

    }                                           

    private void txtOpenFileKeyTyped(java.awt.event.KeyEvent evt) {                                     

        try {

            FileOutputStream fos=new FileOutputStream(file,false);
            DataOutputStream dos=new DataOutputStream(fos);
            String s=txtOpenFile.getText();
            try {
                dos.writeChars(s);
            } catch (IOException ex) {
                Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                    

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

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

    }                                         

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

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        ReplaceAll();        // TODO add your handling code here:
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            Find();        
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        FindNext();        // TODO add your handling code here:
    }                                        

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        undoManager.undo ();        // TODO add your handling code here:
    }                                          

    public static void main(String args[]) { 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Notepad().setVisible(true);
            }
        });
    }

and This the complete program,i'm working on undo function i do this but it won't work what is wrong with this guys?!
thanks...

it won't work
is pretty vague. what's the code for undo?

This part:

protected UndoManager undoManager = new UndoManager ();
    private void showDialogReplace(){
        jDialog1.setSize(500, 230);
        jDialog1.setTitle("Replace");
        jDialog1.setLocation(700, 300);
        jDialog1.show();
    }

and this part:

 private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        undoManager.undo ();        // TODO add your handling code here:
    } 

does it throw a CanNotUndoException?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.