Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30511 > unrolled thread

JEditorPane question

Started byMartin Gregorie <martin@address-in-sig.invalid>
First post2014-12-08 13:47 +0000
Last post2014-12-10 00:46 -0500
Articles 14 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-08 13:47 +0000
    Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 17:48 -0500
      Re: JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-08 23:30 +0000
        Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 19:14 -0500
          Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 19:49 -0500
            Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 20:48 -0500
              Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 21:21 -0500
                Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 21:37 -0500
        Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 23:11 -0500
          Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 23:28 -0500
            Re: JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-11 18:31 +0000
          Re: JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-09 09:41 +0000
          Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-09 22:41 -0500
            Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-10 00:46 -0500

#30511 — JEditorPane question

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2014-12-08 13:47 +0000
SubjectJEditorPane question
Message-ID<m64a5v$d3j$1@dont-email.me>
Just a quicky: is there any way to force the newline symbol that 
JEditorPane outputs the the display to take a value that isn't native for 
the OS the application is running on?

Why I'm asking: I need to pass data from a Linux system to Windows-based 
website by copy & paste between a JEditorPane in my Java application and 
an input area on a web page. The web application is expecting CRLF 
newlines. My application uses String.replace() to convert newlines to 
CRLF  before the string is passed to the JEditorPane via its setText() 
method. However, when I copy the displayed text from the screen it always 
has the Linux LF linebreak in it rather than the CRLF.

According to the class description that the DefaultEditorKit sets itself 
up to use the first linebreak when data is loaded into it. There are 
hints in the description that the EndOfLineStringProperty can be changed, 
but as its declared final, this is never going to work.

So, my question: does anybody know how to change the linebreaks used by 
the editor after its been started and, particularly, if its possible to 
change the linebreak symbol used in the JEditorPane's on screen display 
area?


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |
.

[toc] | [next] | [standalone]


#30514

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 17:48 -0500
Message-ID<m659sr$5vl$1@dont-email.me>
In reply to#30511
On 12/08/2014 08:47 AM, Martin Gregorie wrote:
> Just a quicky: is there any way to force the newline symbol that
> JEditorPane outputs the the display to take a value that isn't native for
> the OS the application is running on?
>
> Why I'm asking: I need to pass data from a Linux system to Windows-based
> website by copy & paste between a JEditorPane in my Java application and
> an input area on a web page. The web application is expecting CRLF
> newlines. My application uses String.replace() to convert newlines to
> CRLF  before the string is passed to the JEditorPane via its setText()
> method. However, when I copy the displayed text from the screen it always
> has the Linux LF linebreak in it rather than the CRLF.

Just a WAG. Have you looked at TransferHandler et al.
>
> According to the class description that the DefaultEditorKit sets itself
> up to use the first linebreak when data is loaded into it. There are
> hints in the description that the EndOfLineStringProperty can be changed,
> but as its declared final, this is never going to work.

There seems to be a hint that the EndOfLineStringProperty may be changed
using the get and put Property methods.

There also seems to be a statement that despite in/out test that
newlines are always stored internally as \n.

>
> So, my question: does anybody know how to change the linebreaks used by
> the editor after its been started and, particularly, if its possible to
> change the linebreak symbol used in the JEditorPane's on screen display
> area?
>
>
Sorry I can't be more helpful.

[toc] | [prev] | [next] | [standalone]


#30517

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2014-12-08 23:30 +0000
Message-ID<m65car$d52$1@dont-email.me>
In reply to#30514
On Mon, 08 Dec 2014 17:48:13 -0500, Jeff Higgins wrote:

> On 12/08/2014 08:47 AM, Martin Gregorie wrote:
>> Just a quicky: is there any way to force the newline symbol that
>> JEditorPane outputs the the display to take a value that isn't native
>> for the OS the application is running on?
>>
>> Why I'm asking: I need to pass data from a Linux system to
>> Windows-based website by copy & paste between a JEditorPane in my Java
>> application and an input area on a web page. The web application is
>> expecting CRLF newlines. My application uses String.replace() to
>> convert newlines to CRLF  before the string is passed to the
>> JEditorPane via its setText() method. However, when I copy the
>> displayed text from the screen it always has the Linux LF linebreak in
>> it rather than the CRLF.
> 
> Just a WAG. Have you looked at TransferHandler et al.
>
WAG? - he says, displaying ignorance.

>>
>> According to the class description that the DefaultEditorKit sets
>> itself up to use the first linebreak when data is loaded into it. There
>> are hints in the description that the EndOfLineStringProperty can be
>> changed,
>> but as its declared final, this is never going to work.
> 
> There seems to be a hint that the EndOfLineStringProperty may be changed
> using the get and put Property methods.
> 
Yes, I saw that, but that documentation trail disappears off into a 
morass of unspecified properties and unstated values that can be given to 
them, I thought it was better to ask here in the hope that somebody had a 
similar requirement.

> There also seems to be a statement that despite in/out test that
> newlines are always stored internally as \n.
>
Yes, that is straight-forward and clearly stated. 
 
> 
>> So, my question: does anybody know how to change the linebreaks used by
>> the editor after its been started and, particularly, if its possible to
>> change the linebreak symbol used in the JEditorPane's on screen display
>> area?
>>
>>
> Sorry I can't be more helpful.
>
Thanks anyway.

It looks as though we may have to change the website's parser to accept LF 
as well as CRLF as linebreaks from this input path.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#30518

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 19:14 -0500
Message-ID<m65eus$nrp$1@dont-email.me>
In reply to#30517
On 12/08/2014 06:30 PM, Martin Gregorie wrote:
> On Mon, 08 Dec 2014 17:48:13 -0500, Jeff Higgins wrote:
>
>> On 12/08/2014 08:47 AM, Martin Gregorie wrote:
>>> Just a quicky: is there any way to force the newline symbol that
>>> JEditorPane outputs the the display to take a value that isn't native
>>> for the OS the application is running on?
>>>
>>> Why I'm asking: I need to pass data from a Linux system to
>>> Windows-based website by copy & paste between a JEditorPane in my Java
>>> application and an input area on a web page. The web application is
>>> expecting CRLF newlines. My application uses String.replace() to
>>> convert newlines to CRLF  before the string is passed to the
>>> JEditorPane via its setText() method. However, when I copy the
>>> displayed text from the screen it always has the Linux LF linebreak in
>>> it rather than the CRLF.
>>
>> Just a WAG. Have you looked at TransferHandler et al.
>>
> WAG? - he says, displaying ignorance.
Geez. Am I overly sensitive today?
>
>>>
>>> According to the class description that the DefaultEditorKit sets
>>> itself up to use the first linebreak when data is loaded into it. There
>>> are hints in the description that the EndOfLineStringProperty can be
>>> changed,
>>> but as its declared final, this is never going to work.
>>
>> There seems to be a hint that the EndOfLineStringProperty may be changed
>> using the get and put Property methods.
>>
> Yes, I saw that, but that documentation trail disappears off into a
> morass of unspecified properties and unstated values that can be given to
> them, I thought it was better to ask here in the hope that somebody had a
> similar requirement.
>
>> There also seems to be a statement that despite in/out test that
>> newlines are always stored internally as \n.
>>
> Yes, that is straight-forward and clearly stated.
>
>>
>>> So, my question: does anybody know how to change the linebreaks used by
>>> the editor after its been started and, particularly, if its possible to
>>> change the linebreak symbol used in the JEditorPane's on screen display
>>> area?
>>>
>>>
>> Sorry I can't be more helpful.
>>
> Thanks anyway.

<http://www.coderanch.com/t/335312/GUI/java/JTextPane-lines>
has perhaps a bit of a hint.

>
> It looks as though we may have to change the website's parser to accept LF
> as well as CRLF as linebreaks from this input path.
That would sure make the Java end easier.

[toc] | [prev] | [next] | [standalone]


#30519

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 19:49 -0500
Message-ID<m65h0h$tt5$1@dont-email.me>
In reply to#30518
> <http://www.coderanch.com/t/335312/GUI/java/JTextPane-lines>
> has perhaps a bit of a hint.

another
<http://tips4java.wordpress.com/2009/02/07/text-and-new-lines/>

there may be more around jeditorpane+newlines

[toc] | [prev] | [next] | [standalone]


#30520

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 20:48 -0500
Message-ID<m65kei$7q3$1@dont-email.me>
In reply to#30519
Huh.
When I open LoremIpsum the \r\n pairs are there.
When I copy/paste from the console to a hex editor they're gone.

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.DefaultEditorKit;

public class JEditorPaneTest {
    JFrame frame = null;
    public static void main(String[] a)
        throws FileNotFoundException, UnsupportedEncodingException {
       (new JEditorPaneTest()).test();
    }
    private void test() throws FileNotFoundException, 
UnsupportedEncodingException {
       frame = new JFrame("JEditorPane Test");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(300,200);
       JEditorPane pane = new JEditorPane();
       pane.setContentType("text/plain");
       pane.setText(
          "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\n"
          +"Nunc pellentesque quam et justo fringilla, eu gravida nunc 
tempus.");
 
pane.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, 
"\r\n");
       System.out.println(pane.getText());
       new PrintWriter("LoremIpsum", 
"UTF-8").printf(pane.getText()).close();
       frame.setContentPane(pane);
       frame.setVisible(true);

    }
}

[toc] | [prev] | [next] | [standalone]


#30524

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 21:21 -0500
Message-ID<m65mcm$dee$1@dont-email.me>
In reply to#30520
On 12/08/2014 08:48 PM, Jeff Higgins wrote:
> Huh.
> When I open LoremIpsum the \r\n pairs are there.
> When I copy/paste from the console to a hex editor they're gone.
Huh again.
I think it must be a gtk clipboard thing.

[toc] | [prev] | [next] | [standalone]


#30525

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 21:37 -0500
Message-ID<m65nac$gda$1@dont-email.me>
In reply to#30524
On 12/08/2014 09:21 PM, Jeff Higgins wrote:
> On 12/08/2014 08:48 PM, Jeff Higgins wrote:
>> Huh.
>> When I open LoremIpsum the \r\n pairs are there.
>> When I copy/paste from the console to a hex editor they're gone.
> Huh again.
> I think it must be a gtk clipboard thing.
>
Nope, I can copy/paste LorumIpsum around keeping the crlf.

[toc] | [prev] | [next] | [standalone]


#30526

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 23:11 -0500
Message-ID<m65sr8$tdq$1@dont-email.me>
In reply to#30517
This one with a TransferHandler allows
me to consistently copy/paste with crlf.
Modified from Java Tutorial.

That ignorant comment kinda pissed me off.

package bluesg.sandbox;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JEditorPane;
import javax.swing.TransferHandler;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;

public class JEditorPaneTest {
   JFrame frame = null;

   public static void main(String[] a) throws FileNotFoundException,
       UnsupportedEncodingException {
     (new JEditorPaneTest()).test();
   }

   private void test() throws FileNotFoundException,
       UnsupportedEncodingException {
     TransferHandler th = new TextTransferHandler();
     frame = new JFrame("JEditorPane Test");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(300, 200);
     frame.setJMenuBar(createMenuBar());
     JEditorPane pane = new JEditorPane();
     pane.setTransferHandler(th);
     pane.setContentType("text/plain");
     pane.setText("Lorem ipsum dolor sit amet, consectetur adipiscing 
elit.\n\n"
         + "Nunc pellentesque quam et justo fringilla, eu gravida nunc 
tempus.");
     String test = pane.getText();
     System.out.print(test);
     new PrintWriter("LoremIpsum", "UTF-8").printf(test);
     frame.setContentPane(pane);
     frame.setVisible(true);

   }

   public JMenuBar createMenuBar() {
     JMenuItem menuItem = null;
     JMenuBar menuBar = new JMenuBar();
     JMenu mainMenu = new JMenu("Edit");
     mainMenu.setMnemonic(KeyEvent.VK_E);

     menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
     menuItem.setText("Cut");
     menuItem.setMnemonic(KeyEvent.VK_T);
     mainMenu.add(menuItem);

     menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
     menuItem.setText("Copy");
     menuItem.setMnemonic(KeyEvent.VK_C);
     mainMenu.add(menuItem);

     menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
     menuItem.setText("Paste");
     menuItem.setMnemonic(KeyEvent.VK_P);
     mainMenu.add(menuItem);

     menuBar.add(mainMenu);
     return menuBar;
   }

   class TextTransferHandler extends TransferHandler {
     // Start and end position in the source text.
     // We need this information when performing a MOVE
     // in order to remove the dragged text from the source.
     Position p0 = null, p1 = null;

     /**
      * Perform the actual import.
      * This method supports both drag and drop and
      * cut/copy/paste.
      */
     public boolean importData(TransferHandler.TransferSupport support) {
       // If we can't handle the import, bail now.
       if (!canImport(support)) {
         return false;
       }

       // Fetch the data -- bail if this fails
       String data;
       try {
         data = (String) support.getTransferable().getTransferData(
             DataFlavor.stringFlavor);
       } catch (UnsupportedFlavorException e) {
         return false;
       } catch (java.io.IOException e) {
         return false;
       }

       JEditorPane tc = (JEditorPane) support.getComponent();
       tc.replaceSelection(data);
       return true;
     }

     /**
      * Bundle up the data for export.
      */
     protected Transferable createTransferable(JComponent c) {
       JEditorPane source = (JEditorPane) c;
       int start = source.getSelectionStart();
       int end = source.getSelectionEnd();
       Document doc = source.getDocument();
       doc.putProperty(DefaultEditorKit.EndOfLineStringProperty, "\r\n");
       if (start == end) {
         return null;
       }
       try {
         p0 = doc.createPosition(start);
         p1 = doc.createPosition(end);
       } catch (BadLocationException e) {
         System.out
             .println("Can't create position - unable to remove text 
from source.");
       }
       String data = source.getText().substring(start, end);
       return new StringSelection(data);
     }

     /**
      * These text fields handle both copy and move actions.
      */
     public int getSourceActions(JComponent c) {
       return COPY_OR_MOVE;
     }

     /**
      * When the export is complete, remove the old text if the action was a
      * move.
      */
     protected void exportDone(JComponent c, Transferable data, int 
action) {
       if (action != MOVE) {
         return;
       }

       if ((p0 != null) && (p1 != null) && (p0.getOffset() != 
p1.getOffset())) {
         try {
           JTextComponent tc = (JTextComponent) c;
           tc.getDocument().remove(p0.getOffset(),
               p1.getOffset() - p0.getOffset());
         } catch (BadLocationException e) {
           System.out.println("Can't remove text from source.");
         }
       }
     }

     /**
      * We only support importing strings.
      */
     public boolean canImport(TransferHandler.TransferSupport support) {
       // we only import Strings
       if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
         return false;
       }
       return true;
     }
   }

}

[toc] | [prev] | [next] | [standalone]


#30527

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-08 23:28 -0500
Message-ID<m65trp$l8$1@dont-email.me>
In reply to#30526
should be:
source.getText().substring(p0.getOffset(), p1.getOffset());

[toc] | [prev] | [next] | [standalone]


#30539

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2014-12-11 18:31 +0000
Message-ID<m6cnui$3tg$1@dont-email.me>
In reply to#30527
On Mon, 08 Dec 2014 23:28:59 -0500, Jeff Higgins wrote:

> should be:
> source.getText().substring(p0.getOffset(), p1.getOffset());
>
Thanks for the tutorial in using the TransferHandler class and for the 
correction shown above, but cut and paste still doesn't seem to be 
getting CRLF: without that correction cut and paste seems to be 
duplicating LF, i.e.  the double newline in the middle of your examole 
test becomes LFLFLFLF and with the correction I'm seeing LFLF. In both 
cases the text, when grabbed with getText() and sent to System.out or 
written to a file via the PrintWriter, shows CRLFCRLF.

So, it looks very much as though copying and pasting text with CRLF isn't 
going to be at all easy. I tried adding String.replace() into the last 
line of CreateTransferrable():

	return new StringSelection(data.replace("\n", "\r\n"));

but that merely doubled the number of "\n" symbols output, so I think the 
way to go will be to modify the website to accept both Linux and Windows 
linebreaks.

Thanks for your help: I've learnt quite a lot about this part of Swing.

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#30530

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2014-12-09 09:41 +0000
Message-ID<m66g49$n9m$1@dont-email.me>
In reply to#30526
On Mon, 08 Dec 2014 23:11:38 -0500, Jeff Higgins wrote:

> This one with a TransferHandler allows me to consistently copy/paste
> with crlf.
> Modified from Java Tutorial.
> 
> That ignorant comment kinda pissed me off.
>
That was me accusing *myself* of not recognising WAG as a technical 
acronym. I only know it as shorthand for Wives And Girlfriends.
 
I'm expecting a cab Real Soon Now, so will be back later with JEditorPane-
related responses. 

Meanwhile, thanks for your help so far.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#30535

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-09 22:41 -0500
Message-ID<m68fdt$smd$1@dont-email.me>
In reply to#30526
Huh.
Contents disappear from the system (OS)
clipboard when I exit the application. :(

[toc] | [prev] | [next] | [standalone]


#30536

FromJeff Higgins <jeff@invalid.invalid>
Date2014-12-10 00:46 -0500
Message-ID<m68mof$f58$1@dont-email.me>
In reply to#30535
On 12/09/2014 10:41 PM, Jeff Higgins wrote:
> Huh.
> Contents disappear from the system (OS)
> clipboard when I exit the application. :(
>
Damn.
SWT gets this simple thing right!
Close the Display and my selection is
still in the system clipboard.

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.RTFTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class bb {
   public static void main(String[] args) {
     Display display = new Display();
     final Clipboard cb = new Clipboard(display);
     Shell shell = new Shell(display);
     final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);
     text.setBounds(10, 10, 300, 300);
     Button button = new Button(shell, SWT.PUSH);
     button.setText("Copy");
     button.setBounds(320, 10, 100, 40);
     button.addListener(SWT.Selection, new Listener() {
       public void handleEvent(Event e) {
         String textData = text.getSelectionText();
         if (textData == null)
           return;
         // to show the rtf formatting, make the text bold and italic
         String rtfData = "{\\rtf1\\b\\i " + textData + "}";
         TextTransfer textTransfer = TextTransfer.getInstance();
         RTFTransfer rtfTransfer = RTFTransfer.getInstance();
         Transfer[] types = new Transfer[] { textTransfer, rtfTransfer };
         cb.setContents(new Object[] { textData, rtfData }, types);
       }
     });
     button = new Button(shell, SWT.PUSH);
     button.setText("Paste");
     button.setBounds(320, 60, 100, 40);
     button.addListener(SWT.Selection, new Listener() {
       public void handleEvent(Event e) {
         TextTransfer transfer = TextTransfer.getInstance();
         String data = (String) cb.getContents(transfer);
         if (data == null)
           return;
         text.insert(data);
       }
     });
     shell.open();
     while (!shell.isDisposed()) {
       if (!display.readAndDispatch())
         display.sleep();

     }
     cb.dispose();
     display.dispose();
   }
}

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web