Is it possible to write into the clipboard with HTML flavor ?

Hello,
Last week I already posted this question in the awt abstract  forum, but since nobody answered, I guess it was not the right place. Thanks for any suggestions or comments
I'm having a problem with the code below. Here is the scenario I wish to perform :
1. Display a web page with my favorite navigator
2. Select a few lines from this page and copy this area to the clipboard (CTL-C)
3. When doing CTL-V (move) in a JTextPane, I intercept this action, to manipulate the data ( method clearClipBoard)
4. This method does the following
a) read the clipboard get the result into a String (this works fine).
b) loop on this String to find occurrences of a particular word, and suppress the line containing this word.
c) re-write the new String into the clipboard.
This works pretty well when having the JTextPane with a RTF EditorKit. But it does not work with an HTMLEditorKit.
I guess there is something wrong with DataFlavors, but I don't know how to set the DataFlavor to a text/html mime when writting the data back.
When testing the authorized DataFlavor, none "text/html" mimes appear and of course I get an exception for the one I'm trying to use.
Therefore the data is just a continuous string displayed in the JTextPane with all html tags visible instead of having formated data
I did a lot of tries, but I failed. below is a piece of code I'm using.
Thanks a lot, for any suggestions<
Gégé
private void clearClipboard()
  String      str     =      null,
               result =      null;
DataFlavor           flv          =      null;
StringWriter             sw           =     null;
try
     flv = new DataFlavor("text/html;charset=unicode;class=java.lang.String" );
     result= readClipBoard(flv );    //  This works fine
     sw = new StringWriter();       
     BufferedReader buffreader = new BufferedReader( new StringReader(result));
     BufferedWriter buffwriter = new BufferedWriter(sw);
     while ((str = buffreader.readLine()) != null)
        if (str.length() <= 0) continue;
        str=str.trim();                            
        String wk = "";
        while (true)
           //  re-create the string...... into str.  then break;     
        buffwriter.write(str);
  buffwriter.close();
catch(Exception e) { e.printStackTrace();}
writeClipBoard(sw.toString(),flv);      // on écrit dans le clipboad
public static void  writeClipBoard(String s, DataFlavor flavor)
   try
      java.awt.datatransfer.Clipboard  clipboard = 
                        java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
      java.awt.datatransfer.Transferable transferable =
                   new java.awt.datatransfer.StringSelection(s);
      //  DataFlavor[] flv = transferable.getTransferDataFlavors();
      //   for (int i=0; i<flv.length; i++) System.out.println(flv.toString());
// The Dataflavor I'm using is not in the flv table and
// of course it raises an exception.
transferable.getTransferData(flavor);
clipboard.setContents(transferable, null);
catch (Exception ex) {System.out.println( ex.toString());}

Stanislav, I'm back with my complete test code.
In fact, rather than modifying anything, I wrote this sample to just read the clipboard and write it back (without any change).
1) First, you may test it without entering my code (that's the default) . Just start my sample.
2) Go to your favorite Web browser. Select a few lines, and (copy to clipboard)
3) Go to the application and "Paste" I works fine
4) Then if you go to the initialize method, and please, uncomment the instruction where I'm defining the KeyListener (at the end of the method).
5) Restart the application and redo the same. It does not work.
In the list of flavors that I have printed (I got only 2 valid flavors, I tested both, without any success. I even try to set the flavor to null (got an exception of course).
Thanks again to your help. I hope my English is not too bad, to make you understand.
Gege
package test;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.StringReader;
import java.io.StringWriter;
import javax.swing.text.html.HTMLEditorKit;
public class TestTextPane extends javax.swing.JFrame implements KeyListener, ActionListener
     private static final long serialVersionUID = 1L;
     private final javax.swing.JTextPane myTextPane =   new javax.swing.JTextPane();
     private final javax.swing.JPanel frameContentPane = new javax.swing.JPanel();
     private final javax.swing.JButton  Terminer = new javax.swing.JButton();
     public TestTextPane() {
          super();
          initialize();
      public void actionPerformed(java.awt.event.ActionEvent e)
           System.exit(0);
    public static void clearClipboard()
         String      str          =      null,
                   result      =      null;
                    DataFlavor           flv          =      null;
                   StringWriter      sw           =     null;
         try
              flv = new DataFlavor("text/html;charset=unicode;class=java.lang.String" );
              result=  readClipBoard(flv );    // get clipboard content
              sw = new StringWriter();       
              BufferedReader buffreader = new BufferedReader( new StringReader(result));
              BufferedWriter buffwriter = new BufferedWriter(sw);
              while ((str = buffreader.readLine()) != null)
                   //  I re-write without processing     to see what's happening     
                   System.out.println(str);
                   buffwriter.write(str);
              buffwriter.close();
         catch(Exception e) { e.printStackTrace();}
         writeClipBoard(sw.toString(),flv); // on écrit dans le clipboad
     public static void writeClipBoard(String s, DataFlavor flavor)
     try
            // flavor= new DataFlavor("application/x-java-serialized-object;class=java.lang.String");
          java.awt.datatransfer.Clipboard  clipboard = 
                    java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
          java.awt.datatransfer.Transferable transferable =
                    new java.awt.datatransfer.StringSelection(s);
          DataFlavor[] flv = transferable.getTransferDataFlavors();
          for (int i=0; i<flv.length; i++) System.out.println(flv.toString());
          transferable.getTransferData(flavor);
          clipboard.setContents(transferable, null);
     catch (Exception ex) {System.out.println( ex.toString());}
public void FrameCenter( )
     java.awt.Dimension fenetre = this.getSize();
     int sizex = fenetre.width, sizey = fenetre.height;
     java.awt.Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
          setBounds((screen.width - sizex) / 2,(screen.height - sizey) / 2, sizex, sizey);
private void initialize()
          setName("TestTextPane");
          setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
          setSize(557, 451);
          myTextPane.setName("myTextPane");
          myTextPane.setBounds(23, 18, 513, 307);
          Terminer.setName("Terminer");
          Terminer.setText("Finish");
          Terminer.setBounds(230, 370, 101, 25);
          frameContentPane.setName("myFrame");
          frameContentPane.setLayout(null);
          frameContentPane.add(myTextPane, myTextPane.getName());
          frameContentPane.add(Terminer, Terminer.getName());
          setContentPane( frameContentPane);
          Terminer.addActionListener(this);
          FrameCenter( );
          HTMLEditorKit htmlKit = new HTMLEditorKit();
          myTextPane.setEditorKit(htmlKit);
          myTextPane.setEditable(true);
          myTextPane.setText("Hello. Clipbard contains should be appended below" );
          // myTextPane.addKeyListener(this); // to be uncommented to trap clipboard events
this.setVisible(true);
     public void keyPressed(java.awt.event.KeyEvent ke)
          if (ke.getKeyCode() == java.awt.event.KeyEvent.VK_V)
               int i = ke.getModifiers();
               if ( (i & java.awt.event.InputEvent.CTRL_MASK) == java.awt.event.InputEvent.CTRL_MASK)      {clearClipboard();}
     public void keyReleased(java.awt.event.KeyEvent ke){}
     public void keyTyped(java.awt.event.KeyEvent ke){}
     public static String readClipBoard(DataFlavor type)
          java.awt.datatransfer.Transferable contents = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
          if (! contents.isDataFlavorSupported(type) ) return null;
          if ((contents != null) && contents.isDataFlavorSupported(type))
               try
                    return (String) contents.getTransferData(type);
               catch (java.awt.datatransfer.UnsupportedFlavorException ex){return  ex.toString();     }
               catch (java.io.IOException ex)      {return  ex.toString();     }
          else return "";
     public static void main(java.lang.String[] args) {
          new TestTextPane();

Similar Messages

  • Possible to write into the Calendar direct on the Ipod Touch?

    Hello I have an old version of teh Ipod Touch I cant write directly into the Calendar from my Ipod Touch am I suppose to be able to do that? It would be very convinient if I could and a very stupid design if its not possible.
    Please let me know if its my ipod touch that is faulty or maybe i am doing something wrong.
    Cheers

    Yeah, v1.1.2 can do that, as well as v1.1.3

  • Is it possible to login into the Java instance without password's input, using only my Windows workstation authorization?

    Dear Sirs,
    I try to do an authorization to my NW 7.3 Java instance through my Windows domain authorization.
    I done:
    1) Create connection to LDAP-server and tested it.
    2) Add windows domain certificate to TrustedCAs
    3) Configure SPnego
    Now, I can to login in my NW7.3 Java instance with my windows password, but however I must to input password when I open NW7.3 Java homepage.
    Is it possible to login into the Java instance without password's input, using my windows workstation login/password?
    What I have to do for that?
    I use Windows XP on my workstation and IE 8.0.6 & Chrome 38.0.2125.
    Best regards,
    Alexey Lugovskoy

    Please check
    Using Kerberos Authentication on SAP NetWeaver AS Java - User Authentication and Single Sign-On - SAP Library (NW7.3)
    Using Kerberos Authentication for Single Sign-On - User Authentication and Single Sign-On - SAP Library (NW7.0)

  • Is it possible to get into the IC using the SALESPRO business role?

    Is it possible to get into the Interaction centre when using the SALESPRO business role?.
    If so, how is this done.
    I know using specific IC* business roles, like IC_AGENT, you are thrown straight into the IC, but I can't see how you can get into it via the SALESPRO business role, which I assume you should be able to do.
    Jason

    Please check
    Using Kerberos Authentication on SAP NetWeaver AS Java - User Authentication and Single Sign-On - SAP Library (NW7.3)
    Using Kerberos Authentication for Single Sign-On - User Authentication and Single Sign-On - SAP Library (NW7.0)

  • I am making a photobook through iPhoto for my Guest Book for guests to sign at my wedding.  I was wondering if it's possible to write on the paper used in the book with ink?  With some photopaper this isn't possible.

    I am making a photobook through iPhoto for my Guest Book for guests to sign at my wedding.  I was wondering if it's possible to write on the paper used in the book with ink?  With some photopaper this isn't possible

    It certainly woujld depend on the pen used - the paper specs are here
    LN

  • How do you use Airport extreme at hotels etc, when traveling? I can plug my computer into the Airport with my ethernet but how does Airport pickup signal?

    How do you use Airport extreme at hotels etc, when traveling? I can plug my computer into the Airport with my ethernet but how does Airport pickup signal? What equipment & devices do I need to travel with to make this possible?

    You may mean the AirPort Express.....not AirPort Extreme.....as the Express is a popular travel router.
    The whole idea behind using this device is that the hotel must provide an Ethernet jack in the hotel room. Then you connect the AirPort Express to the Ethernet jack with an Ethernet cable and configure it to provide your own wireless network in the room. You still have to agree to terms, pay the fees, etc.
    The problem with this approach is that it is getting very difficult to find hotels in North America that prrovide an Ethernet jack....most have moved to wireless networks and the others are not far behind.
    So, if the hotel is already providing a wireless signal, the AirPort Express is of no use in that situation.
    If you normally stay at the same hotels, and know that they provide Ethernet ports, an AirPort Express might make sense in terms of convenience.

  • IPad Photo App with the ability to write on the image with stylus?

    Is there a photo app for 4th generation iPad that allows me to take a picture and then write on the image with a stylus? I'm in the sign business and that would be ideal for on site assessments (write dimensions and notes)

    Creating notes using stylus on the application is not possible on Adobe Reader Touch.
    But, you can add a sticky note on the document. To add a sticky note, launch app bar, select Comments button and then select Sticky Note option and click on the point where you want you add the sticky note.

  • When I plug my macbook retina into the tv with the hdmi port, all I see is a black screen with a white mouse How do I fix this?

    Recently I bought my macbook with retina display and when I plugged it into the television with using the HDMi cord it worked perfectly fine with the image on the television. But now I when I plug it in to the television it comes up with a black screen and a white mouse. Before this incident I was mucking up with the setting of the mac book and I dont know what I did wrong. Can someone tell me how to fix this ?

    Howdy Lumagofe,
    I was thinking that you may need to reset the iPod Nano, outlined here How to reset iPod http://support.apple.com/kb/ht1320.
    Press and hold the Sleep/Wake button and Volume Down button simultaneously for at least eight seconds or until the Apple logo appears. You may need to repeat this step.
    If the above step did not work, try connecting iPod to a power adapter and plug the power adapter into an electrical outlet, or connect iPod to your computer. Make sure the computer is turned on and isn't set to go to sleep. Try resetting the iPod while it is connected to power.
    If that doesn't quite get the iPod going again, then it will need to be restored. Use this article to help with that Restoring iPod to factory settings http://support.apple.com/kb/ht1339.
    All the very best,
    Sterling

  • Yesterday I was pushed into the sea with my Iphone 5s in my back pocket, as I got out I took my phone out of my pocket and it turned itself off. I have tried many ways but still is unsuccessful and wont turn on, what shall I do?

    I was pushed into the sea with my iphone 5s in my back pocket, I got out and pulled my phone out of my back pocket and it turnt itself off. I went straight home and tried using a hair dryer but was unsucessful, put it in rice which also failed and i left it screen up on the radiator over night but still wouldnt turn on. It has charge on it although i still tried plugging it in to give it power and still was unable to power up. Can anyone give any suggestions on how i can fix it as for its not even 2 months old.

    Watamu wrote:
    Try putting it in rice for a few hours. It does sound daft but it did work for mine
    If you're going to put a phone in rice (not daft at all, the rice acts as a dessicant), it needs to be for a few days, not a few hours. The OP stated in their post that the did put it in rice. In addition to the liquid damage, salt water is also much more corrosive to electronics than plain water. The rice won't help with the salt.

  • I have an iMac (the desktop) I've stupidly left the cable you plug the monitor into the wall with there. What are they called and where can I get a new one?

    I have an iMac (the desktop) I've stupidly left the cable you plug the monitor into the wall with there. What are they called and where can I get a new one?

    Presumably the mains cable, which you can get from an Apple Store.

  • How can I get Lightroom 5.7 into the "Open With" menu on an external Harddrive?

    Using Lightroom 5.7, I imported photo images as a .dng.  I edited the images in lightroom.  Then I dragged the file from the local Disk (C) to an external Hard drive (left panel of Library).  When I look in the External harddrive, I would like to see thumbnails of the images. I think I would like to open the images in Lightroom but I cannot get Lightroom into the "Open With" dropdown menu.  Photo shop, Adobe Reader and a bunch of Windows photo editing stuff is in there but I cant seem to get Lightroom into this menu. 

    The "open with" command is used to open images that are already in Lightroom in another external editor such as Photoshop or Photoshop Elements or some other photo editing program. And you are opening an image that you are already seeing in Lightroom. So Lightroom won't be on the list and cannot be on the list because you are already looking at the images in Lightroom.
    Concerning the images that you moved to the external hard drive, if you actually moved them using Lightroom then you should be able to see that external hard drive in the library module library tree and be able to choose that folder. Then the images will be in Lightroom and you can continue working on them. There doesn't need to be any "open with" to do that. If you moved those images some other way, please explain.

  • My iPad 4 wifi 3G keeps charging slowly takes half a hour to get to 15% when plug into the wall with the plug and the lead what come with the iPad just wondering as I think some thing is wrong with the iPad

    My iPad 4 wifi 3G keeps charging slowly takes half a hour to get to 15% when plug into the wall with the plug and the lead what come with the iPad just wondering as I think some thing is wrong with the iPad

    Using the battery level meter in this manner is comparable to using your car's fuel gauge to calculate miles per gallon. The only thing that matters is the total amount of operating time from full charge to auto-shutdown.
    Use the wall-mount charger that came with the iPad and charge overnight.  Do NOT use an iPod/iPhone charger.  Do NOT use a computer's USB port.  Then, operate it normally until auto shut-down (ignore any low level alerts that may appear).  An irony is that doing that test to determine the total operating time is also the procedure necessary to calibrate the battery level meter.
    I'm not claiming that you do not have a problem.  I am stating, however, that we don't yet know.  If the above test does, in fact, indicate a problem, read this.
    Also, according to Apple:
    Use Your iPad Regularly
    For proper reporting of the battery’s state of charge, be sure to go through at least one charge cycle per month (charging the battery to 100% and then completely running it down).
    Elsewhere, Apple elaborates and explains that two half-discharges (or four quarter-discharges, etc.) equals one full discharge.

  • I wish an Ipad. Is it possible to work on the screen with 2 application toghether (2 Pages file in the same screen view) ?

    I wish an Ipad. Is it possible to work on the screen with 2 files toghether (ex: 2 Pages files in the same screen view) ?
    I need to make some transalations and so it's important for me to have the english text and the italian version that I am making, on the same screen view.
    Thank You.

    No. You cannot work on two Pages files in the same window. It is not possible to have multiple windows on the screen at the same time in iOS.

  • TS3219 i am setting up an ipod touch for my daughter, created her an account, had it verified,  but when I get to the section that reads 'connect to itunes' I plug it into the computer with her log in and nothing happens.  HELP.  I have been at this for 4

    i am setting up an ipod touch for my daughter, created her an account, had it verified,  but when I get to the section that reads 'connect to itunes' I plug it into the computer with her log in and nothing happens.  HELP.  I have been at this for 4 hours

    Hello GJS6,
    Thank you for the details of the issue you are experiencing when trying to connect your daughter's iPod touch to the computer.  I recommend the following article:
    iOS: Device not recognized in iTunes for Mac OS X
    http://support.apple.com/kb/TS1591
    Thank you for using Apple Support Communities.
    Best,
    Sheila M.

  • I have a new MacBook Air 11'inch  and I presume that it should come with the iLife programs. However, when I signed into the appstore with my account I have used with all of my Mac products, it now said I had to purchase each program, like iPhoto seperate

    I have a new MacBook Air 11'inch  and I presume that it should come with the iLife programs. However, when I signed into the appstore with my account I have used with all of my Mac products, it now said I had to purchase each program, like iPhoto, iMovie, ect. seperate. I have already logged in and out of my account and called the "free" part of tech support. How can I reslove this issue?

    Welcome to Apple Support Communities
    If you really have a new MacBook, iPhoto, iMovie and GarageBand come with the Mac for free.
    As you see their price at the Mac App Store, it's clear that there's a problem with your MacBook. Simply return it if you purchased it less than 14 days ago and you will receive a new MacBook

Maybe you are looking for