Jcomponent - opening new URL for Applets? Any Clues?

Hello everybody,

I'm creating a series of Jcomponents, These components are all designed for applets.

I have a set URL method for the components,.

I want the effect that when you click the component it loads a html page in the same or a new window (depending on the setting).

I don't want to have to write this code into the applet for each component and for every applet.

Here is the code that currently works:

try{

URL url =new URL("http://www.mydomain.com/my.htm");

getAppletContext().showDocument(url,"_self");

}catch (MalformedURLException mue){

mue.printStackTrace();

}

Now this works fine if I insert the code into the "mouse_released" method of the jcomponent in the applet itself.

It doesn't work if I have this in the "mouse_released" method of the jcomponent.

I understand that as a seperate class, the Jcomponent doesn't know if it belongs to an applet or not. It's the "getAppletContext()" that fails.

Does anybody have a solution to this issue?

Sorry if I should have posted this in the bean forum, but it is applet related.

[1404 byte] By [3Pca] at [2007-10-3 4:49:38]
# 1

If you add your JComponents to an Applet then you should find the Applet in the parent-child chain of components:

void jLabel_mouseClicked(MouseEvent e) {

Container parent = jLabel.getParent();

while (parent != null && !(parent instanceof Applet)) {

parent = parent.getParent();

}

if (parent != null && parent instanceof Applet) {

Applet applet = (Applet) parent;

applet.getAppletContext().showDocument(url, "_blank");

}

}

attilaracza at 2007-7-14 22:54:11 > top of Java-index,Desktop,Core GUI APIs...
# 2
Cheers, That worked a treat.
3Pca at 2007-7-14 22:54:11 > top of Java-index,Desktop,Core GUI APIs...
# 3

These components are all designed for applets.

That seems a very odd thing. Components are generally designed to appear in a user interface, whether that interface appears in an applet or an application.

Chances are you'd be better off implementing Actions than components. Then you don't have your functionality baked into a particular widget - so you could invoke it from menus, toolbars, buttons, keyboard shortcuts, mouse events, code, you name it - all with virtually no code. If you want a button that ivokes the action, it's a one-liner to create it passing in the action.

Placing application functionality in actual components is nearly always a bad way of going about things.

itchyscratchya at 2007-7-14 22:54:11 > top of Java-index,Desktop,Core GUI APIs...
# 4
the components are all animated Hyperlink buttons for 100% java websites. I wanted to load all the html pages with the buttons.I don't see any application for my buttons to be in menus or so forth.Have I gone about this the wrong way, could you suggest a better method.
3Pca at 2007-7-14 22:54:11 > top of Java-index,Desktop,Core GUI APIs...
# 5
If that's all you're ever going to be using them for then if it works it's fine.Just throwing in some more generic ideas :o) ...would be relevant if you were writing a larger app; if you're just writing little widgety things then it probably makes no real difference.
itchyscratchya at 2007-7-14 22:54:11 > top of Java-index,Desktop,Core GUI APIs...