can someone point me to how to fix this issue
inserting html content (marked up with <br> or <p style=margin-top:0>) in JTextPane does not show the correct view of the content
SENERIO #1
-----------------
inserting
signatureInHTML="<p>Name</p><p>Company</p><p>PhoneNo.</p>"
with
document.insertBeforeEnd(bodyElement, signatureInHTML);
puts empty lines in between Name, Company and PhoneNo
SENERIO #2
-----------------
inserting
signatureInHTML="Name<br>Company<br>PhoneNo.<br>"
with
document.insertBeforeEnd(bodyElement, signatureInHTML);
puts empty lines and centers the content
If I use
signatureInHTML="<p style='margin-top: 0; margin-bottom: 0'>Name</p><p style='margin-top: 0; margin-bottom: 0'>Company</p><p style='margin-top: 0; margin-bottom: 0'>PhoneNo.</p><p style='margin-top: 0; margin-bottom: 0'></p>"
it displays correctly.
NOTE: for the 2 cases where is does not display correctly, if I view source
with View Source menu item, don't change a thing and hit Save button,
it corrects the display, even though no HTML changes were entered.
can someone comment and help
here is my SSCCE proggie,
(camickr kicked my butt last time I did not post properly :-))
changing convertPlainTextToHTML(..)
toggles broken code with working code
package testing;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.border.EmptyBorder;
public class MyHTMLExample extends JFrame implements ActionListener {
private HTMLDocument document;
private JTextPane textPane = new JTextPane();
private Action insertBreakAction = new DefaultEditorKit.InsertBreakAction();
String signature = "FirstName LastName\nCompany\nPhoneNo.\n";
public MyHTMLExample() {
super("MyHTMLExample");
init();
}
public static void main(String[] args) {
MyHTMLExample editor = new MyHTMLExample();
}
public void init() {
addWindowListener(new FrameListener());
JMenuBar menuBar = new JMenuBar();
getContentPane().add(menuBar, BorderLayout.NORTH);
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("View Source and Insert Signature");
JMenuItem srcItem = new JMenuItem("View Source");
srcItem.addActionListener(this);
helpMenu.add(srcItem);
JMenuItem sigItem = new JMenuItem("Insert Signature");
sigItem.addActionListener(this);
helpMenu.add(sigItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem exitItem = new JMenuItem("Exit", new ImageIcon("exit.gif"));
exitItem.addActionListener(this);
fileMenu.add(exitItem);
JMenuItem insertBreaKItem = new JMenuItem(insertBreakAction);
insertBreaKItem.setText("Break");
editMenu.add(insertBreaKItem);
JPanel editorControlPanel = new JPanel();
editorControlPanel.setLayout(new FlowLayout());
JPanel specialPanel = new JPanel();
specialPanel.setLayout(new FlowLayout());
JPanel alignPanel = new JPanel();
alignPanel.setLayout(new FlowLayout());
JScrollPane scrollPane = new JScrollPane(textPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension scrollPaneSize = new Dimension(5 * screenSize.width / 8,
5 * screenSize.height / 8);
scrollPane.setPreferredSize(scrollPaneSize);
JPanel toolPanel = new JPanel();
toolPanel.setLayout(new BorderLayout());
toolPanel.add(editorControlPanel, BorderLayout.NORTH);
toolPanel.add(specialPanel, BorderLayout.CENTER);
toolPanel.add(alignPanel, BorderLayout.SOUTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
startNewDocument();
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String actionCommand = ae.getActionCommand();
if (actionCommand.compareTo("Exit") == 0) {
exit();
}
else if(actionCommand.compareTo("Insert Signature") == 0)
{
insertSignature();
}
else if (actionCommand.compareTo("View Source") == 0) {
Action viewSourceAction = new ViewSourceAction();
viewSourceAction.actionPerformed(ae);
/*
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
ta.setText(textPane.getText());
f.getContentPane().add(sp, BorderLayout.CENTER);
f.setSize(400, 500);
f.setVisible(true);
*/
}
}
public void startNewDocument()
{
HTMLEditorKit editorKit = new HTMLEditorKit();
textPane.setContentType("text/html");
// Here's where we force the pane to use our new editor kit
textPane.setEditorKit(editorKit);
document = (HTMLDocument) editorKit.createDefaultDocument();
textPane.setDocument(document);
// add <style> for <p> tags
Style mainStyle = document.getStyle(StyleContext.DEFAULT_STYLE);
Style marginStylePlusMainStyle = textPane.addStyle("p", mainStyle);
StyleConstants.setSpaceBelow(marginStylePlusMainStyle, 0f);
StyleConstants.setSpaceAbove(marginStylePlusMainStyle, 0f);
}
private String convertPlainTextToHTML(String plainText)
{
// replace newlines with <p> tags
// seems to be a bug where if I do not put style in the p tag, the view of
// the html in JTextPane shows empty lines in between signature lines of text.
// When I view content with CNTRL-ALT-S and hit save, it displays correctly
// If I put style attribute in p tags, it displays correct first time
String signatureInHTML = "";
// BROKEN: this one displays empty lines in between signature lines
signatureInHTML = ("<p>" +plainText.replaceAll("\n", "</p>\n<p>") +"</p>").trim();
// BROKEN: this one displays empty lines and centers content in between signature lines
//signatureInHTML = plainText.replaceAll("\n", "<br>").trim();
// works
//signatureInHTML = "<p style='margin-top: 0; margin-bottom: 0'>" +
// plainText.replaceAll("\n","</p>\n<p style='margin-top: 0; margin-bottom: 0'>") +
// "</p>";
return signatureInHTML;
}
private void insertSignature()
{
String signatureInHTML = convertPlainTextToHTML(signature);
Element bodyElement = getElement("body");
HTMLDocument document = (HTMLDocument)textPane.getDocument();
try
{
document.insertBeforeEnd(bodyElement,
signatureInHTML);
}
catch(Exception ex) { ex.printStackTrace(); }
}
private Element getElement(String name)
{
ElementIterator iterator = new ElementIterator(textPane.getDocument());
Element element;
while ((element = iterator.next()) != null) {
AttributeSet attributes = element.getAttributes();
Object attributeName = attributes.getAttribute(StyleConstants.NameAttribute);
if (attributeName instanceof HTML.Tag) {
if (element.getName().equals(name))
return element;
}
}
return null;
}
public void exit() {
String exitMessage = "Are you sure you want to exit?";
if (JOptionPane.showConfirmDialog(this, exitMessage) ==
JOptionPane.YES_OPTION) {
System.exit(0);
}
}
class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent we) {
exit();
}
}
class HtmlSourceDlg extends JDialog {
protected boolean m_succeeded = false;
protected JTextArea m_sourceTxt;
public HtmlSourceDlg(JFrame parent, String source) {
super(parent, "HTML Source", true);
JPanel pp = new JPanel(new BorderLayout());
pp.setBorder(new EmptyBorder(10, 10, 5, 10));
m_sourceTxt = new JTextArea(source, 20, 60);
m_sourceTxt.setFont(new Font("Courier", Font.PLAIN, 12));
JScrollPane sp = new JScrollPane(m_sourceTxt);
pp.add(sp, BorderLayout.CENTER);
JPanel p = new JPanel(new FlowLayout());
JPanel p1 = new JPanel(new GridLayout(1, 2, 10, 0));
JButton bt = new JButton("Save");
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
m_succeeded = true;
dispose();
}
};
bt.addActionListener(lst);
p1.add(bt);
bt = new JButton("Cancel");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
};
bt.addActionListener(lst);
p1.add(bt);
p.add(p1);
pp.add(p, BorderLayout.SOUTH);
getContentPane().add(pp, BorderLayout.CENTER);
pack();
setResizable(true);
setLocationRelativeTo(parent);
}
public boolean succeeded() {
return m_succeeded;
}
public String getSource() {
return m_sourceTxt.getText();
}
}
class ViewSourceAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
try {
HTMLEditorKit m_kit = (HTMLEditorKit)MyHTMLExample.this.textPane.getEditorKit();
HTMLDocument m_doc = (HTMLDocument)MyHTMLExample.this.textPane.getDocument();
StringWriter sw = new StringWriter();
m_kit.write(sw, m_doc, 0, m_doc.getLength());
sw.close();
HtmlSourceDlg dlg = new HtmlSourceDlg(null, sw.toString());
dlg.setVisible(true);
if (!dlg.succeeded())
return;
StringReader sr = new StringReader(dlg.getSource());
m_doc = createDocument();
m_kit.read(sr, m_doc, 0);
sr.close();
MyHTMLExample.this.textPane.setDocument(m_doc);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
private HTMLDocument createDocument() {
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
StyleSheet styles = htmlEditorKit.getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
HTMLDocument doc = new HTMLDocument(ss);
return doc;
}
}
I would like to have it display correctly and secondly, it be nice
to have a style section in the head like so
<style type="text/css">
<!--
p { margin-top: 0.0; margin-bottom: 0.0 }
-->
</style>
and not have every <p> tag that gets inserted when hitting CR
put <p style=margin-top:0>
can someone tell me how to accomplish one or both of these
thanks