Results 1 to 10 of 10
  1. #1
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default [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:)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Suggestions: Read a java text book. Do a search online for sample code.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    You can find this in TextComponentDemo.java on Text Component Features.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    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
    Java Code:
    JTextArea textArea=new JTextArea();
    Java 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:
    Java 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:
    Java 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){
    				
    			}
    		}

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    The selection option for JTextArea is :
    Java Code:
    textArea.selectAll();
    String selectedText=textArea.getSelectedText();

  6. #6
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    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
    Java Code:
    Action cutAction = textArea.getActionMap().get(DefaultEditorKit.cutAction);
    cutItem = editMenu.add(cutAction);
    cutItem.setText("Cut");
    Last edited by Eku; 07-02-2008 at 02:27 AM.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  7. #7
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    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:)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Would the cursor be in the component that has the focus?

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    TextAction was designed for this. The nested classes in DefaultEditorKit extend it.
    Java 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);
        }
    }

  10. #10
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    Thanks Hardwired,

    This code really works.It helped me alot.
    Thanks & Regards
    Subroto Bhattacharjee:)

Similar Threads

  1. How To Edit/Add JSP Pages in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 2
    Last Post: 02-17-2009, 11:14 AM
  2. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  3. Replies: 7
    Last Post: 05-09-2008, 07:54 AM
  4. TextArea additable and uneditable (copy/past problem)
    By qwerty55 in forum Advanced Java
    Replies: 0
    Last Post: 01-19-2008, 11:41 PM
  5. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •