JTextPane how to get text w/o the html markup
hi all, i am struggling with this, can someone help
if I have a
JTextPane, HTMLDocument, HTMLEditorKit
combination
and I have this for my html
<html>
<head>
</head>
<body>
<p style="margin-top: 0">
Dear <b>Customer</b>,Thanks
</body>
</html>
how can I call something like textPane.getText() and have it return
Dear Customer, Thanks
with no html markup
I am thinking it must be stored somewhere, or at least figured out because if I call
int p0 = editor.getSelectionStart();
and my cursor is after Dear, it returns 4, so getting the cursor
off the editor skipped over <html><head>... and returned
where my caret was as if I had no html markup.
I just want the text I am seeing on the screen
can someone point me how to do this
thanks
robert.walker@verizonwireless.com.delete.this.part
# 3
Works fine for me using JDK1.4.2 on XP.
If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",
see http://homepage1.nifty.com/algafield/sscce.html,
that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the "Code Formatting Tags",
see http://forum.java.sun.com/help.jspa?sec=formatting,
so the posted code retains its original formatting.
# 4
it works, thanks again
in case anyone is interested
package test;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.undo.*;
public class SimpleHTMLEditor extends JFrame implements ActionListener {
private HTMLDocument document;
private JTextPane textPane = new JTextPane();
private Action boldAction = new StyledEditorKit.BoldAction();
public SimpleHTMLEditor() {
super("SimpleHTMLEditor");
init();
}
public static void main(String[] args) {
SimpleHTMLEditor editor = new SimpleHTMLEditor();
}
public void init() {
addWindowListener(new FrameListener());
JMenuBar menuBar = new JMenuBar();
getContentPane().add(menuBar, BorderLayout.NORTH);
JMenu styleMenu = new JMenu("Style");
JMenu helpMenu = new JMenu("Other");
JMenuItem srcItem = new JMenuItem("View Source");
srcItem.addActionListener(this);
helpMenu.add(srcItem);
JMenuItem textPlainItem = new JMenuItem("Print text/plain");
textPlainItem.addActionListener(this);
helpMenu.add(textPlainItem);
menuBar.add(styleMenu);
menuBar.add(helpMenu);
JMenuItem boldMenuItem = new JMenuItem(boldAction);
boldMenuItem.setText("Bold");
styleMenu.add(boldMenuItem);
JMenuItem helpItem = new JMenuItem("Other");
helpItem.addActionListener(this);
helpMenu.add(helpItem);
JScrollPane scrollPane = new JScrollPane(textPane);
JPanel toolPanel = new JPanel();
toolPanel.setLayout(new BorderLayout());
getContentPane().add(scrollPane, BorderLayout.CENTER);
this.setSize(400,400);
startNewDocument();
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String actionCommand = ae.getActionCommand();
if (actionCommand.compareTo("Exit") == 0) {
exit();
}
else if (actionCommand.compareTo("View Source") == 0) {
// this show text with html markup */
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);
}
else if (actionCommand.compareTo("Print text/plain") == 0) {
// i want to see text with NO html markup
try
{
String textWithNoMarkup =
textPane.getDocument().getText(0,
textPane.getDocument().getLength());
System.out.println(textWithNoMarkup);
}
catch(Exception e) { e.printStackTrace(); }
}
}
public void startNewDocument() {
HTMLEditorKit editorKit = new HTMLEditorKit();
textPane.setContentType("text/html");
textPane.setEditorKit(editorKit);
document = (HTMLDocument) editorKit.createDefaultDocument();
textPane.setDocument(document);
setTitle("SimpleHTMLEditor");
}
public void exit() {
System.exit(0);
}
class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent we) {
exit();
}
}
}