🔒 Locked
[java] HTML output
Advertisement
Original Post
Hey,
Does anybody have experience with the
javax.swing.text.html.HTMLWriter
class?
I would like to produce HTML output from my app, and this might be an option but it would be nice to see an example using this class to see if I can use it the way I intend to. So if you know where I could find an example or a more detailed description than the one in the JavaDocs I''d appreciate it very much.
Thanks,
Stefan.
******************************
Stefan Baert
On the day we create intelligence and consciousness, mankind becomes God.
On the day we create intelligence and consciousness, mankind becomes obsolete...
******************************
from an old techrepublic''s techtip:
Today''s Java tip
RENDERING HTML WITH SWING
A common need for Java developers, especially in the context of help
systems, is to display rendered HTML within their Swing applications.
Some programs simply use the installed browser on the user''s system to
display URLs in a separate window, but that approach can be clumsy and
requires platform-specific code.
Fortunately, Swing provides a simpler, integrated way to display and
interact with basic HTML or RTF formats: the JEditorPane.
To use JEditorPane as an HTML-rendering component, you''ll need to:
* Set the editable property to false.
* Create and register a HyperlinkListener object if you want links to
be active.
* Provide the URL or HTML text to render using one of the setPage()
methods or in the constructor.
Here''s a simple HTML renderer window that just displays a single URL
and leaves exception and hyperlink handling as an exercise for the
reader (you''ll need to be online for the URL to work):
There are some limitations to JEditorPane: it doesn''t understand style
sheets; it doesn''t have java-script; it doesn''t handle many non-English
languages very well, if at all; and it''s fairly brain-dead about
complex or nonconformant HTML tags. You''re not going to build a
complete browser out of JEditorPane, but you can display
straightforward, hyperlinked information with tables and images, and
that''s all that''s needed for many applications. You can also do quite a
lot to customize JEditorPane''s behavior, so be sure to read the API
description thoroughly to explore the available options.
HTML files can be a simple way to present formatted, hyperlinked
documentation systems. The JEditorPane class provides a simple,
effective method to present and manage that content for both online and
offline environments.
I believe I have an article on this straight from sun.. I''ll dig through my email archives and check for you..
War doesn''t determine who is right, war determines who is left.
Today''s Java tip
RENDERING HTML WITH SWING
A common need for Java developers, especially in the context of help
systems, is to display rendered HTML within their Swing applications.
Some programs simply use the installed browser on the user''s system to
display URLs in a separate window, but that approach can be clumsy and
requires platform-specific code.
Fortunately, Swing provides a simpler, integrated way to display and
interact with basic HTML or RTF formats: the JEditorPane.
To use JEditorPane as an HTML-rendering component, you''ll need to:
* Set the editable property to false.
* Create and register a HyperlinkListener object if you want links to
be active.
* Provide the URL or HTML text to render using one of the setPage()
methods or in the constructor.
Here''s a simple HTML renderer window that just displays a single URL
and leaves exception and hyperlink handling as an exercise for the
reader (you''ll need to be online for the URL to work):
|
There are some limitations to JEditorPane: it doesn''t understand style
sheets; it doesn''t have java-script; it doesn''t handle many non-English
languages very well, if at all; and it''s fairly brain-dead about
complex or nonconformant HTML tags. You''re not going to build a
complete browser out of JEditorPane, but you can display
straightforward, hyperlinked information with tables and images, and
that''s all that''s needed for many applications. You can also do quite a
lot to customize JEditorPane''s behavior, so be sure to read the API
description thoroughly to explore the available options.
HTML files can be a simple way to present formatted, hyperlinked
documentation systems. The JEditorPane class provides a simple,
effective method to present and manage that content for both online and
offline environments.
I believe I have an article on this straight from sun.. I''ll dig through my email archives and check for you..
War doesn''t determine who is right, war determines who is left.
WOW!! that seems really straight forward. Do you think you could mail me that article as well??
Be
S
It''s Da BOMB Baby!!!
. o O ~
A little nonsense now and then,
is relished by the wisest men
~ O o .
-- Willy Wonka
Be
SIt''s Da BOMB Baby!!!
. o O ~
A little nonsense now and then,
is relished by the wisest men
~ O o .
-- Willy Wonka
i'm sorry, but i don't know a damn thing about "javax.swing.text.html.HTMLWriter"... but it shouldn't be too hard to make your own class that outputs HTML (after all, it is just text with a few tags here and there).
what exactly are you trying to do?
--- krez (krezisback@aol.com)
Edited by - krez on December 5, 2001 4:50:37 PM
what exactly are you trying to do?
--- krez (krezisback@aol.com)
Edited by - krez on December 5, 2001 4:50:37 PM
Krez,
I think that''s what I''m going to do, but before I start writing my own class I usually first check out the Java libraries and this one was an option. However I tinkered with it, but couldn''t get it quite to do what I want to, so I''ll write my own class.
BTW, the thing I want to do is this: the app is turn-based and there''s a lot of statistical information. I''d like to output all this data after every turn to HTML (possibly with some graphs if possible) so the user can check them out later.
Thanks for the answers,
******************************
Stefan Baert
On the day we create intelligence and consciousness, mankind becomes God.
On the day we create intelligence and consciousness, mankind becomes obsolete...
******************************
I think that''s what I''m going to do, but before I start writing my own class I usually first check out the Java libraries and this one was an option. However I tinkered with it, but couldn''t get it quite to do what I want to, so I''ll write my own class.
BTW, the thing I want to do is this: the app is turn-based and there''s a lot of statistical information. I''d like to output all this data after every turn to HTML (possibly with some graphs if possible) so the user can check them out later.
Thanks for the answers,
******************************
Stefan Baert
On the day we create intelligence and consciousness, mankind becomes God.
On the day we create intelligence and consciousness, mankind becomes obsolete...
******************************
Two ways to do it, essentially. You can create a javax.swing.text.html.HTMLDocument object, then create nodes, add nodes, replicate nodes - things like that - then print it with an HTMLWriter class. Classes that might be usefull for you in manipulating the DOM are here [http://www.w3.org/TR/2001/WD-DOM-Level-3-Core-20010913/java-binding.html]. A simpler approach would be to create an HTML page that looks how you want it, cut it up into pieces, and have certain parts of your code output those pieces in order. For example, you might have a part that writes ''<HTML><BODY>'' to a stream, then another part that does
write ''<table>''
for (int x=0; x < items.length; ++x) {
write ''<tr><td>'' + items[x].name + ''</td></tr>'';
}
write ''</table>''
things like that. There''s also some other ways that are variations of the first, including XSLT and XPATH.
write ''<table>''
for (int x=0; x < items.length; ++x) {
write ''<tr><td>'' + items[x].name + ''</td></tr>'';
}
write ''</table>''
things like that. There''s also some other ways that are variations of the first, including XSLT and XPATH.
sounds interesting...
what kind of game is it? is it multiplayer? if it is, you might want to make a servlet (since you are generating all this HTML anyway), and then the players can play each other right through their browser...
--- krez (krezisback@aol.com)
what kind of game is it? is it multiplayer? if it is, you might want to make a servlet (since you are generating all this HTML anyway), and then the players can play each other right through their browser...
--- krez (krezisback@aol.com)
I actually have an html parseing class written an it works, it only uses bold and Italics tags. And an image tag I I had defined especially for the program. If you would like to take a look at it I can send you the source.. Right now I have it all packed in Jars but I think I just emailed my self the source. If you are interested then reply. Maybe we can work soemthing out. I might have to modifiy it a bit since it is actually code that we are using here at work.. I didn''t sign a non compete agreement. But I still think it is bad policy to give out source code I was paid to write. Anyway drop me a line if you are interested.. BTW.. It was written for an applet, so it should be compatable for all versoins of the JDK.
War doesn''t determine who is right, war determines who is left.
War doesn''t determine who is right, war determines who is left.
Krez : No, it is not multiplayer, it''s more a ''write your agent, then send it in the ring to compete'' kinda thing, so there''s no control once the app runs for any player.
Wrathnut : Thanks for the offer, but in the meantime I''ve already started writing a class myself. I''ll mainly use tables to format everything so I think I''ll manage, but I appreciate your offer as well as the helpful remarks of the other repliers.
******************************
Stefan Baert
On the day we create intelligence and consciousness, mankind becomes God.
On the day we create intelligence and consciousness, mankind becomes obsolete...
******************************
Wrathnut : Thanks for the offer, but in the meantime I''ve already started writing a class myself. I''ll mainly use tables to format everything so I think I''ll manage, but I appreciate your offer as well as the helpful remarks of the other repliers.
******************************
Stefan Baert
On the day we create intelligence and consciousness, mankind becomes God.
On the day we create intelligence and consciousness, mankind becomes obsolete...
******************************
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement