OBCT
asked on
Inserting HTML to a JTextPane
This is hard to explain but I'll do my best.
I'm doing a basic html document editing application for a client (my first job with java :) and what I'm trying to achieve is to read a html document and add the text (without the html tags) into a JTextPane.
So far this has been successful with the setPage() method.
The problem I am having is inserting html tags to selected text....bold for example..... So using a StringBuffer "<B>" is added to the start and "</B>" to the end.
When I do a System.out.println(selecte dText), it prints out some of the html...
If you've ever used Dreamweaver for example, you'll see that you can select text, add bold tags and the text will appear bold without showing the inserted html tags.
How should I go about doing this?
If I haven't explained well enough, please let me know.
Cheers
-OBCT
I'm doing a basic html document editing application for a client (my first job with java :) and what I'm trying to achieve is to read a html document and add the text (without the html tags) into a JTextPane.
So far this has been successful with the setPage() method.
The problem I am having is inserting html tags to selected text....bold for example..... So using a StringBuffer "<B>" is added to the start and "</B>" to the end.
When I do a System.out.println(selecte
If you've ever used Dreamweaver for example, you'll see that you can select text, add bold tags and the text will appear bold without showing the inserted html tags.
How should I go about doing this?
If I haven't explained well enough, please let me know.
Cheers
-OBCT
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think I've jumped in the deep end here. I'm having lots of trouble...
I'm trying out the HTMLEditorKit so give me a little bit and I'll see how I go.
My main problem is...
When I select some text from the JEditorPane (I'm using that now because it seems to have more functionality with html) and do a System.out.println(), it will print out any html that the text uses.
For example...
If I select 'Click here to email me'
It will print out 'Click <a href="myemail@email.com">h ere</a> to email me'
Is there any way to prevent html from being selected in the JEditorPane?
I'm trying out the HTMLEditorKit so give me a little bit and I'll see how I go.
My main problem is...
When I select some text from the JEditorPane (I'm using that now because it seems to have more functionality with html) and do a System.out.println(), it will print out any html that the text uses.
For example...
If I select 'Click here to email me'
It will print out 'Click <a href="myemail@email.com">h
Is there any way to prevent html from being selected in the JEditorPane?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> Is there any way to prevent html from being selected in the JEditorPane?
you could disable the editable region of JEditorPane like :
JEditorPane x = new JEditorPane();
x.setEditable(false);
you could disable the editable region of JEditorPane like :
JEditorPane x = new JEditorPane();
x.setEditable(false);
> I think I've jumped in the deep end here. I'm having lots of trouble...
Think that the best way to do that is to have an example, click the download link :
http://forum.java.sun.com/thread.jsp?thread=502981&forum=57&message=2379998
If you really want to display a html page like a web browser then here is a sample :
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html
Hope that helps . . .
Javatm
Think that the best way to do that is to have an example, click the download link :
http://forum.java.sun.com/thread.jsp?thread=502981&forum=57&message=2379998
If you really want to display a html page like a web browser then here is a sample :
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html
Hope that helps . . .
Javatm
Or you could use this from the above sample :
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Browser extends JFrame implements HyperlinkListener,
ActionListener {
public static void main(String[] args) {
if (args.length == 0)
new Browser("http://www.google.com/");
else
new Browser(args[0]);
}
private JButton homeButton;
private JTextField urlField;
private JEditorPane htmlPane;
private String initialURL;
public Browser(String initialURL) {
super("Simple Swing Browser");
this.initialURL = initialURL;
JPanel topPanel = new JPanel();
topPanel.setBackground(Col or.lightGr ay);
homeButton = new JButton("Home");
homeButton.addActionListen er(this);
JLabel urlLabel = new JLabel("URL:");
urlField = new JTextField(30);
urlField.setText(initialUR L);
urlField.addActionListener (this);
topPanel.add(homeButton);
topPanel.add(urlLabel);
topPanel.add(urlField);
getContentPane().add(topPa nel, BorderLayout.NORTH);
try {
htmlPane = new JEditorPane(initialURL);
htmlPane.setEditable(false );
htmlPane.addHyperlinkListe ner(this);
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrol lPane, BorderLayout.CENTER);
} catch(IOException ioe) {
warnUser("Can't build HTML pane for " + initialURL
+ ": " + ioe);
}
Dimension screenSize = getToolkit().getScreenSize ();
int width = screenSize.width * 8 / 10;
int height = screenSize.height * 8 / 10;
setBounds(width/8, height/8, width, height);
setVisible(true);
}
public void actionPerformed(ActionEven t event) {
String url;
if (event.getSource() == urlField)
url = urlField.getText();
else // Clicked "home" button instead of entering URL
url = initialURL;
try {
htmlPane.setPage(new URL(url));
urlField.setText(url);
} catch(IOException ioe) {
warnUser("Can't follow link to " + url + ": " + ioe);
}
}
public void hyperlinkUpdate(HyperlinkE vent event) {
if (event.getEventType() == HyperlinkEvent.EventType.A CTIVATED) {
try {
htmlPane.setPage(event.get URL());
urlField.setText(event.get URL().toEx ternalForm ());
} catch(IOException ioe) {
warnUser("Can't follow link to "
+ event.getURL().toExternalF orm() + ": " + ioe);
}
}
}
private void warnUser(String message) {
JOptionPane.showMessageDia log(this, message, "Error",
JOptionPane.ERROR_MESSAGE) ;
}
}
Hope that helps . . .
Javatm
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Browser extends JFrame implements HyperlinkListener,
ActionListener {
public static void main(String[] args) {
if (args.length == 0)
new Browser("http://www.google.com/");
else
new Browser(args[0]);
}
private JButton homeButton;
private JTextField urlField;
private JEditorPane htmlPane;
private String initialURL;
public Browser(String initialURL) {
super("Simple Swing Browser");
this.initialURL = initialURL;
JPanel topPanel = new JPanel();
topPanel.setBackground(Col
homeButton = new JButton("Home");
homeButton.addActionListen
JLabel urlLabel = new JLabel("URL:");
urlField = new JTextField(30);
urlField.setText(initialUR
urlField.addActionListener
topPanel.add(homeButton);
topPanel.add(urlLabel);
topPanel.add(urlField);
getContentPane().add(topPa
try {
htmlPane = new JEditorPane(initialURL);
htmlPane.setEditable(false
htmlPane.addHyperlinkListe
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrol
} catch(IOException ioe) {
warnUser("Can't build HTML pane for " + initialURL
+ ": " + ioe);
}
Dimension screenSize = getToolkit().getScreenSize
int width = screenSize.width * 8 / 10;
int height = screenSize.height * 8 / 10;
setBounds(width/8, height/8, width, height);
setVisible(true);
}
public void actionPerformed(ActionEven
String url;
if (event.getSource() == urlField)
url = urlField.getText();
else // Clicked "home" button instead of entering URL
url = initialURL;
try {
htmlPane.setPage(new URL(url));
urlField.setText(url);
} catch(IOException ioe) {
warnUser("Can't follow link to " + url + ": " + ioe);
}
}
public void hyperlinkUpdate(HyperlinkE
if (event.getEventType() == HyperlinkEvent.EventType.A
try {
htmlPane.setPage(event.get
urlField.setText(event.get
} catch(IOException ioe) {
warnUser("Can't follow link to "
+ event.getURL().toExternalF
}
}
}
private void warnUser(String message) {
JOptionPane.showMessageDia
JOptionPane.ERROR_MESSAGE)
}
}
Hope that helps . . .
Javatm
ASKER
>you need to stip the text out of the html
I just tried than and it shows up as normal text where as I need it to show any bold font, colored text etc
Am I going about this the right way?
I just tried than and it shows up as normal text where as I need it to show any bold font, colored text etc
Am I going about this the right way?
Are you doing an editor keyword highlighting on html document ?
ASKER
Sorry, I dont understand...what do you mean?
> Sorry, I dont understand...what do you mean?
Are you trieng to create a html editor like dreamweaver ?
If Yes then I can help you about it cause I've created an example just
for this question.
Are you trieng to create a html editor like dreamweaver ?
If Yes then I can help you about it cause I've created an example just
for this question.
ASKER
Yes I am.
I can give you the link and you can download it is that okey :)
ASKER
Yeh thats fine, cheers.
I've done my best and here is a simple example :
http://velasquez.2.forumer.com/index.php?showtopic=13&st=0&
Click on the Editor.zip to download it.
Hope that helps . . .
Javatm
http://velasquez.2.forumer.com/index.php?showtopic=13&st=0&
Click on the Editor.zip to download it.
Hope that helps . . .
Javatm
ASKER
Thanks Javatm, that's helped me alot however I don't think I'm explaining my problem well enough so I'll try once more...
I have loaded a html page into my JEditorPane using the setPage() method.
In doing this, any html tags that effect the appearance of the text will show up in the text area. E.g. any fonts with <b>mytext</b> will appear bold and the tags will be hidden.
When a user selects some text and clicks the bold button, the aim is to add the bold tags onto the string then replace the selected string with the newly appended string and somehow hide the bold tags so the text will appear bold without the tags being visible.
I'm using this method to add the tags and attempt to replace the text but that’s when the dramas occur.
public static void addBoldTags()
{
int start = HtmlEditorGUI.htmlTextBox. getSelecti onStart();
int end = HtmlEditorGUI.htmlTextBox. getSelecti onEnd();
String selection = HtmlEditorGUI.htmlTextBox. getText(). substring( start, end);
System.out.println(selecti on);
StringBuffer newText = new StringBuffer(selection);
newText.insert(selection.l ength(), "<b>");
newText.insert(0, "</b>");
HtmlEditorGUI.htmlTextBox. replaceSel ection(new Text.toStr ing());
}
If you can somehow try selecting some text in a JEditorPane that has had the setPage() method used.....(so google for example shows up)....you will notice the selected text contains html tags.
Please let me know if this makes sense.
Thanks for all your help so far.
Cheers
-OBCT
I have loaded a html page into my JEditorPane using the setPage() method.
In doing this, any html tags that effect the appearance of the text will show up in the text area. E.g. any fonts with <b>mytext</b> will appear bold and the tags will be hidden.
When a user selects some text and clicks the bold button, the aim is to add the bold tags onto the string then replace the selected string with the newly appended string and somehow hide the bold tags so the text will appear bold without the tags being visible.
I'm using this method to add the tags and attempt to replace the text but that’s when the dramas occur.
public static void addBoldTags()
{
int start = HtmlEditorGUI.htmlTextBox.
int end = HtmlEditorGUI.htmlTextBox.
String selection = HtmlEditorGUI.htmlTextBox.
System.out.println(selecti
StringBuffer newText = new StringBuffer(selection);
newText.insert(selection.l
newText.insert(0, "</b>");
HtmlEditorGUI.htmlTextBox.
}
If you can somehow try selecting some text in a JEditorPane that has had the setPage() method used.....(so google for example shows up)....you will notice the selected text contains html tags.
Please let me know if this makes sense.
Thanks for all your help so far.
Cheers
-OBCT
I'm quite having a problem understanding your situation actually there many things that we have
to consider, can you submit the codes on the the site that I gave you above so that I can see
and try to debug the codes thanks.
Javatm
to consider, can you submit the codes on the the site that I gave you above so that I can see
and try to debug the codes thanks.
Javatm
As I said above you need to parse the html to extract the text.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just dont forget to import :
import javax.swing.text.*;
To view the solution and the example go here :
http://velasquez.2.forumer.com/index.php?showtopic=13
Hope that helps . . .
Javatm
import javax.swing.text.*;
To view the solution and the example go here :
http://velasquez.2.forumer.com/index.php?showtopic=13
Hope that helps . . .
Javatm
ASKER
Have a look at the following class. It's a simplified version of what I'm doing. I'll make more comments at the bottom.
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class Demo extends JFrame implements ActionListener
{
JPanel mainPanel;
JEditorPane htmlTextBox;
JTextPane viewTextBox;
JButton addBoldBtn;
JScrollPane scrollPane;
public Demo() throws IOException
{
mainPanel = new JPanel(new BorderLayout());
htmlTextBox = new JEditorPane("http://www.google.com");
addBoldBtn = new JButton("Add bold tags");
scrollPane = new JScrollPane(htmlTextBox);
addBoldBtn.addActionListen er(this);
scrollPane.setPreferredSiz e(new Dimension(300,200));
mainPanel.add(addBoldBtn, BorderLayout.PAGE_START);
mainPanel.add(scrollPane, BorderLayout.CENTER);
setDefaultCloseOperation(E XIT_ON_CLO SE);
getContentPane().add(mainP anel);
setResizable(false);
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEven t e)
{
String selection = htmlTextBox.getSelectedTex t();
System.out.println(selecti on);
StringBuffer newText = new StringBuffer(selection);
newText.insert(selection.l ength(), "</b>");
newText.insert(0, "<b>");
htmlTextBox.replaceSelecti on(newText .toString( ));
}
public static void main(String[] args)
{
try { Demo d = new Demo(); }
catch (IOException e) { e.printStackTrace(); }
}
}
My first problem was I wasn't using common sense, so I've fixed alot of small problems.
When you run this class, the frame will show and googles' website will apear in the JEditorPane. The html isn't visible but any text that has a link, bold etc will show up as it should on a webpage.
Select any text and click the "Add bold tags" button, then you'll see '<B>' and '</B>' appended on the start and end of the selected text.
How am I meant to hide the bold tags that I'm adding to the text and have that text formatted so it appears as bold in the JEditorPane.
The people using this won't have any knowledge of html so having a html view isn't an option.
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class Demo extends JFrame implements ActionListener
{
JPanel mainPanel;
JEditorPane htmlTextBox;
JTextPane viewTextBox;
JButton addBoldBtn;
JScrollPane scrollPane;
public Demo() throws IOException
{
mainPanel = new JPanel(new BorderLayout());
htmlTextBox = new JEditorPane("http://www.google.com");
addBoldBtn = new JButton("Add bold tags");
scrollPane = new JScrollPane(htmlTextBox);
addBoldBtn.addActionListen
scrollPane.setPreferredSiz
mainPanel.add(addBoldBtn, BorderLayout.PAGE_START);
mainPanel.add(scrollPane, BorderLayout.CENTER);
setDefaultCloseOperation(E
getContentPane().add(mainP
setResizable(false);
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEven
{
String selection = htmlTextBox.getSelectedTex
System.out.println(selecti
StringBuffer newText = new StringBuffer(selection);
newText.insert(selection.l
newText.insert(0, "<b>");
htmlTextBox.replaceSelecti
}
public static void main(String[] args)
{
try { Demo d = new Demo(); }
catch (IOException e) { e.printStackTrace(); }
}
}
My first problem was I wasn't using common sense, so I've fixed alot of small problems.
When you run this class, the frame will show and googles' website will apear in the JEditorPane. The html isn't visible but any text that has a link, bold etc will show up as it should on a webpage.
Select any text and click the "Add bold tags" button, then you'll see '<B>' and '</B>' appended on the start and end of the selected text.
How am I meant to hide the bold tags that I'm adding to the text and have that text formatted so it appears as bold in the JEditorPane.
The people using this won't have any knowledge of html so having a html view isn't an option.
Did you try my other solution ?
because the easiest way to do that is the way I did it, see my comments above :)
because the easiest way to do that is the way I did it, see my comments above :)
ASKER
Yes I did but the users wont know any html so they're not going to know what the syntax means if they see it.
Is there any method avaliable that get the JEditorPane to read its content (including html tags) and re-read/reparse the content so the text that has been changed will now show up as being bold???
Is there any method avaliable that get the JEditorPane to read its content (including html tags) and re-read/reparse the content so the text that has been changed will now show up as being bold???
> Yes I did but the users wont know any html so they're not going to know what the syntax means if they see it.
You told me that you are creating an editor like Dreamweaver, that's why I gave you an example.
Your project seems to be hard its like a browser, the problem w/ that is you want to edit the
webpage it self.
You want to do it in a single frame ? in that case then you need to make a temporary file which
will get the page from JEditorPane and save it in a file so that when you want to edit it its there
hidden in a temporary file.
Then again what I gave you should already be a normal standard of an html editor.
You told me that you are creating an editor like Dreamweaver, that's why I gave you an example.
Your project seems to be hard its like a browser, the problem w/ that is you want to edit the
webpage it self.
You want to do it in a single frame ? in that case then you need to make a temporary file which
will get the page from JEditorPane and save it in a file so that when you want to edit it its there
hidden in a temporary file.
Then again what I gave you should already be a normal standard of an html editor.
ASKER
I got it working using this :)
String htmlText = htmlTextBox.getText();
String newText = htmlTextBox.getSelectedTex t();
StringBuffer newTextSb = new StringBuffer(newText);
StringBuffer htmlTextSb = new StringBuffer(htmlText);
int start = htmlText.indexOf(newText);
int end = start + newText.length();
try
{
htmlTextSb.insert(end, "</b>");
htmlTextSb.insert(start, "<b>");
}
catch (StringIndexOutOfBoundsExc eption ex)
{
System.out.println("Text already bold");
}
htmlTextBox.setText(htmlTe xtSb.toStr ing());
Thank you both for all your help. I couldn't have done it without you :)
Cheers
-OBCT
String htmlText = htmlTextBox.getText();
String newText = htmlTextBox.getSelectedTex
StringBuffer newTextSb = new StringBuffer(newText);
StringBuffer htmlTextSb = new StringBuffer(htmlText);
int start = htmlText.indexOf(newText);
int end = start + newText.length();
try
{
htmlTextSb.insert(end, "</b>");
htmlTextSb.insert(start, "<b>");
}
catch (StringIndexOutOfBoundsExc
{
System.out.println("Text already bold");
}
htmlTextBox.setText(htmlTe
Thank you both for all your help. I couldn't have done it without you :)
Cheers
-OBCT
http://javaalmanac.com/egs/javax.swing.text/style_HiliteWords2.html