Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

JTextPane ("text/html") : overwrite default behavior???

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to change the behavior of JTextPane one and for all?
Actually, I am trying to develop a HTMLEditor and I want to make it to feel like normal text editor as close as possible... ie when user click [ENTER], a single line <br> should be inserted, instead of a paragraph <p></p>.
For the time being, I have managed to overwrite the [ENTER] action but this is not good enuf because this behavior also apply to PASTE action as well!
Any comment is appreciated. Thx in adv.
Rgds
Eric Low
SCJP2
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same problem. I searched the web site and find 2 solution.
1) Generate a pair of <p> </p> tag after press an enter key is a "standard" behavior of most of word product(frontpage, word...). But JTextPane and JEditor doesn't suport another common behavior which is to generate a <br> tag after press [SHIFT] + [ENTER]. You may add this combination to your keymap. It didn't work in my project.
2) put you css in Your JTextPane or JEditorPane. That's means change the behavior of how a paragraph looks like. There is an example in the web site. Here is the link: http://www.jalice.net/attributes.htm and look at the source code of "Zerospace". But you still have to replace <p></p> tag to <br> by yourself.
I hope that will help you a little bit. Good luck.
Renee
 
Eric Low
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Renee,
Thanks for your reply.
As far as I am concern, CSS is used to "style" the presentation of the HTML document. What I really need is to change the "behavior" of JTextPane.
For e.g., I can think of 3 situations where <p></p> will be inserted "automatically":
1. By default, when the JTextPane is empty.
2. When user press [ENTER].
3. During PASTE action.
How can I change these "standard" behavior? I am developing a HTML editor meant for dumb user, that's why I want to make it to behave as close to, let say, MS Word.... i.e. again, the [ENTER] will trigger a single new line, NOT paragraph!
Please help me!
Eric Low
SCJP2
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
Here is a sample code. When user enters [ENTER] key, you will see the behavior is correct although you will get a pair of <p></p> tags. This is the way I do it. The problem is that when you want to display saved html in JTextPane next time, you have to replace all <br> tags to <p></p> tags. I am not sure if this will help you or not. As I said before this is the way I solved the problem before deadline and you may overwrite [ENTER] key or add [SHIFT][ENTER] to yourTextPane's keymap. Good luck to you.

import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class test extends JFrame {
private StyleSheet css = new StyleSheet();
private JEditorPane editor = new JEditorPane();
private HTMLEditorKit kit = new HTMLEditorKit();
private HTMLDocument doc = new HTMLDocument();
private JButton showSource = new JButton("Show Source");
public test() {
css.addRule("BODY{ margin : 0;}");
kit.setStyleSheet(css);
editor.setDocument(doc);
editor.setEditorKit(kit);
editor.setBorder(null);
editor.setEditable(true);
JScrollPane scroll = new JScrollPane(editor);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(scroll, BorderLayout.CENTER);
this.getContentPane().add(showSource, BorderLayout.SOUTH);
this.showSource.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("Original html:");
System.out.println(editor.getText());
System.out.println("After you take care of <p></p>");
System.out.println(takeCareOfPTag(editor.getText()));
}
});
setSize(220, 180);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public String takeCareOfPTag (String origHtmlString) {
if (origHtmlString == null) return null;
//Step 1:RemoveAll invisible characters which ASII value is less than 32
StringBuffer sb = new StringBuffer(origHtmlString);
for (int i = sb.length()-1; i >=0; i--) {
char aChar = sb.charAt(i);
if (((int)aChar) < 32)
sb.deleteCharAt(i);
}
//Step 2 elete all <P> or <p>
int index5 = sb.lastIndexOf("<P>");
while (index5 >= 0) {
sb.delete(index5, index5+ 3);
index5 = sb.lastIndexOf("<P>");
}
int index6 = sb.lastIndexOf("<p>");
while (index6 >= 0) {
sb.delete(index6, index6+3);
index6 = sb.lastIndexOf("<p>");
}
//Step 3:Replace all </p> by <br> and take care of white space
origHtmlString = sb.toString();
if (origHtmlString.indexOf("/p>") >0) {
String[] qTextString = origHtmlString.split("</p>");
if (qTextString != null) {
origHtmlString = qTextString[0].trim();
if (qTextString.length > 1) {
for (int i = 1; i < qTextString.length; i ++) {
origHtmlString = origHtmlString + "<br>" + qTextString[i].trim();
}
}
}
}
return origHtmlString;
}
public static void main(String[] args) {
new test().setVisible(true);
}
}

Renee
 
Eric Low
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Renee,
Thanks, it works somehow!
However, this can not solve the problem of the PASTE action which by default insert a "newline" with the text!
Anyway, thanks a lot!
Cheers
Eric Low
SCJP2
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic