Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 7th, 2011, 4:08 PM   #1
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 8 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Copying Text in Java TextField

This has been a thorn in my side for some time now. How can I copy text from a Swing JTextField to the clipboard? I highlight the text in the JTextField and press Ctrl+C, but it won't copy the text. I would also prefer a right click context menu to allow me to right click and select all or copy.
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Aug 8th, 2011, 12:16 AM   #2
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Copying Text in Java TextField

// ClipboardTest.java
class ClipboardTest extends javax.swing.JPanel
implements java.awt.event.ActionListener
{

  javax.swing.JPopupMenu popupMenu;
  javax.swing.JTextField textField;

  public ClipboardTest()
  {
    this.setPreferredSize(new java.awt.Dimension(640,480));
    this.textField = new javax.swing.JTextField(30);
    this.add(textField);    

    createPopupMenu();
    textField.addMouseListener(new MouseHandler(popupMenu));
  }


  public void createPopupMenu()
  {
    javax.swing.JMenuItem copy = new javax.swing.JMenuItem("copy");
    javax.swing.JMenuItem paste = new javax.swing.JMenuItem("paste");
    javax.swing.JMenuItem selectAll = new javax.swing.JMenuItem("select all");

    popupMenu = new javax.swing.JPopupMenu();
    popupMenu.add( copy );
    popupMenu.add( paste );
    popupMenu.add( selectAll );

    copy.addActionListener(this);
    paste.addActionListener(this);
    selectAll.addActionListener(this);
  }


  public void actionPerformed(java.awt.event.ActionEvent e)
  {
    String cmd = e.getActionCommand();

    if (cmd.equals("copy"))
    {
      textField.copy();
    }
    else if (cmd.equals("paste"))
    {
      textField.paste();
    }
    else if (cmd.equals("select all"))
    {
      textField.selectAll();
    }
  }

  public static void main(String[] args) throws Exception
  {
    ClipboardTest panel = new ClipboardTest();
    javax.swing.JFrame window = new javax.swing.JFrame("Clipboard Test");
    window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    window.add(panel);
    window.pack();
    window.setVisible(true);
  }
}

class MouseHandler extends java.awt.event.MouseAdapter
{
  private javax.swing.JPopupMenu popupMenu;

  public MouseHandler(javax.swing.JPopupMenu popup)
  {
    this.popupMenu = popup;
  }

  public void mousePressed(java.awt.event.MouseEvent e)
  {
    if (e.isPopupTrigger())
      popupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
            
  public void mouseReleased(java.awt.event.MouseEvent e )
  {
    if (e.isPopupTrigger())
      popupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
            
}

Compile and run :
javac ClipboardTest.java
java ClipboardTest
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Aug 8th, 2011, 8:30 AM   #3
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 8 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Copying Text in Java TextField

That's a little convoluted to be putting in a small program. Is there a way to condense that?

EDIT: I think I'll edit it and take out the TextField in the class and make a constructor with a TextField parameter so you can just create a context menu object and attach it to a specific TextField.

EDIT: I got the context menu working now, but it won't let me paste the text outside of the Java applet. How can I copy the information to the clipboard?
__________________
I'm trying to change the world, but I can't find the source code...

Last edited by PaCkEtPiRaTe; Aug 8th, 2011 at 8:59 AM.
PaCkEtPiRaTe is offline   Reply With Quote
Old Aug 8th, 2011, 9:40 AM   #4
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Copying Text in Java TextField

There are two ways to access the system clipboard in an applet

1. Using a signed applet,

2. Modifying the policy file in the user computer (suitable for intranet only)
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Aug 8th, 2011, 9:43 AM   #5
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Copying Text in Java TextField

To sign an applet, see here
http://download.oracle.com/javase/1,...a_signing.html

To change policy file in the user's computer, see here
http://download.oracle.com/javase/tu...ur2/step2.html
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Aug 8th, 2011, 9:51 AM   #6
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 8 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Copying Text in Java TextField

Quote:
Originally Posted by macteki View Post
To sign an applet, see here
http://download.oracle.com/javase/1,...a_signing.html

To change policy file in the user's computer, see here
http://download.oracle.com/javase/tu...ur2/step2.html
Both of those seem a little too advanced for me. Any other ideas?
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Aug 8th, 2011, 9:53 AM   #7
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Copying Text in Java TextField

Quote:
Originally Posted by PaCkEtPiRaTe View Post
Both of those seem a little too advanced for me. Any other ideas?
Don't use an applet.

Option 1 : Make a Jar file for the user to download
Option 2 : Use Java Webstart
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Aug 8th, 2011, 10:23 AM   #8
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 8 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Copying Text in Java TextField

What's the difference between running an applet through a browser and using a JAR file? I know Java runs in a "sandbox mode" so you can't access certain parts of a user's system. Does doing that allow you to? And if so, would the methods I've already written then work? I'm testing the applet through JCreator LE's applet viewer, not a browser.
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Aug 8th, 2011, 10:31 AM   #9
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Copying Text in Java TextField

In short, running an unrestricted program in a browser is dangerous.
Therefore, if you want applet, you must tell the user your applet is safe.
By signing or by asking the user to modify his policy file.

On the other hand, downloading an executable file (or a jar file) is also dangerous, but at least it is not executed automatically, and the user knows what he is doing. He knows he is downloading some file from a web site, and he decides to take the risk running the program.

If you provide a jar file, it is just a java application. In this case, your application can access all resource of the user's computer, because it is running locally. Also because the user has decided to run it locally. He knows what he is doing. He decides to take the risk.

Search for "executable jar" for creating a jar file.
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Aug 8th, 2011, 11:24 AM   #10
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 8 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Copying Text in Java TextField

Quote:
Originally Posted by macteki View Post
In short, running an unrestricted program in a browser is dangerous.
Therefore, if you want applet, you must tell the user your applet is safe.
By signing or by asking the user to modify his policy file.

On the other hand, downloading an executable file (or a jar file) is also dangerous, but at least it is not executed automatically, and the user knows what he is doing. He knows he is downloading some file from a web site, and he decides to take the risk running the program.

If you provide a jar file, it is just a java application. In this case, your application can access all resource of the user's computer, because it is running locally. Also because the user has decided to run it locally. He knows what he is doing. He decides to take the risk.

Search for "executable jar" for creating a jar file.
Okay, but if I did the JAR file option, would the JTextField's .copy() method copy directly to the clipboard? Or would I still have to use a different method?
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
87 video Java Tutorial phys.rainbow Java 8 Feb 18th, 2012 2:45 AM
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Post&submit text to a form in java long_john321 Java 3 Aug 31st, 2005 2:54 PM
How to detect cursor location and insert text??? syntax-error C# 3 Jun 30th, 2005 1:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC