UR a Genius...If u solve my problem.besides the reward is this cool program
Well, if u copy something from the textarea and paste agin on the textarea it does not respond.How ever, if u paste somewhere else on the system it works perfectly.please....help..and one more thing can anyone tell me how u underline something in JTextArea and also superscript...any help wud be welcomed.
I have worked hard on this and i have to submit my project in a couple of days so please....
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
publicclass notepadimplements ActionListener
{
JMenuItem i2,i3,i6,n19,n20,n21;
JCheckBoxMenuItem u1;
JPanel p=new JPanel();
JFrame frame;
JTextArea ta;
JPopupMenu pm;
String copy,copy1;
GregorianCalendar calendar;
static String strdate;
String strtime,strstore;
JTextField t1;
JToolBar tb1;
JButton b1,b2,b3;
JComboBox c1,c2,c3;
static Font f;
staticchar a,b;
String fontlist[];
String msg;
boolean modal =false;
JColorChooser chooser;
JDialog dialog;
static Color newColor;
public notepad()
{
ta=new JTextArea("",40,80);
JFrame.setDefaultLookAndFeelDecorated(true);
frame=new JFrame("Untitled-Pilotpad");
t1=new JTextField("STATUS BAR");
tb1=new JToolBar(null,JToolBar.HORIZONTAL);
tb1.setFloatable(false);
tb1.setBackground(Color.white);
b1=new JButton("B");
b1.setToolTipText("BOLD");
b1.setBackground(Color.white);
b1.addActionListener(this);
b2=new JButton("I");
b2.setBackground(Color.white);
b2.setToolTipText("ITALIC");
b2.addActionListener(this);
b3=new JButton("P");
b3.setBackground(Color.white);
b3.setToolTipText("PLAIN");
b3.addActionListener(this);
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
fontlist=ge.getAvailableFontFamilyNames();
c1=new JComboBox(fontlist);
c1.setToolTipText("FONT");
c1.addActionListener(this);
c1.setMaximumSize(c1.getMinimumSize());
String size[]={"10","12","14","16","18","20","22","24","26","28","30","32","34","36","40","46","52","60","72","84","90","100"};
c2=new JComboBox(size);
c2.setToolTipText("SIZE");
c2.addActionListener(this);
c2.setMaximumSize(c2.getMinimumSize());
String colour[]={"","From ColorChooser","black","blue","cyan","darkGray","gray","green","lightGray","magenta","orange","pink","red","white","yellow"};
c3=new JComboBox(colour);
c3.setToolTipText("COLOUR");
c3.addActionListener(this);
c3.setMaximumSize(c3.getMinimumSize());
tb1.add(b1);
tb1.add(b2);
tb1.add(b3);
tb1.addSeparator();
tb1.add(c1);
tb1.addSeparator();
tb1.add(c2);
tb1.addSeparator();
tb1.add(c3);
//right click start
pm=new JPopupMenu();
JMenuItem i1=new JMenuItem("Select All");
i2=new JMenuItem("Copy");
i3=new JMenuItem("Paste");
JMenuItem i4=new JMenuItem("Change Case");
JMenuItem i5=new JMenuItem("Delete");
i6=new JMenuItem("Cut");
i1.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
ta.selectAll();
}});
i4.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
String gettingAll=ta.getSelectedText();
StringBuffer sf=new StringBuffer(gettingAll);
char ch=sf.charAt(0);
int ch1;
ch1=(int) ch;
System.out.println(ch1);
if((ch1>=65) && (ch1<=92))
{
String settingAll=gettingAll.toLowerCase();
ta.setText(settingAll);
}
if((ch1>=93) && (ch1<=120))
{
String settingAll=gettingAll.toUpperCase();
ta.setText(settingAll);
}
}});
i5.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
ta.replaceSelection("");
}});
i2.addActionListener(this);
i2.addActionListener(this);
i6.addActionListener(this);
pm.add(i1);
pm.add(i2);
pm.add(i3);
pm.add(i4);
pm.add(i5);
pm.add(i6);
JMenuBar m1=new JMenuBar();
m1.setForeground(Color.white);
//1.File start
JMenu e1=new JMenu("File");
e1.setBackground(Color.white);
JMenuItem n1=new JMenuItem("New");
n1.setBackground(Color.white);
n1.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
frame.setTitle("New");
ta.setText("");
}});
JMenuItem n2=new JMenuItem("Open...");
n2.setBackground(Color.white);
n2.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(new JFrame(),"Select a File:",FileDialog.LOAD);
fd.setVisible(true);
String dir=fd.getDirectory();
String file=fd.getFile();
String filename=dir+file;
try
{
FileReader fr=new FileReader(filename);
BufferedReader br1=new BufferedReader(fr);
String s;
while((s=br1.readLine())!=null)
{
String get=ta.getText();
ta.setText(get+s+'\n');
}
fr.close();
}
catch(Exception es)
{}
}
});
JMenuItem n3=new JMenuItem("Save");
n3.setBackground(Color.white);
n3.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(new JFrame(),"Name of the File:",FileDialog.SAVE);
fd.setVisible(true);
String dir=fd.getDirectory();
String file=fd.getFile();
String filename=dir+file;
String source=ta.getText();
char buffer[]=newchar[source.length()];
source.getChars(0,source.length(),buffer,0);
try
{
FileWriter f1=new FileWriter(filename);
f1.write(buffer);
f1.close();
}
catch(IOException fds)
{}
}
});
JMenuItem n4=new JMenuItem("Save As...");
n4.setBackground(Color.white);
n4.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(new JFrame(),"Name of the File:",FileDialog.SAVE);
fd.setVisible(true);
String dir=fd.getDirectory();
String file=fd.getFile();
String filename=dir+file;
String source=ta.getText();
char buffer[]=newchar[source.length()];
source.getChars(0,source.length(),buffer,0);
try
{
FileWriter f1=new FileWriter(filename);
f1.write(buffer);
f1.close();
}
catch(IOException fds)
{}
}
});
JMenuItem n5=new JMenuItem("Page Setup...");
n5.setBackground(Color.white);
JMenuItem n6=new JMenuItem("Print");
n6.setBackground(Color.white);
JMenuItem n7=new JMenuItem("Exit");
n7.setBackground(Color.white);
n7.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
//1.File end
//2.Edit start
JMenu e2=new JMenu("Edit");
e2.setBackground(Color.white);
JMenuItem n8=new JMenuItem("Undo");
n8.setBackground(Color.white);
JMenuItem n9=new JMenuItem("Cut");
n9.setBackground(Color.white);
n9.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
copy=ta.getSelectedText();
ta.setText("");
}});
JMenuItem n10=new JMenuItem("Copy");
n10.setBackground(Color.white);
n10.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
copy=ta.getSelectedText();
}});
JMenuItem n11=new JMenuItem("Paste");
n11.setBackground(Color.white);
n11.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
if(ta.equals(""))
{
ta.setText(copy);
}
else
copy1=ta.getText();
ta.setText(copy1+copy);
}});
JMenuItem n12=new JMenuItem("Delete");
n12.setBackground(Color.white);
n12.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
ta.replaceSelection("");
}});
JMenuItem n13=new JMenuItem("Find");
n13.setBackground(Color.white);
JMenuItem n14=new JMenuItem("Replace");
n14.setBackground(Color.white);
JMenuItem n15=new JMenuItem("Go to");
n15.setBackground(Color.white);
JMenuItem n16=new JMenuItem("Select All");
n16.setBackground(Color.white);
JMenuItem n17=new JMenuItem("Date/Time");
n17.setBackground(Color.white);
n17.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
calendar=new GregorianCalendar();
strdate=(calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);
strtime=calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
strstore=("Date:"+strdate+" Time:"+strtime);
String getting=ta.getText();
ta.setText(getting+""+strstore);
}});
//2.edit end
//3.format start
JMenu e3=new JMenu("Format");
e3.setBackground(Color.white);
JMenuItem n18=new JMenuItem("ShortCut to Google.com");
n18.setBackground(Color.white);
n18.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
try{
Runtime.getRuntime().exec("explorer http://www.google.com");
}
catch(Exception ae)
{
System.out.println(ae);
}
}});
Color initialColor = Color.red;
chooser=new JColorChooser(initialColor);
dialog = JColorChooser.createDialog(null,"Dialog Title", modal,chooser,okListener,cancelListener);
n19=new JMenuItem("ColorChooser");
n19.setBackground(Color.white);
n19.addActionListener(this);
dialog.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent evt){
// getColor() returns the latest selected color.
newColor = chooser.getColor();
c3.setSelectedIndex(1);
}}) ;
//3.format end
//4.view start
JMenu e4=new JMenu("View");
e4.setBackground(Color.white);
u1=new JCheckBoxMenuItem("Status Bar");
u1.addItemListener(new ItemListener(){
publicvoid itemStateChanged(ItemEvent e){
Object source=e.getSource();
if(source==u1){
if(u1.getState())
{
t1.setEnabled(false);
t1.setForeground(Color.black);
frame.getContentPane().add(t1,BorderLayout.SOUTH);
int a=frame.getHeight();
int b=frame.getWidth();
a++;b++;
frame.setSize(b,a);
}
else
{
frame.getContentPane().remove(t1);
int a=frame.getHeight();
int b=frame.getWidth();
a++;b++;
frame.setSize(b,a);
}
}
}
});
u1.setBackground(Color.white);
//view end
//help start
JMenu e5=new JMenu("Help");
e5.setBackground(Color.white);
n20=new JMenuItem("Topics");
n20.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==n20){
JOptionPane.showMessageDialog(frame,new String("PilotPad is much like notepad with some extra features.\nSend your feedback at unsafe_pilot1@yahoo.com"));
}
}
});
n20.setBackground(Color.white);
n21=new JMenuItem("About PilotPad!");
n21.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(frame,new String("PilotPad Version 1.0\nCopyright@Ashutosh khanna 2005"));
}});
n21.setBackground(Color.white);
//help end
e1.add(n1);
e1.add(n2);
e1.add(n3);
e1.addSeparator();
e1.add(n4);
e1.add(n5);
e1.add(n6);
e1.addSeparator();
e1.add(n7);
e2.add(n8);
e2.addSeparator();
e2.add(n9);
e2.add(n10);
e2.add(n11);
e2.add(n12);
e2.addSeparator();
e2.add(n13);
e2.add(n14);
e2.add(n15);
e2.addSeparator();
e2.add(n16);
e2.add(n17);
e3.add(n18);
e3.add(n19);
e4.add(u1);
e5.add(n20);
e5.add(n21);
m1.add(e1);
m1.add(e2);
m1.add(e3);
m1.add(e4);
m1.add(e5);
frame.setJMenuBar(m1);
//Textarea
ta.addMouseListener(new MouseAdapter(){
publicvoid mouseReleased(MouseEvent e){
if(e.isPopupTrigger()){
pm.show(e.getComponent(),e.getX(),e.getY());
}}});
JScrollPane jsp=new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(tb1,BorderLayout.NORTH);
frame.getContentPane().add(jsp);
//end
frame.setSize(1000,750);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
ActionListener okListener =new ActionListener(){
// Called when user clicks ok
publicvoid actionPerformed(ActionEvent evt){
// Get selected color
newColor = chooser.getColor();
c3.setSelectedIndex(1);
}
};
ActionListener cancelListener =new ActionListener(){
// Called when user clicks cancel
publicvoid actionPerformed(ActionEvent evt){
// getColor() returns the latest selected color.
newColor=chooser.getColor();
c3.setSelectedIndex(1);
}
};
publicstaticvoid main(String args[])
{
new notepad();
}
publicvoid actionPerformed(ActionEvent e)
{
String style=(String)c1.getSelectedItem();
String size=(String)c2.getSelectedItem();
int size1=Integer.parseInt(size);
if(e.getSource()==b1)
{
if((a==3)&&(b==3))
{
f=new Font(style,Font.BOLD,size1);
}
elseif((a==2)&&(a==2))
{
f=new Font(style,Font.ITALIC|Font.BOLD,size1);
}
else
{
f=new Font(style,Font.BOLD,size1);
}
a=1;
b=1;
}
elseif(e.getSource()==b2)
{
if((a==3)&&(b==3))
{
f=new Font(style,Font.ITALIC|Font.PLAIN,size1);
}
elseif((a==1)&&(b==1))
{
f=new Font(style,Font.BOLD|Font.ITALIC,size1);
}
else
{
f=new Font(style,Font.ITALIC,size1);
}
a=2;
b=2;
}
elseif(e.getSource()==b3)
{
if((a==2)&&(b==2))
{
f=new Font(style,Font.PLAIN|Font.ITALIC,size1);
}
else
{
f=new Font(style,Font.PLAIN,size1);
}
a=3;
b=3;
}
elseif(e.getSource()==c1)
{
f=new Font(style,Font.PLAIN,size1);
if(a==1)
{
f=new Font(style,Font.BOLD,size1);
}
elseif(a==2)
{
f=new Font(style,Font.ITALIC,size1);
}
elseif(a==3)
{
f=new Font(style,Font.PLAIN,size1);
}
}
elseif(e.getSource()==c2)
{
f=new Font(style,Font.PLAIN,size1);
if(a==1)
{
f=new Font(style,Font.BOLD,size1);
}
elseif(a==2)
{
f=new Font(style,Font.ITALIC,size1);
}
elseif(a==3)
{
f=new Font(style,Font.PLAIN,size1);
}
}
elseif(e.getSource()==c3)
{
f=new Font(style,Font.PLAIN,size1);
if(a==1)
{
f=new Font(style,Font.BOLD,size1);
}
elseif(a==2)
{
f=new Font(style,Font.ITALIC,size1);
}
elseif(a==3)
{
f=new Font(style,Font.PLAIN,size1);
}
}
elseif(e.getSource()==n19)
{
dialog.setVisible(true);
}
ta.setFont(f);
String colour=(String)c3.getSelectedItem();
if(colour.equals("black"))
{
ta.setForeground(Color.black);
}
elseif(colour.equals("blue"))
{
ta.setForeground(Color.blue);
}
elseif(colour.equals("cyan"))
{
ta.setForeground(Color.cyan);
}
elseif(colour.equals("darkGray"))
{
ta.setForeground(Color.darkGray);
}
elseif(colour.equals("gray"))
{
ta.setForeground(Color.gray);
}
elseif(colour.equals("green"))
{
ta.setForeground(Color.green);
}
elseif(colour.equals("lightGray"))
{
ta.setForeground(Color.lightGray);
}
elseif(colour.equals("magenta"))
{
ta.setForeground(Color.magenta);
}
elseif(colour.equals("orange"))
{
ta.setForeground(Color.orange);
}
elseif(colour.equals("pink"))
{
ta.setForeground(Color.pink);
}
elseif(colour.equals("red"))
{
ta.setForeground(Color.red);
}
elseif(colour.equals("white"))
{
ta.setForeground(Color.white);
}
elseif(colour.equals("yellow"))
{
ta.setForeground(Color.yellow);
}
elseif(colour.equals("From ColorChooser"))
{
ta.setForeground(newColor);
}
Clipboard clipboard=frame.getToolkit().getSystemClipboard();
if (e.getSource()==i2){
StringSelection data =new StringSelection(ta.getText());
clipboard.setContents(data, data);
}
elseif (e.getSource()==i3){
Transferable clipData = clipboard.getContents(this);
String s;
try{
s = (String)(clipData.getTransferData(DataFlavor.stringFlavor));
}
catch (Exception ae){
s =ae.toString();
}
ta.setText(s);
}
elseif (e.getSource()==i6){
StringSelection data =new StringSelection(ta.getText());
clipboard.setContents(data, data);
ta.setText("");
}
}
}

