Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: program a button that copy whatever in a JTextArea

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default program a button that copy whatever in a JTextArea

    Say there are a JButton called copy, and a JTextArea called txtArea, once the button clicked a copy of whatever in the JTextArea is, moved to the clipboard so that I can paste it to any text editor (e.g. notepad)

    Within the listener ; what is the required code


    [COLOR="Black"]   private class ButtonWatcher implements ActionListener
       {
     
          public void actionPerformed(ActionEvent a)
          {
             // possible code?
           }
     
       }[/COLOR]



  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: program a button that copy whatever in a JTextArea

    JTextArea inherits a copy() method from JTextComponent, so just invoke this method:
    textarea.copy();
    Remember that if there is a selection the copy() method only copies that portion.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: program a button that copy whatever in a JTextArea

    Quote Originally Posted by copeg View Post
    JTextArea inherits a copy() method from JTextComponent, so just invoke this method:
    textarea.copy();
    Remember that if there is a selection the copy() method only copies that portion.
    I used it, no errors, but after clicking the JButton, I expected to be able to right-click on any text editor and normally paste whatever copied from the JTextArea, but that doen't happen

    This is the full code for the class:
    [COLOR="Black"]
    package numbergeneratorchecker;
     
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class NewClass extends JFrame
    {
     
       private JButton click_1;
       private JButton click_2;
       [COLOR="DarkOrange"]private JButton copyy;[/COLOR]
     
       private Panel pn1;
       private Panel pn2;
       private JTextArea txtArea;
       private JScrollPane scrollTxtArea;
     
     
       public NewClass()
       {
          setSize(300, 200);
          setTitle("Number Gererator");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
     
          /********** 2 panels ******************/
          pn1 = new Panel();
          pn2 = new Panel();
     
          /********** 3 button ******************/
          click_1 = new JButton("click_1");
          click_2 = new JButton("click_2");
    [COLOR="darkorange"]      copyy = new JButton("copy");[/COLOR]
     
          /********** text area ******************/
          txtArea = new JTextArea(2, 22);
          scrollTxtArea = new JScrollPane(txtArea);
     
     
     
     
     
          /***********  Interface  *******/
          Container cp = getContentPane();
          cp.setLayout(new GridLayout(3,1));
     
          /***** 2 panels ****/
          cp.add(pn1);
          cp.add(pn2);
     
     
          click_1.addActionListener(new ButtonWatcher());
          click_2.addActionListener(new ButtonWatcher());
          [COLOR="DarkOrange"]copyy.addActionListener(new ButtonWatcher());[/COLOR]
     
          /********** button ******************/
          pn1.add(click_1);
          pn1.add(click_2);
     [COLOR="DarkOrange"]     pn1.add(copyy);[/COLOR]
     
          /******* text area ******/
          pn2.add(scrollTxtArea);
     
       }
       private class ButtonWatcher implements ActionListener
       {
          public void actionPerformed(ActionEvent a)
          {
     
             Object buttonPressed = a.getSource();
             if (buttonPressed.equals(click_1))
             {
                txtArea.setText(" Click_1 is pressed ");
             }
             if (buttonPressed.equals(click_2))
             {
                txtArea.setText(" click_2 is pressed ");
             }
    [COLOR="darkorange"]         if (buttonPressed.equals(copyy))
             {
                txtArea.copy();
             }[/COLOR]
          }
       }
    }[/COLOR]

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: program a button that copy whatever in a JTextArea

    Remember that if there is a selection the copy() method only copies that portion.
    Do you mean that this method only works when there is a selected text, and doesn't work otherwise? Ok I have tried both ways (with selected and unselected text) but no result

    If it is the case, then is there a way to copy whatever in JTextArea with just clicking a button without a selection… if it is complex to do that, just correct my use to copy() method .

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: program a button that copy whatever in a JTextArea

    Read the API for the JTextComponent (Java 2 Platform SE 5.0)
    It only works for the selected text. A workaround is to save the selection, select all, call copy, then reset the original selection. I haven't implemented something like that in a while but it should work. The alternative is to make calls directly to the Clipboard. You can find many solutions to this by googling "java Clipboard copy paste"

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: program a button that copy whatever in a JTextArea

    If you go the clipboard way, you might want to take a look at the API for java.awt.datatransfer.StringSelection

    db

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. Copy folder based on latest date
    By ayonsoni in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 17th, 2010, 07:01 AM
  3. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  4. Convert contents of JTextArea / JEditorPane to PDF
    By rangarajank in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 30th, 2009, 02:38 PM
  5. Replies: 1
    Last Post: May 21st, 2009, 03:41 AM