Results 1 to 7 of 7
  1. #1
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default Help!! Copy JTextField to clipboard!

    :confused:
    Hi,
    Making great progress on my Colour Picker, just wondering how i can copy my final hex value using a JButton, so the user will be able to paste it into another program.
    I have the GUI set out with the button etc, just a bit stuck on the code in which i use to copy the JTextField VIA the JButton.

    Any help will be very much appreciated!
    Thanks, Goffy :p

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

    Default

    You'll want to use a ClipBoard object and you'll get it from the Toolkit object. Then you'll want to convert your String to a StringSelection object. Something like:

    Java Code:
            private void pasteToClipBoard() {
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                Clipboard clipboard = toolkit.getSystemClipboard();
                StringSelection selection = new StringSelection(myTextField.getText());
                clipboard.setContents(selection, null);
            }
    The API for the Clipboard can help you fill in the details.

  3. #3
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You'll want to use a ClipBoard object and you'll get it from the Toolkit object. Then you'll want to convert your String to a StringSelection object. Something like:

    Java Code:
            private void pasteToClipBoard() {
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                Clipboard clipboard = toolkit.getSystemClipboard();
                StringSelection selection = new StringSelection(myTextField.getText());
                clipboard.setContents(selection, null);
            }
    The API for the Clipboard can help you fill in the details.
    Thanks for the reply, but i don't understand where my button comes into action with this?
    Thanks again

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

    Default

    Quote Originally Posted by goffy View Post
    Thanks for the reply, but i don't understand where my button comes into action with this?
    Thanks again
    If you don't understand how to use JButtons and call code from them using ActionListeners, then your best bet is to hit the Sun Swing tutorials. It's where most of us learned to code Swing.

    Best of luck.

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

    Default

    Here are some links for you:

    How to use Buttons
    How to Write an Action Listener

    Much luck!

  6. #6
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Here are some links for you:

    How to use Buttons
    How to Write an Action Listener

    Much luck!
    Thanks for the advice, will have a read now!

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

Similar Threads

  1. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  2. Copy from Clipboard in japplet
    By AZMichael in forum Java Applets
    Replies: 3
    Last Post: 09-06-2008, 03:30 PM
  3. How to copy and paste data with the clipboard
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:35 PM
  4. SWT Clipboard Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:34 PM
  5. Using the clipboard
    By Java Tip in forum java.awt.datatransfer
    Replies: 0
    Last Post: 04-16-2008, 11:52 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
  •