Results 1 to 15 of 15
Like Tree4Likes
  • 1 Post By Fubarable
  • 2 Post By camickr
  • 1 Post By Fubarable

Thread: cut/copy/paste in JTextField

  1. #1
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Unhappy cut/copy/paste in JTextField

    here r some problems...
    when i'm writing tf.cut(); before tf.copy(); then only cut method works not copy. & same happens in vice versa.

    also paste method is not working.
    see bold part of the program.

    can anyone please help...??




    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class Assign1 implements ActionListener
    {
    	
    	JFrame fr;
    	JTextField tf;
    	JButton b1,b2,b3;
    	
    	Assign1()
    	{
    		fr=new JFrame();
    		fr.setLayout(null);
    		
    		tf=new JTextField();
    		b1=new JButton("cut");
    		b2=new JButton("copy");
    		b3=new JButton("paste");
    		
    		tf.setBounds(20,35,200,30);
    		b1.setBounds(20,80,55,50);
    		b2.setBounds(80,80,65,50);
    		b3.setBounds(160,80,75,50);
    		
    		fr.add(b1);
    		fr.add(b2);
    		fr.add(b3);
    		fr.add(tf);
    		
    		fr.setSize(250,250);
    		fr.setVisible(true);
    		
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    		
    		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    	}
    	
    	public void actionPerformed(ActionEvent arg0) {
    		
    	[B]tf.setSelectionStart(5);
    	tf.setSelectionEnd(10);
    	
    	tf.cut();
    	tf=new JTextField();
    	tf.copy();
    	tf=new JTextField();
    	tf.getSelectedText();
    	tf.paste();
    	tf=new JTextField();
    	[/B]
    	}
    	
    	public static void main(String[] args) {
    	
    		new Assign1();
    	}
    
    }
    Last edited by Fubarable; 09-24-2011 at 09:36 PM. Reason: Moderator edit: code tags added to improve readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: cut/copy/paste in JTextField

    Quote Originally Posted by gauravmanral View Post
    [B]here r some problems...
    when i'm writing tf.cut(); before tf.copy(); then only cut method works not copy. & same happens in vice versa.
    Moderator edit: code tags added to original post to allow code to retain formatting and be readable.

    • Why are you creating new JTextFields and assigning them to tf? What is the purpose of creating a textfield that is never displayed and contains no text?
    • And when you fix the above issue, a next issue is do you know what cut does with the selected text when its called? The API will answer this for you.
    camickr likes this.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: cut/copy/paste in JTextField

    Creating new text fields won't do anything. The text field is not automatically added to the GUI.

    I would suggest for your test you create 2 text fields that are visible on the GUI. Then you cut/copy/paste from the first text field to the second text field.

    Edit:

    Fubarable is the faster typist :)
    Last edited by camickr; 09-24-2011 at 09:44 PM.
    Fubarable and gauravmanral like this.

  4. #4
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Default Re: cut/copy/paste in JTextField

    that sounds gud camickr...

    and also how this paste will work...??

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: cut/copy/paste in JTextField

    Quote Originally Posted by gauravmanral View Post
    and also how this paste will work...??
    Please elaborate.

  6. #6
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Default Re: cut/copy/paste in JTextField

    yes i got it...
    thanks for ur support....

  7. #7
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Default Re: cut/copy/paste in JTextField

    oops...
    paste method is working but what about cut and copy.
    still if i'm writing cut method first then copy is also working like cut. and same in case of vice versa...

    what to do with with that...?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: cut/copy/paste in JTextField

    Quote Originally Posted by gauravmanral View Post
    oops...
    paste method is working but what about cut and copy.
    still if i'm writing cut method first then copy is also working like cut. and same in case of vice versa...

    what to do with with that...?
    Again, please elaborate. Show us your latest code, preferably a smallest compilable and runnable example that demonstrates your problem, that we can run, and that contains no extraneous code unrelated to the problem at hand -- an SSCCE. Also, please use code tags when posting code:

    [code]
    your code goes here
    [/code]
    Last edited by Fubarable; 09-24-2011 at 10:28 PM.

  9. #9
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Default Re: cut/copy/paste in JTextField

    this is the code.
    i don't understand how this paste method is working or works...
    please help.
    i want, when i click on cut it simply cut it from tf1; when i click on copy, it only copy the text from tf1. & on clicking on paste it will paste it in tf2 TextField.


    <code>
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class Assign1 implements ActionListener
    {

    JFrame fr;
    JTextField tf1,tf2;
    JButton b1,b2,b3;

    Assign1()
    {
    fr=new JFrame();
    fr.setLayout(null);

    tf1=new JTextField();
    tf2=new JTextField();
    b1=new JButton("cut");
    b2=new JButton("copy");
    b3=new JButton("paste");

    tf1.setBounds(20,35,100,30);
    tf2.setBounds(130,35,100,30);
    b1.setBounds(20,80,55,50);
    b2.setBounds(80,80,65,50);
    b3.setBounds(150,80,70,50);

    fr.add(b1);
    fr.add(b2);
    fr.add(b3);
    fr.add(tf1);
    fr.add(tf2);

    fr.setSize(250,250);
    fr.setVisible(true);

    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);

    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent ae) {

    tf1.setSelectionStart(5);
    tf1.setSelectionEnd(10);

    tf1.cut();
    tf1.copy();
    tf2.getSelectedText();
    tf2.paste();

    }

    public static void main(String[] args) {

    new Assign1();
    }

    }
    </code>
    Last edited by gauravmanral; 09-25-2011 at 05:30 AM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: cut/copy/paste in JTextField

    Please edit your post above and place the code tags in as I described in post number 8 in this thread.

    Also, why are you calling copy() after calling cut()?

  11. #11
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Default Re: cut/copy/paste in JTextField

    i want to work all 3 methods separately that's why i called copy method too.

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: cut/copy/paste in JTextField

    i want, when i click on cut it simply cut it from tf1; when i click on copy, it only copy the text from tf1. & on clicking on paste it will paste it in tf2 TextField.
    Then you need 3 separate ActionListeners. For example to do the copy you can use something like:

    Java Code:
    b1.addActionListener( new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            tf1.copy();
        }
    });
    Also learn how to use LayoutManagers. You should not be using a null layout to position components on a GUI.
    Last edited by camickr; 09-25-2011 at 05:37 AM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: cut/copy/paste in JTextField

    Quote Originally Posted by gauravmanral View Post
    <code>
    ...
    </code>
    !!???

    To do well at programming, you need to be good at taking directions. :)
    Please have another look at my recommendations in post #8.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: cut/copy/paste in JTextField

    Myself, I'd use AbstractActions, but the result is the same...

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class CutCopyPaste extends JPanel {
       private JTextField srcField = new JTextField(10);
       private JTextField destField = new JTextField(10);
       
       public CutCopyPaste() {
          JPanel topPanel = new JPanel();
          topPanel.add(srcField);
          topPanel.add(destField);
          
          JPanel bottomPanel = new JPanel(new GridLayout(1, 0, 10, 0));
          bottomPanel.add(new JButton(new CutAction()));
          bottomPanel.add(new JButton(new CopyAction()));
          bottomPanel.add(new JButton(new PasteAction()));
          
          int eb = 5;
          setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
          setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
          add(topPanel);
          add(bottomPanel);
       }
       
       private class CutAction extends AbstractAction {
          public CutAction() {
             super("Cut");
          }
          
          @Override
          public void actionPerformed(ActionEvent e) {
             srcField.cut();
          }
       }
    
       private class CopyAction extends AbstractAction {
          public CopyAction() {
             super("Copy");
          }
          
          @Override
          public void actionPerformed(ActionEvent e) {
             srcField.copy();
          }
       }
    
       private class PasteAction extends AbstractAction {
          public PasteAction() {
             super("Paste");
          }
          
          @Override
          public void actionPerformed(ActionEvent e) {
             destField.paste();
          }
       }
    
       private static void createAndShowGui() {
          CutCopyPaste mainPanel = new CutCopyPaste();
    
          JFrame frame = new JFrame("CutCopyPaste");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    But again to use code tags here on this forum, you need to use square brackets [], not angle brackets <>. For the benefit of future readers of this forum, please re-edit your previous posts to add the appropriate tags. I've already done so for your first post; please do it for the rest.
    camickr likes this.

  15. #15
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: cut/copy/paste in JTextField

    Yes, learning about extending AbstractAction is a better solution in the long run.
    Last edited by camickr; 09-25-2011 at 07:12 AM.

Similar Threads

  1. JTable copy and paste problem on Mac
    By willlotr in forum AWT / Swing
    Replies: 4
    Last Post: 04-07-2011, 07:15 PM
  2. Copy, Cut and Paste Problems!
    By bones in forum New To Java
    Replies: 4
    Last Post: 07-17-2009, 10:18 AM
  3. Cut, copy and Paste in JTextPane
    By Gudradain in forum AWT / Swing
    Replies: 1
    Last Post: 01-03-2009, 06:43 AM
  4. Problems with copy paste
    By fredand44 in forum Eclipse
    Replies: 0
    Last Post: 12-17-2008, 04:14 PM
  5. Replies: 0
    Last Post: 07-07-2008, 04:35 PM

Posting Permissions

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