• Post Reply Bookmark Topic Watch Topic
  • New Topic

cut copy paste

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

now i face problems for copy cut paste options for a text editor
which i am making in swing.

kindly guide.

regards srijan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please describe the problem ur facing
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gowtham N:
Please describe the problem ur facing


IN CREATETOOLBAR() METHOD THE COMMENTED LINES GIVE RUNTIME ERROR.
KINDLY GUIDE
SRIJAN
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import javax.swing.text.*;
import java.util.Hashtable;
public class framework extends JFrame implements ActionListener
{
Container cc;
JToolBar bar;
public JTextArea area;
String arr1[]={"gif","jpg","tif"};
String arr2[]={"mov","avi"};
String arr3[]={"au","aiff","wav"};
public JFileChooser d;
public JMenu filemenu;
public JMenuBar mymenu;
public JMenuItem filenew;
public JMenuItem fileopen;
public JMenuItem filesave;
public JMenuItem filesaveas;
public JMenuItem filequit;
public JMenu editmenu;
public JMenuItem editundo;
public JMenuItem editcut;
public JMenuItem editcopy;
public JMenuItem editpaste;
public JFileChooser opend;
public JFileChooser saved;
Hashtable actionhash = new Hashtable();
public framework (String title)
{
area=new JTextArea();
cc=getContentPane();
cc.add(area,BorderLayout.CENTER);
cc.add(createtoolbar(),BorderLayout.NORTH);
//this.pack();
this.show();
hashdefaultactions();
make();
super.setBackground(Color.white);
super.resize(300,200);
super.setTitle(title);
this.initmenu();
addWindowListener(new eventhandler());
}

public void initmenu()
{
mymenu=new JMenuBar();
setJMenuBar(mymenu);
filemenu=new JMenu("File");
editmenu=new JMenu("Edit");
filenew= new JMenuItem("New");
filenew.addActionListener(this);
fileopen=new JMenuItem("Open");
fileopen.addActionListener(this);
filesave=new JMenuItem("Save");
filesave.addActionListener(this);
filesaveas=new JMenuItem("SaveAs");
filesaveas.addActionListener(this);
filequit=new JMenuItem("Quit");
filequit.addActionListener(this);
filemenu.add(filenew);
filemenu.add(fileopen);
filemenu.add(filesave);
/*filemenu.addSeperator();*/
filemenu.add(filesave);
filemenu.add(filequit);
mymenu.add(filemenu);
editundo=new JMenuItem("Undo");
editundo.addActionListener(this);
editcut=new JMenuItem("Cut");
editcut.addActionListener(this);
editcopy=new JMenuItem("Copy");
editcopy.addActionListener(this);
editpaste=new JMenuItem("Paste");
editpaste.addActionListener(this);
editmenu.add(editundo);
editmenu.add(editcut);
editmenu.add(editcopy);
editmenu.add(editpaste);
mymenu.add(editmenu);
}

protected void hashdefaultactions()
{
Action[] actions = area.getActions();
for(int i=0;i<actions.length;i++)<br /> {<br /> String name =(String)actions[i].getValue(Action.NAME);<br /> actionhash.put(name,actions[i]);<br /> }<br /> }<br /> <br /> <br /> public Action getact(String name)<br /> {<br /> return (Action)actionhash.get(name);<br /> }<br /> <br /> <br /> public void make()<br /> {<br /> Action a;<br /> a=getact(DefaultEditorKit.cutAction);<br /> a.putValue(Action.SMALL_ICON,new ImageIcon("cut.gif"));<br /> a.putValue(Action.NAME,"Cut");<br /> <br /> a=getact(DefaultEditorKit.copyAction);<br /> a.putValue(Action.SMALL_ICON,new ImageIcon("copy.gif"));<br /> a.putValue(Action.NAME,"Copy");<br /> <br /> a=getact(DefaultEditorKit.pasteAction);<br /> a.putValue(Action.SMALL_ICON,new ImageIcon("paste.gif"));<br /> a.putValue(Action.NAME,"Paste");<br /> <br /> a=getact(DefaultEditorKit.selectAllAction);<br /> a.putValue(Action.NAME,"Select all");<br /> }<br /> <br /> <br /> public JToolBar createtoolbar(){<br /> bar = new JToolBar();<br /> bar.addSeparator();<br /> //bar.add(getact(DefaultEditorKit.cutAction)).setText("");<br /> //bar.add(getact(DefaultEditorKit.copyAction)).setText("");<br /> //bar.add(getact(DefaultEditorKit.pasteAction)).setText("");<br /> return bar;<br /> }<br /> <br /> <br /> public void newframe(String title)<br /> {<br /> framework newf= new framework(title);<br /> newf.resize(300,200);<br /> newf.show();<br /> }<br /> <br /> <br /> public void actionPerformed(ActionEvent e)<br /> {<br /> <br /> String ss= e.getActionCommand();<br /> if(ss.equals("New"))<br /> {<br /> System.out.println("hello");<br /> newframe("new frame");<br /> }<br /> if(ss.equals("Open")) opendialog();<br /> if(ss.equals("Save")) savedialog();<br /> if(ss.equals("Quit")) System.exit(0);<br /> if(ss.equals("Undo"));<br /> if(ss.equals("Cut"));<br /> if(ss.equals("Copy"));<br /> if(ss.equals("Paste"));<br /> }<br /> <br /> public void opendialog()<br /> {<br /> JFileChooser jf = new JFileChooser();<br /> jf.setMultiSelectionEnabled(false);<br /> jf.addChoosableFileFilter(new mfilter(arr1,"Images(*.gif,*.jpg,*.tif)"));<br /> jf.addChoosableFileFilter(new mfilter(arr2,"Movies(*.mov,*.avi)"));<br /> jf.addChoosableFileFilter(new mfilter(arr3,"Audios(*.aiff,*.au,*.wav)"));<br /> int option = jf.showOpenDialog(this);<br /> if(option==JFileChooser.APPROVE_OPTION)<br /> {<br /> String str=jf.getSelectedFile().getName();<br /> System.out.println(str);<br /> try<br /> {<br /> FileReader ff = new FileReader(str);<br /> area.read(ff,null);<br /> }<br /> catch(IOException ex)<br /> {<br /> JOptionPane.showMessageDialog(this,"File not found","ERROR",JOptionPane.ERROR_MESSAGE);<br /> setVisible(true);<br /> }<br /> }<br /> }<br /> <br /> public void savedialog()<br /> {<br /> JFileChooser jf = new JFileChooser();<br /> int option = jf.showSaveDialog(this);<br /> String str=jf.getSelectedFile().getName();<br /> System.out.println(str);<br /> try<br /> {<br /> FileWriter ff = new FileWriter(str);<br /> area.write(ff);<br /> }<br /> catch(IOException ex)<br /> {<br /> }<br /> <br /> }<br /> <br /> <br /> class eventhandler extends WindowAdapter<br /> {<br /> public void windowClosing(WindowEvent e)<br /> {<br /> System.exit(0);<br /> }<br /> }<br /> public static void main(String args[])<br /> {<br /> framework mm = new framework("untitled");<br /> }<br /> }<br /> <br /> class mfilter extends FileFilter<br /> {<br /> String[] extensions;<br /> String description;<br /> public mfilter(String ext)<br /> {<br /> this(new String[]{ext},null);<br /> }<br /> public mfilter(String[] exts,String desc)<br /> {<br /> extensions=new String[exts.length];<br /> for(int i= exts.length-1;i>=0;i--)
{
extensions[i]=exts[i].toLowerCase();
}
description = (desc==null?exts[0]+"files":desc);
}


public boolean accept(File f)
{
if(f.isDirectory())
return true;

String name=f.getName().toLowerCase();

for(int i=extensions.length-1;i>=0;i--)
{
if(name.endsWith(extensions[i]))
{
System.out.println(extensions[i]);
return true;
}
}
return false;
}
public String getDescription()
{
return description;
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!