Override paste function jtextpane

Hello,
Is there a way how i can override the default paste function in a JTextPane?
My goal, paste the cilpboard in the JTextPane in a formatted (with styles) way.
maybe these code -snips can help:
                logPane = new JTextPane();
          logPane.setContentType("text/plain");
          doc = new DefaultStyledDocument();
          //ADD Key listener (paste)
          logPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V,0,true),"pasteKey");
          logPane.getActionMap().put("pasteKey", pasteKey);
Action pasteKey = new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
                  logger.debug("Pushed paste");
Edited by: Mooner on Apr 21, 2009 9:47 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Camickr,
Thanks for the reply!
Indeed the paste-function is done by Control-V in a JTextPane, but that just the thin i want to override.
When i paste (press ctrl+v) i want to first format the text (with the addtext() methode) before paste it to the JTextPane
I've create a SSCCE:
package es.jes.LogViewer.gui.logPanel;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import es.jes.LogViewer.gui.MainGui;
import es.jes.LogViewer.gui.logPanel.tail.fileTailer;
public class SSCCELogField extends JFrame {
     private StyledDocument doc;
     private JTextPane logPane;
     private File logFileName;
     private fileTailer tailer;
     private Style currentUsedStyle;
     private int lineNr = 0;
     public SSCCELogField(File logFileName){          
          super();
          this.logFileName = logFileName;
          logPane = new JTextPane();
          logPane.setContentType("text/plain");
          doc = new DefaultStyledDocument();
          //ADD Key listener (paste)
          logPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V,0,true),"pasteKey");
          logPane.getActionMap().put("pasteKey", pasteKey);
          initStyles();
          //SET initial style
          currentUsedStyle = doc.getStyle("warnStyle");
          JScrollPane scrollPaneLogField = new JScrollPane(logPane);     
          this.add(scrollPaneLogField);
          this.setSize(800, 600);
          readLogFile();          
          logPane.setStyledDocument(doc);
      Action pasteKey = new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("Pushed paste");
     private void readLogFile() {
        BufferedReader fileReader = null;
        try {
            fileReader = new BufferedReader(new FileReader(logFileName));
            String line;
            while( (line = fileReader.readLine()) != null ){
                addText(line);
                line = fileReader.readLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        finally {
            if (fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException exp) {
                    exp.printStackTrace();
     public void addText(String text){
          lineNr++;     
          if (text.startsWith("E")){
                currentUsedStyle = doc.getStyle("errorStyle");
           else if (text.startsWith("I")){
                currentUsedStyle = doc.getStyle("infoStyle");
           else if (text.startsWith("W")){
                currentUsedStyle = doc.getStyle("warnStyle");
           else if (text.startsWith("D")){
                currentUsedStyle = doc.getStyle("debugStyle");
          try {
               text = text + "\n";
              doc.insertString(doc.getLength(), text, currentUsedStyle);
          } catch (BadLocationException e) {
               e.printStackTrace();
     public static String getClipboard() {
          // get the system clipboard
          Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
          // get the contents on the clipboard in a transferable object
          Transferable clipboardContents = systemClipboard.getContents(null);
          // check if clipboard is empty
          if (clipboardContents == null) {
               return ("Clipboard is empty!!!");
          } else
               try {
                    // see if DataFlavor of DataFlavor.stringFlavor is supported
                    if (clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                         // return text content
                         String returnText =(String) clipboardContents.getTransferData(DataFlavor.stringFlavor);
                         return returnText;
               } catch (UnsupportedFlavorException ufe) {
                    ufe.printStackTrace();
               } catch (IOException ioe) {
                    ioe.printStackTrace();
          return null;
     private void initStyles() {
          //SET Style
          String font = "Courier New";
          int size = 14;
          Style errorStyle = doc.addStyle("errorStyle", null);
        StyleConstants.setFontFamily(errorStyle, font);
        StyleConstants.setFontSize(errorStyle, size);
        StyleConstants.setForeground(errorStyle, Color.RED);
        StyleConstants.setBackground(errorStyle, Color.WHITE);
        Style infoStyle = doc.addStyle("infoStyle", null);
        StyleConstants.setFontFamily(infoStyle, font);
        StyleConstants.setFontSize(infoStyle, size);
        StyleConstants.setForeground(infoStyle, Color.BLUE);
        StyleConstants.setBackground(infoStyle, Color.WHITE);
        Style warnStyle = doc.addStyle("warnStyle", null);
        StyleConstants.setFontFamily(warnStyle, font);
        StyleConstants.setFontSize(warnStyle, size);
        StyleConstants.setForeground(warnStyle, Color.DARK_GRAY);
        StyleConstants.setBackground(warnStyle, Color.WHITE);
        Style debugStyle = doc.addStyle("debugStyle", null);
        StyleConstants.setFontFamily(debugStyle, font);
        StyleConstants.setFontSize(debugStyle, size);
        StyleConstants.setForeground(debugStyle, Color.GRAY);
        StyleConstants.setBackground(debugStyle, Color.WHITE);
     public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                    SSCCELogField inst = new SSCCELogField(new File("F:\\logfile.log"));
                    inst.setLocationRelativeTo(null);
                    inst.setVisible(true);
}

Similar Messages

  • I am trying to edit my website using the copy and paste function and am getting an error message that says my browser will not allow this function.. what do I do? thanks!

    I am trying to do a simple copy and paste function while editing my website and I am getting an error message that says that my browser will not allow this function.. how can I override this?
    thanks
    Doug Snyder

    You need to set Copy'n'Paste security policies for that domain in Firefox.
    AllowClipboard Helper will help you do that. <br />
    https://addons.mozilla.org/en-US/firefox/addon/allowclipboard-helper/

  • New ABAP editor: problems with the paste function

    Hi all,
    for a few weeks, I am working with the new frontend editor now. It is great - however, there is a thing I don't understand using the paste function. Let's make an example:
    i put the following lines into clipboard by highlighting the entire lines and pressing CTRL+C
    select * from ztmk
             where kunnr in s_kunnr.
    after pasting (CTRL+C) it shows up like this:
    select * from ztmk
    where kunnr in s_kunnr.
    Is it a bug or a feature?
    Jörg Krause, GEZE GmbH, Germany

    Hi Rich,
    sorry, I actually upgraded my GUI to 23, but the problem is not solved. Look at this code:
      SELECT * FROM ztmk INTO TABLE lt_ztmk
             WHERE messe IN s_messe AND
                   kunnr_berater IN s_kunnr AND
                   status IN s_statu AND
                   ersda IN s_ersda AND
                   NOT namecpl = space AND
                   country IN s_count AND
                   kontaktid IN s_kntid AND
                   lvorm in s_lvorm.
      SELECT * FROM ztmk INTO TABLE lt_ztmk
      WHERE messe IN s_messe AND
      kunnr_berater IN s_kunnr AND
      status IN s_statu AND
      ersda IN s_ersda AND
      NOT namecpl = space AND
      COUNTRY IN s_count AND
      kontaktid IN s_kntid AND
      lvorm IN s_lvorm.
    the lower select-statement has been pasted from the upper one
    Thanks for answering!
    Jörg

  • Cannot use copy and paste function in PSE10?

    Today i went to copy and then paste an image onto another one,i could not use the copy or paste function(in pse 10). Only the paste function was usable  and when i hit it it pasted the picture of an eagle onto the image. This eagle i had copied from a particular website(unrelated to photoshop) amd pasted it to a folder in windows live photo gallery. I tried deleting this eagle from the windows gallery,but still i could paste the eagle onto the other picture.  And it created another layer with the picture of the eagle.This is very strange. How can this be corrected. Also as usual my lassoo tool is malfunctioning in pse 10?  Maybe uninstalling  and re-installing pse 10 would solve the problem  and if so can you give me instructions on how to do this safely or refer me to a link.  Would appreciate a quick reply. Thanks!!

    Thanks Barbara for your help  and your nice explanation as to how the copy and paste function works. Never had a problem before but now it functions normally!
    Irwin Lakin
    <mailto:[email protected]> [email protected]
    508-866-4250

  • Paste function no longer works for me.

    I have only noticed in the past couple of days that the 'paste' function when I right-click no longer works, it's greyed out. I can copy no problem but when it comes to pasting it, I can't get it to work. I have tried 'ctrl+v', that doesn't work for me, I have reset Firefox and still it's not there. I have seen previous posts about this but it seems to be from long ago (sorry if this has been asked recently but I never saw the post if it has).
    The paste function works for everything else, like Chrome and Word etc. it's only Firefox I have problem with.
    Does anyone else know what I can do?

    It's unusual that the standard Windows cut, copy and paste do not work.
    Have you noticed any difference between dumb form controls (like on this site) versus rich text editors (like on webmail sites)?
    In case one of your extensions is involved, could you test in Firefox's Safe Mode? That's a standard diagnostic tool to deactivate extensions and some advanced features of Firefox. More info: [[Troubleshoot Firefox issues using Safe Mode]].
    You can restart Firefox in Safe Mode using
    Help > Restart with Add-ons Disabled ''(Flash and other plugins still run)''
    In the dialog, click "Start in Safe Mode" (''not'' Reset)
    Any difference?

  • Using javascript to override  pressing function keys (f1,f3,f6,f7,....)

    hi all i wonder if there is a way to to override pressing function keys(f1,f3,f6,f7,....) in adf page for all of the page not just for an input text .
    in another way can i make a client listener for a <f:view> or <af:document> so the javascript function will be called when i preses any button in keyboard without focusing in inputtext or a command button
    Edited by: user554540 on 22/06/2010 03:03 ص

    Hi,
    the problem is that keyboard events are only raised from input components. client listeners can only added to ADF Faces components and af:document does not accept keyboard entries. However, I tried
    af:document > panelStrechtLayout (center facet) > panelGroupLayout (scroll) and assigned a clientListener. At runtime has the desired effect
    Frank
    Edited by: Frank Nimphius on Jun 22, 2010 6:27 PM

  • Copy and paste function not stable

    i have problems using copy and paste function. the copied stuff SOMETIMES would not paste after i either Ctrl + C or right click to copy the stuff. does anyone here experience the same problem or have the solution? thanks in advance.

    you might want to check whether you can copy the contents or not.. try to use the copy and paste function by right click the mouse to test..

  • Copy and paste function stopped working?

    The copy and paste function stopped working on my laptop (ibook g4, OSX 10.3.9). It does not work in any applications, tried MS word, safari, preview.
    How do I recover this function?
    Thanks.

    Hi Magno2002
    Your Finder may have lost contact with the clipboard, so try this.
    In ~/library/preferences locate these two files and trash them
    com.apple.finder.plist
    com.apple.sidebarlists.plist
    To re-establish them, log out and log back in again.
    You will need to reset your finder and sidebar as you had them before.
    regards roam

  • Disable copy and paste function in jtextfield

    I wish to disable the copy and paste function (CTRL+C and CTRL+V) of the data in the jtextfield... anybody can help?

    I think you can add a KeyListener to listen to those keys and when you get such an input just do nothing. Not tested though.

  • Copy and Paste function no longer works in version 3.6

    Ever since I upgraded to version 3.6 (finally after waiting for all of the crash bugs to be fixed after having to downgrade last fall) now the simple copy & paste function won't work when copying from one webpage to put link or paragraph of text into the body of an e-mail, through any of my web based mail services (Yahoo, Onebox, etc.)
    So, when doing all of this version updating how could Mozilla allow such a simple function used in every program since the invention of the computer... to be disabled?

    Im having this problem, and although it took time to tell because it is intermittent, it still happens when all add-ons & theme are manually disabled.
    I would have to test safe mode a little bit longer to be sure it didnt happen there.
    It also affects other programs (like my metapad- notepad replacement) as long as firefox is open, but does not when the browser is closed, as far as I can tell.
    (version 3.6.12)
    It may or may not be related, but there's also a flicker
    (black screen in page window for an eyeblink) when firefox
    opens or returns from maximized state, and when it opens a menu.
    Both bugs only recently started.

  • Really slow copy and paste function

    I'm having alot of trouble with the copy and paste function in Numbers on my Mac.  I work from home and have to copy and paste alot of spreadsheets and numbers but when I copy a large set of numbers from one spreadsheet to another it takes FOREVER to paste and it constantly freezes my computer.  Does anyone else have this problem and have you found a solution?

    Have you upgraded SQL server from 2000?  You may also check this thread:
    SQL 2005 with database compatibiltiy level set to 80

  • My Copy and Paste Function is not working

    I recently upgraded to Firefox 16.02 and everything worked fine for a while but last night out of the blue my copy and paste function stopped working. As far as I know I have not done anything different. My copy paste function still works in Chrome and IE.
    I have uninstalled/reinstalled Firefox and that did not solve the problem. As we speak I am running an AV scan and I also cleared the web cache and what not in Firefox. Ran it in safe mode... nothing seems to work!
    Interestingly though, when I went to Copy and Paste the "trouble shooting info" from the clipboard to here on this page it worked fine.

    For problems copying the results of Google translations, see these threads:
    * [https://support.mozilla.org/en-US/questions/940979 i can't copy translate from google traslator, ctrl-c ctrl-v don't work]
    * [https://support.mozilla.org/en-US/questions/940871 Can't copy translated text from Google Translate web page?]

  • Copy/paste function not working in Firefox 22. Always good in previous versions

    Copy/paste function not working in Firefox 22. Always good in previous versions editions

    Well I've uninstalled FF22, thanks for the suggestion.
    Chrome works perfectly with ZA and C&P.

  • My copy and paste functions are not working properly since iOS 8 update

    Ever since I updated to iOS 8 on my iPhone 4s the copy and paste function isn't working. It'll work the first time but any time I copy something else it pastes the first thing I copy. The only fix I've found is rebooting the phone but that is a pain to do every time I want to copy something and should be very unnecessary. I updated to 8.0.2 but that didn't fix it.

    Have you tried rebooting?

  • How to add cut,copy,paste functionality ?

    Hello everybody,
    I have to add cut,copy,paste functionality to my RichTextEditor for which I have already added
    buttons to editors toolbar,Now I want to add these three functionalities to my editor.
    Can anyone tell me or provide me some code sample for it ?
    Thanks

    Well Thank you for your response,What I am looking for is ,the user should be able to copy,cut and paste the selected text anywhere,
    (e.g.If user selects the text and clicks the COPY button on the RichTextEditor , he can copy that selected text into notepad or any other editor or any other place in the system)
    I have written the code for copy and its working properly
    System.setClipboard(richTextEditor.selection.text);
    I was wondering how to write the code for cut and paste.
    many thanks again

Maybe you are looking for

  • Parallel Processing in Oracle 10g

    Dear Oracle Experts, I would like to use the Parallel Processing feature on My production database running on Unix Box. No: of CPU in each node is 8 and its RAC database Before going for this option i would like to certain things regarding Parallel P

  • 2 or more cascading selectonechoice in Oracle ADF 11g Rel 2

    Hi, I want to have 2 (later more) cascading (depending) selectonechoice component. The first combobox (partners) will be filled with the result of a SQL view object (PartnerLovView) execution, this query has NO (bind) parameter and only has one prima

  • Can the Apple TV support more multiple remote apps?

    We just got a new Apple TV and would like to use two different iPhones using different ITunes accounts to control the device through the remote app.  Is is possible?

  • What to do with one 25i and one 25p file in same Timeline???

    Hey! I was bothering you with questions of how do I put 30p and 25i footage together, but things have changed. [i]HUNTREX and ANN BENS, thanx a bunch for your help)[/i] Canon released new Firmware for its 5D MkII camera, so I can shoot 25p and 24p...

  • CTXCAT search index Issue - trying to index multi columns

    i can index more than one column in the same table using MULTI_COLUMN_DATASTORE but when the index type is CONTEXT but i need to index more than one column ,when the index type is CTXCAT putting into considerations that i found that MULTI_COLUMN_DATA