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]

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");
}
}
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.