Hypertext in java

M

Marte Brenne

Hi!

Can anyone please tell me if it's possible to use hypertext in java? I
have designed a user interface, and I want something to happen when
you click on some Strings. Because it's a lot of text that I want to
make clickable, I can't use buttons.

Thank you very much in advance!
 
A

Andrew Thompson

Can anyone please tell me if it's possible to use hypertext in java?

As in HTML? Yes. Swing components accept it.
I
have designed a user interface, and I want something to happen when
you click on some Strings. Because it's a lot of text that I want to
make clickable,

Over 10 Meg eh?!?
What happened when you tried it?
...I can't use buttons.

A JButton will accept HTML, lots of it.
but that will be a BIG button. ;-)
 
P

Pimousse

Can anyone please tell me if it's possible to use hypertext in java? I
have designed a user interface, and I want something to happen when
you click on some Strings. Because it's a lot of text that I want to
make clickable, I can't use buttons.

use HTML hyperlinks
create a JEditorPane myEditorPane

do
myEditorPane.setEditorKit(new HTMLEditorKit());

then
myEditorPane.addHyperLinkListener(this);

after you can put your html text in :
myEditorPane.setText(myTextInHTML);

you'll have to implement the following method :
public void hyperlinkUpdate(HyperlinkEvent e){

// you'll define here the behaviour when clicking an hyperlink
}

@++
Pimousse
 
M

Marte Brenne

Andrew Thompson said:
As in HTML? Yes. Swing components accept it.


Over 10 Meg eh?!?
What happened when you tried it?


A JButton will accept HTML, lots of it.
but that will be a BIG button. ;-)

Thank you very much for your answer! I probably didn't express myself
clearly. I don't want to use HTML, I just want to make some String's
clickable. I have a paragraph with words - some individual words and
some groups of words (separated with commas). Depending on which
word/group of words that is clicked, something is going to happen
(e.g. a JTree with some nodes are going to get new nodes).

If you (or someone else) know if this is possible to do, I will
appreciate it a lot if you let me know. Thank you very much in advance
:)

PS! If it makes a difference, my program is yet an application, and no
applet, but I will perhaps change it into an applet.
 
R

Rogan Dawes

Marte said:
Thank you very much for your answer! I probably didn't express myself
clearly. I don't want to use HTML, I just want to make some String's
clickable. I have a paragraph with words - some individual words and
some groups of words (separated with commas). Depending on which
word/group of words that is clicked, something is going to happen
(e.g. a JTree with some nodes are going to get new nodes).

If you (or someone else) know if this is possible to do, I will
appreciate it a lot if you let me know. Thank you very much in advance
:)

PS! If it makes a difference, my program is yet an application, and no
applet, but I will perhaps change it into an applet.

JEditorPane supports HTML, and you can register a Listener that will be
notified when you click on a link. You just need to make the JEditorPane
readonly, otherwise a click on a link will be interpreted as trying to
edit the text of the link.

Hope this helps.

Regards,

Rogan
 
R

Roedy Green

Thank you very much for your answer! I probably didn't express myself
clearly. I don't want to use HTML, I just want to make some String's
clickable. I have a paragraph with words - some individual words and
some groups of words (separated with commas). Depending on which
word/group of words that is clicked, something is going to happen
(e.g. a JTree with some nodes are going to get new nodes).

Just how complicated is this html? If in is very simple you could
convert it yourself to a list of tokens. with x,y, font, colour and
text, and delegate what to do if user clicks this.

Then you do render this yourself in your mini browser in a Canvas or
Jpanel with custom paint.

You field the Mouse click event, and figure out which thing he
clicked, and call the appropriate method.

For a huge document, you need to scroll, and avoid rendering outside
theclipregion.

You could also use a hanging moss algorithm to figure out which
element was clicked rather than by comparing against every possibility
linearly.

see http://mindprod.com/jgloss/canvas.html
see http://mindprod.com/jgloss/paint.html
See http://mindprod.com/jgloss/hangingmoss.html
 
R

Roedy Green

Then you do render this yourself in your mini browser in a Canvas or
Jpanel with custom paint.

Don't consider this unless the other solution letting Swing render for
you and detect the link clicks proves hopelessly slow. You just need
to set up some dummy links in your html for the magic words.


The other solution will be orders of magnitude easier to implement
than mine.
 
A

Andrew Thompson

....errm. OoooK.

The thing is, the easiest way to
'make Strings clickable' is to
...write them as HTML and add an
HyperLinkListener.

Once something is clicked, the program
can do the appropriate actions.

There _are_ other ways to do it,
but I suspect that is the easiest.
 
A

Andrew Thompson

On 26 Apr 2004 01:10:18 -0700, Marte Brenne wrote:

[something screwy was happening with my feed
to usenet so that I received Roedy's response
to you before your post, so I only just saw
this bit..]
PS! If it makes a difference, my program is yet an application, and no
applet, but I will perhaps change it into an applet.

No. I was basing that purely on
'Swing does easy (quick'n'dirty) HTML support'.

It sounds perfect for your application..

...put your text inside a JEDitorPane in
HTML format, use a style sheet to remove the
link underlines as well if you like.

Connect am HyperLinkListener to the JEP
and do whatever you like with the resultant
events detected.

Applet or App., does not matter.

See how easily you can get a
'functional' browser in this example..
<http://java.sun.com/developer/onlineTraining/GUI/Swing1/shortcourse.html#JFCEditorPane>

So, that's..
<http://www.physci.org/api.jsp?class=javax.swing.JEditorPane>
<http://www.physci.org/api.jsp?class=javax.swing.event.HyperlinkListener>
You need to look at..

HTH
 
R

Roedy Green

[something screwy was happening with my feed
to usenet so that I received Roedy's response
to you before your post, so I only just saw
this bit..]

This happens if one of the posters has a out of whack clock. Posts get
tagged with the timestamp of the poster's clock, not of the newserver.
I find this irritating since some people deliberately future stamp
posts to make them stick at the top of the display. One goofball with
an out of whack clock can confuse everyone.

See http://mindprod.com/jgloss/atomiclock.html
 
A

Andrew Thompson

[something screwy was happening with my feed
to usenet so that I received Roedy's response
to you before your post, so I only just saw
this bit..]

This happens if one of the posters has a out of whack clock.

No, this was happening across multiple
groups, my feed stopped for over 18 hours,
then they began to trickle in, in bizarre
order (and again across multiple groups,
with no single connection but me).

(ehh.. shrugs) It seems to be sorting itself out.
 
Y

Yan Lijun

in awt,the base "component" class support the "MouseListener",and it means
nearly all components can receive mouse click events.

for instance
public class MouseClickListener extends MouseAdapter{
public void mouseClick(MouseEvent evnet){
......
}
}

Label label=new Label("text");
label.addMouseListener(new MouseClickListener());

so,when you click the label "text",method "mouseClick" will be invoked

ÔÚ 23 Apr 2004 02:18:43 -0700 ʱ, (e-mail address removed) (Marte Brenne) дÁË:
--
Hi!

Can anyone please tell me if it's possible to use hypertext in java? I
have designed a user interface, and I want something to happen when
you click on some Strings. Because it's a lot of text that I want to
make clickable, I can't use buttons.

Thank you very much in advance!

(e-mail address removed)
/**
/* Java Is Not Platform-independent.It Is The Platform!
*/
 
M

Marte Brenne

Thank you very much for all the answers I have received! And
especially for the coding example :) If someone with the same problem
is reading this, what I forgot to do was when setting text to the
JEditorPane to include <A HREF> before and </A HREF> after the text to
be clicked - like this:

pane.setText("<A HREF>text to be clicked</A HREF>");

Again - thank you very much for all your help!
 
A

Andrew Thompson

pane.setText("<A HREF>text to be clicked</A HREF>");

pane.setText("<A HREF>text to be clicked</A>");

Is actually slightly more valid, while..

pane.setText("<A HREF='somebloodywhere.html'>text to be clicked<A>");

Is even more so.. (shrugs) not sure about these
closing '/' ---> </A> it looks like something
from XML.. it is not needed for normal HTML..

[ Waits for Chris S. to jump in and describe
some arcane situation where it matters ;-) ]
 
C

Christophe Vanfleteren

Andrew said:
pane.setText("<A HREF>text to be clicked</A HREF>");

pane.setText("<A HREF>text to be clicked</A>");

Is actually slightly more valid, while..

pane.setText("<A HREF='somebloodywhere.html'>text to be clicked<A>");

Is even more so.. (shrugs) not sure about these
closing '/' ---> </A> it looks like something
from XML.. it is not needed for normal HTML..

[ Waits for Chris S. to jump in and describe
some arcane situation where it matters ;-) ]

AFAIK it was always supposed to be <A>...</A>, as with the <P>, the <B>, ...
tags.

Even in HTML you were supposed to close the tags properly, but
unfortunately, a lot of HTML "programmers" got sloppy, and the browsers
were always as liberal as possible in parsing the HTML on a page, so it
didn't get noticed a lot.
 
A

Andrew Thompson

....
AFAIK it was always supposed to be <A>...</A>, as with the <P>, the <B>, ...
tags.

Even in HTML you were supposed to close the tags properly,

(slaps forehead) I don't know _where_
my head was at. But (ooOOuch!), I
seem to have found it now..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top