Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2008, 03:09 PM
smartsubroto's Avatar
Member
 
Join Date: May 2008
Location: India,Kolkatta
Posts: 44
smartsubroto is on a distinguished road
Send a message via Yahoo to smartsubroto
[SOLVED] Cut,Past,copy in edit menu
Hi all,

i just want to create an edit menu which will contain cut,copy paste,selectall option, and which will work.
Please suggest
__________________
Thanks & Regards
Subroto Bhattacharjee
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2008, 05:13 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,487
Norm is on a distinguished road
Suggestions: Read a java text book. Do a search online for sample code.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2008, 09:51 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
You can find this in TextComponentDemo.java on Text Component Features.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-02-2008, 12:25 AM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 153
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
For my diploma work at university i made java editor,so to each of JMenutItem pass with ActionListener,I attach the code in that case if you use JTextArea
actionPerformed(ActionEvent e) for cut command:
Assuming that
Code:
JTextArea textArea=new JTextArea();
Code:
//cut command if(e.getActionCommand().equals("Cut")){ String selection=textArea.getSelectedText(); if(selection==null){ return; } StringSelection clipString=new StringSelection(selection); clipboard.setContents(clipString,clipString); textArea.replaceSelection(""); }
actionPerformed(ActionEvent e) for copy command:
Code:
//copy command if(e.getActionCommand().equals("Copy")){ String selection =textArea.getSelectedText(); if(selection==null){ return; } StringSelection clipString=new StringSelection(selection); clipboard.setContents(clipString, clipString); }
actionPerformed(ActionEvent e) for paste command:
Code:
//paste command if(e.getActionCommand().equals("Paste")){ Transferable clip_data=clipboard.getContents(this); try{ String clip_string=(String)clip_data.getTransferData(DataFlavor.stringFlavor); textArea.replaceSelection(clip_string); }catch(Exception excpt){ } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-02-2008, 12:27 AM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 153
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
The selection option for JTextArea is :
Code:
textArea.selectAll(); String selectedText=textArea.getSelectedText();
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-02-2008, 03:25 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 199
Eku is on a distinguished road
there is already a function for cut-copy-paste in java. you just need to call it ^_^
I also made a javapad almost like notepad but can compile and run java ^_^
But cant handle errors T_T

Here is a sample code from my javapad, it is for cut
Code:
Action cutAction = textArea.getActionMap().get(DefaultEditorKit.cutAction); cutItem = editMenu.add(cutAction); cutItem.setText("Cut");
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.

Last edited by Eku : 07-02-2008 at 03:27 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-02-2008, 12:11 PM
smartsubroto's Avatar
Member
 
Join Date: May 2008
Location: India,Kolkatta
Posts: 44
smartsubroto is on a distinguished road
Send a message via Yahoo to smartsubroto
Ya i know how to create Edit option for TextArea.
but i want to do something different.
For example if there is a 10 fields, then this TextArea concept will not work cause we dont know, in which field my cursor is and since it contains 10 fields, so it is not possible to check all the fields .
Please suggest.. me for creating an edit menu in the JFrame so that it can be implemented in all the fields.
__________________
Thanks & Regards
Subroto Bhattacharjee
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-02-2008, 03:01 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,487
Norm is on a distinguished road
Would the cursor be in the component that has the focus?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-02-2008, 09:16 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
TextAction was designed for this. The nested classes in DefaultEditorKit extend it.
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class TextFocus { private JMenuBar getMenuBar() { JMenu menu = new JMenu("edit"); //DefaultEditorKit kit = new DefaultEditorKit(); //Action[] actions = kit.getActions(); //for(int i = 0; i < actions.length; i++) // System.out.println(actions[i].getValue(Action.NAME)); Action action = new DefaultEditorKit.CutAction(); action.putValue(Action.NAME, "cut"); menu.add(action); action = new DefaultEditorKit.CopyAction(); action.putValue(Action.NAME, "copy"); menu.add(action); action = new DefaultEditorKit.PasteAction(); action.putValue(Action.NAME, "paste"); menu.add(action); menu.add(new SelectAction()); JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); return menuBar; } private class SelectAction extends TextAction { SelectAction() { super(DefaultEditorKit.selectAllAction); super.putValue(NAME, "selectAll"); } public void actionPerformed(ActionEvent e) { getFocusedComponent().selectAll(); } } private JPanel getContent() { String[] strs = { "red", "green", "yellow", "blue" }; JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1.0; gbc.weighty = 1.0; for(int i = 0; i < 4; i++) { JTextField tf = new JTextField(strs[i], 12); gbc.gridwidth = ((i+1) % 2 == 0) ? gbc.REMAINDER : 1; panel.add(tf, gbc); } return panel; } public static void main(String[] args) { TextFocus test = new TextFocus(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setJMenuBar(test.getMenuBar()); f.add(test.getContent()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-03-2008, 11:15 AM
smartsubroto's Avatar
Member
 
Join Date: May 2008
Location: India,Kolkatta
Posts: 44
smartsubroto is on a distinguished road
Send a message via Yahoo to smartsubroto
Thanks Hardwired,

This code really works.It helped me alot.
__________________
Thanks & Regards
Subroto Bhattacharjee
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
React to menu action and checkbox menu Java Tip javax.swing 0 06-27-2008 08:50 PM
how to create Popup Menu with Sub Menu while right-clicking the JTree Node?? Kabiraa AWT / Swing 7 05-09-2008 08:54 AM
TextArea additable and uneditable (copy/past problem) qwerty55 Advanced Java 0 01-20-2008 12:41 AM
how to edit lines. jason27131 New To Java 1 08-01-2007 05:41 AM
How To Edit/Add JSP Pages in NetBeans IDE JavaForums NetBeans 0 07-31-2007 12:13 AM


All times are GMT +3. The time now is 09:07 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org