Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

how to add undo feature in Swing

$
0
0
package niitproject;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class NIITProject extends JFrame implements ActionListener {
    MenuBar mbar;
Menu file,edit,format,font,font1,font2;
MenuItem item1,item2,item3,item4;
MenuItem item5,item6,item7,item8,item9,item10,item11;
MenuItem fname1,fname2,fname3,fname4;
MenuItem fstyle1,fstyle2,fstyle3,fstyle4;
MenuItem fsize1,fsize2,fsize3,fsize4;
JPanel mainpanel;
TextArea text;
Font f;
String command=" ";
GregorianCalendar gcalendar;
String str=" ";
String path,content;
int i=0;
int pos1;
public NIITProject(String str)
{
super(str);

mainpanel=new JPanel();
mainpanel=(JPanel)getContentPane();
mainpanel.setLayout(new GridLayout());

mbar=new MenuBar();
setMenuBar(mbar);
file=new Menu("File");
edit=new Menu("Edit");
format=new Menu("Format");
font=new Menu("Font");
font1=new Menu("Font Style");
font2=new Menu("Size");

file.add(item1=new MenuItem("New..."));
file.add(item2=new MenuItem("Open"));
file.add(item3=new MenuItem("Save As..."));
file.add(item8=new MenuItem("Print"));
file.add(item4=new MenuItem("Exit"));
mbar.add(file);


edit.add(item5=new MenuItem("Cut"));
edit.add(item6=new MenuItem("Copy"));
edit.add(item7=new MenuItem("Paste"));
edit.add(item9=new MenuItem("Time/Date"));


mbar.add(edit);

format.add(font);
format.add(font1);
format.add(font2);
format.add(item10=new MenuItem("Background Color"));
format.add(item11=new MenuItem("Font Color"));

font.add(fname1=new MenuItem("Courier"));
font.add(fname2=new MenuItem("Sans Serif"));
font.add(fname3=new MenuItem("Monospaced"));
font.add(fname4=new MenuItem("Symbol"));

font1.add(fstyle1=new MenuItem("Regular"));
font1.add(fstyle2=new MenuItem("Bold"));
font1.add(fstyle3=new MenuItem("Italic"));
font1.add(fstyle4=new MenuItem("Bold Italic"));

font2.add(fsize1=new MenuItem("12"));
font2.add(fsize2=new MenuItem("14"));
font2.add(fsize3=new MenuItem("18"));
font2.add(fsize4=new MenuItem("20"));

mbar.add(format);



item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
item6.addActionListener(this);
item7.addActionListener(this);
item8.addActionListener(this);
item9.addActionListener(this);
item10.addActionListener(this);
item11.addActionListener(this);
fname1.addActionListener(this);
fname2.addActionListener(this);
fname3.addActionListener(this);
fname4.addActionListener(this);
fstyle1.addActionListener(this);
fstyle2.addActionListener(this);
fstyle3.addActionListener(this);
fstyle4.addActionListener(this);
fsize1.addActionListener(this);
fsize2.addActionListener(this);
fsize3.addActionListener(this);
fsize4.addActionListener(this);


text=new TextArea();
mainpanel.add(text,BorderLayout.CENTER);

f=new Font("Monotype Corsiva",Font.PLAIN,15);
text.setFont(f);

}

   public void actionPerformed(ActionEvent ae)
{
command=(String)ae.getActionCommand();

if(command.equals("New..."))
{
try
{

dispose();
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
NIITProject note1 = new NIITProject("Untitled-Notepad");
note1.setSize(d);
note1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
note1.setVisible(true);
}
catch(Exception abcdefghij)
{

}
}
if(command.equals("Open"))
{
try
{
JFileChooser jfc=new JFileChooser();
jfc.showOpenDialog(mainpanel);
File file=jfc.getSelectedFile();
text.setText(null);
try
{
FileInputStream fin=new FileInputStream(file);
int data=0;
while((data=fin.read())!=-1)
{
text.append(String.valueOf((char)data));
}
}catch(IOException e1)
{
e1.printStackTrace();
}
}
catch(Exception ez)
{

}
}
if(command.equals("Save As..."))
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r = fc.showSaveDialog(this);
if(r==fc.CANCEL_OPTION)
return;
File myfile = fc.getSelectedFile();
if(myfile==null || myfile.getName().equals(""))
{
JOptionPane.showMessageDialog(this,"Please enter a file name!","Error",JOptionPane.ERROR_MESSAGE);
return;
}
if(myfile.exists())
{
r = JOptionPane.showConfirmDialog(this, "A file with same name already exists!\nAre you sure want to overwrite?");
if(r!= 0)
return;
}
try
{
FileWriter fw = new FileWriter(myfile);
fw.write(text.getText());
String content = text.getText();
setTitle(myfile.getName()+" - Notepad");
fw.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"Failed to save the file","Error",JOptionPane.ERROR_MESSAGE);
}
}

if(command.equals("Print"))
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new Print());
if (pj.printDialog()) 
{

try 
{
pj.print();
}
catch (PrinterException e) 
{
System.out.println(e);
}

}

}


if(command.equals("Exit"))
{
//System.exit(0);
if(text.getText().equals("") || text.getText().equals(content))
System.exit(0);
else
{
int b = JOptionPane.showConfirmDialog(null, "The text has been changed.\nDo you want to save the changes?");
if(b==0)
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r = fc.showSaveDialog(this);
if(r==JFileChooser.CANCEL_OPTION) {
        return;
    }
File myfile = fc.getSelectedFile();
if(myfile==null || myfile.getName().equals(""))
{
JOptionPane.showMessageDialog(this,"Please enter a file name!","Error",JOptionPane.ERROR_MESSAGE);
return;
}
if(myfile.exists())
{
r = JOptionPane.showConfirmDialog(this, "A file with same name already exists!\nAre you sure want to overwrite?");
if(r!= 0)
return;
}
try
{
FileWriter fw = new FileWriter(myfile);
fw.write(text.getText());
String content = text.getText();
setTitle(myfile.getName()+" - Notepad");
fw.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"Failed to save the file","Error",JOptionPane.ERROR_MESSAGE);
}
}
else if(b==1)
{
System.exit(0);
}
else if(b==2)
{
return;
}
}
}
if(command.equals("Cut"))
{
str=text.getSelectedText();
i=text.getText().indexOf(str);
text.replaceRange(" ",i,i+str.length());
}

if(command.equals("Copy"))
{
str=text.getSelectedText();
}

if(command.equals("Paste"))
{
pos1=text.getCaretPosition();
text.insert(str,pos1);
}

if(command.equals("Time/Date"))
{
gcalendar=new GregorianCalendar();
String h=String.valueOf(gcalendar.get(Calendar.HOUR));
String m=String.valueOf(gcalendar.get(Calendar.MINUTE));
String s=String.valueOf(gcalendar.get(Calendar.SECOND));
String date=String.valueOf(gcalendar.get(Calendar.DATE));
String mon=String.valueOf(gcalendar.get(Calendar.MONTH));
String year=String.valueOf(gcalendar.get(Calendar.YEAR));
String hms="Time"+" - "+h+":"+m+":"+s+" Date"+" - "+date+" "+mon+" "+year;
int loc=text.getCaretPosition();
text.insert(hms,loc);
}
if(command.equals("Courier"))
{

String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Courier",fontStyle,fontSize);
text.setFont(f);
}
if(command.equals("Sans Serif"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Sans Serif",fontStyle,fontSize);
text.setFont(f);
}
if(command.equals("Monospaced"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Monospaced",fontStyle,fontSize);
text.setFont(f);
}

if(command.equals("Symbol"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Symbol",fontStyle,fontSize);
text.setFont(f);
System.out.println(f.getFamily());
}
if(command.equals("Regular"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.PLAIN,fontSize);
text.setFont(f);
}
if(command.equals("Bold"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.BOLD,fontSize);
text.setFont(f);
}
if(command.equals("Italic"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.ITALIC,fontSize);
text.setFont(f);
}
if(command.equals("Bold Italic"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.BOLD|Font.ITALIC,fontSize);
text.setFont(f);
}

if(command.equals("12"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,12);
text.setFont(f);
}

if(command.equals("14"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,14);
text.setFont(f);
}
if(command.equals("18"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,18);
text.setFont(f);
}
if(command.equals("20"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,20);
text.setFont(f);
}

if(command.equals("Background Color"))
{
Color bgColor= JColorChooser.showDialog(this,"Choose Background Color",text.getBackground());
if (bgColor != null)
{
text.setBackground(bgColor);
}
}

if(command.equals("Font Color"))
{
Color bgColor= JColorChooser.showDialog(this,"Choose Background Color",text.getBackground());
if (bgColor != null)
{
text.setForeground(bgColor);
}
}

}

public static void main(String args[])
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception xyz)
{

}
try
{
NIITProject note = new NIITProject("Untitled-Notepad");
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
note.setSize(d);
note.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
note.setVisible(true);
}
catch(Exception hello)
{

}
}
}
class Print implements Printable
{

public int print(Graphics g, PageFormat pf, int pageIndex) 
{
if (pageIndex != 0)
{ 
return NO_SUCH_PAGE;
}    
else
{
return PAGE_EXISTS;
}

}
}




Can anyone please tell e how to add undo and redo feature in this program. I have tried importing javax.swing.undo using the undo manager but it is of no use. so how do i add this feature in my editor. thanks in advance.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>