kt_vidhya
September 20th, 2000, 05:13 AM
hai guys
i like to have a link feature in a java.
i mean the same link object we have in html.i want to have a text in an applet.if i click on that text it should go to another page.Dont use URL object.No listeners. it should be in the JTable
please answer this as soon as possibe.its urgent.
mjpell
September 20th, 2000, 03:00 PM
Try this class. Specifially, see the catch blocks in the DisplayURL() methods.
package dataccesstool.utilities;
import netscape.javascript.*;
import java.io.IOException;
/**
* A simple, static class to display a URL in the system browser.
*
* Under Unix, the system browser is hard-coded to be 'netscape'.
* Netscape must be in your PATH for this to work. This has been
* tested with the following platforms: AIX, HP-UX and Solaris.
*
* Under Windows, this will bring up the default browser under windows,
* usually either Netscape or Microsoft IE. The default browser is
* determined by the OS. This has been tested under Windows 95/98/NT.
*
* Examples:
*
BrowserControl.displayURL("http://www.javaworld.com")
*
BrowserControl.displayURL("file://c:\\docs\\index.html")
*
BrowserContorl.displayURL("file:///user/joe/index.html");
*
* Note - you must include the url type -- either "http://" or
* "file://".
*/
public class BrowserControl
{
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "netscape";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";
/**
* Display a the DATAccess Tool web site's home page in the browser.
*/
public static void DisplayDATaccessToolHomePage()
{
try
{
JSObject win = JSObject.getWindow(dataccesstool.gui.DATAccessToolApplet.GetSingleton());
String[] args = {""};
win.call("datHome", args);
}
catch (Throwable e) //most likely a null pointer because not running as applet
{
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL");
Logger.LogException(e);
String urlString = "http://elvis5\\MIS_www\\APPL_DEV\\Ctar\\DataAccess\\default.html";
DisplayURL(urlString);
}
}
/**
* Display a the DATAccess Tool web site's home page in the browser.
*/
public static void DisplayDATaccessToolSupportPage()
{
try
{
JSObject win = JSObject.getWindow(dataccesstool.gui.DATAccessToolApplet.GetSingleton());
String[] args = {""};
win.call("datSupport", args);
}
catch (Throwable e) //most likely a null pointer because not running as applet
{
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL");
Logger.LogException(e);
String urlString = "http://elvis5\\MIS_www\\APPL_DEV\\Ctar\\DataAccess\\support.html";
DisplayURL(urlString);
}
}
/**
* Display a the Sas Help page in the browser.
*/
public static void DisplaySasHelpPage()
{
try
{
JSObject win = JSObject.getWindow(dataccesstool.gui.DATAccessToolApplet.GetSingleton());
String[] args = {""};
win.call("sasHelp", args);
}
catch (Throwable e) //most likely a null pointer because not running as applet
{
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL");
Logger.LogException(e);
String urlString = "http://elvis5\\MIS_www\\APPL_DEV\\Ctar\\DataAccess\\support.html";
DisplayURL(urlString);
}
}
/**
* Display a file in the system browser. If you want to display a
* file, you must include the absolute path name.
*
* @param urlString the file's url (the url must start with either "http://" or "file://").
*/
public static void DisplayURL(String urlString)
{
try
{
//java.net.URL url = new java.net.URL(urlString);
//dataccesstool.gui.DATAccessToolApplet.GetSingleton().getAppletContext().showDocument(url);
//dataccesstool.gui.DATAccessToolApplet.GetSingleton().getAppletContext().showDocument(new java.net.URL("javascript:jumpTo(\""+urlString+"\")"));
JSObject win = JSObject.getWindow(dataccesstool.gui.DATAccessToolApplet.GetSingleton());
String[] args = {urlString};
win.call("jumpTo", args);
}
catch (Throwable e) //most likely a null pointer because not running as applet
{
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL");
Logger.LogException(e);
boolean windows = IsWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + urlString;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + urlString + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + urlString;
p = Runtime.getRuntime().exec(cmd);
}
}
catch (InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" + cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch (IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
}
/**
* Display a file in the system browser. If you want to display a
* file, you must include the absolute path name.
*
* @param urlString the file's url (the url must start with either "http://" or "file://").
*/
public static void DisplayURL(String urlString, java.applet.Applet anApplet)
{
try
{
JSObject win = JSObject.getWindow(anApplet);
String[] args = {urlString};
win.call("jumpTo", args);
}
catch (Exception e) //most likely a null pointer because not running as applet
{
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL");
Logger.LogException(e);
boolean windows = IsWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + urlString;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + urlString + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + urlString;
p = Runtime.getRuntime().exec(cmd);
}
}
catch (InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" + cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch (IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
}
/**
* Try to determine whether this application is running under Windows
* or some other platform by examing the "os.name" property.
*
* @return true if this application is running under a Windows OS
*/
public static boolean IsWindowsPlatform()
{
String os = System.getProperty("os.name");
if (os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}
/**
* Simple example.
*/
public static void main(String[] args)
{
DisplayURL("http://www.javaworld.com");
}
/**
* Quit the applet because of normal termination.
*/
public static void QuitApplet(String urlString, java.applet.Applet anApplet)
{
//if (anApplet == null)
//javax.swing.JOptionPane.showMessageDialog(null, "anApplet is null","BrowserControl.quitApplet()", javax.swing.JOptionPane.INFORMATION_MESSAGE);
try
{
JSObject win = JSObject.getWindow(anApplet);
//System.out.println("Win = " + win.toString());
String[] args = {urlString};
win.call("quitApplet", args);
}
catch (Exception e) //most likely a null pointer because not running as applet
{
//System.out.println("in catch: " + e.getMessage() + "\n" + e.getClass());
Logger.LogException(e);
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL from quitApplet()");
boolean windows = IsWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + urlString;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + urlString + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + urlString;
p = Runtime.getRuntime().exec(cmd);
}
}
catch (InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" + cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch (IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
}
/**
* Quit the applet because of abnormal termination.
* urlString param is actually ignored....
*/
public static void QuitAppletAbnormally(String urlString, java.applet.Applet anApplet, String errorMsg)
{
// javax.swing.JOptionPane.showMessageDialog(null, urlString,"quit abnormal url", javax.swing.JOptionPane.INFORMATION_MESSAGE);
try
{
JSObject win = JSObject.getWindow(anApplet);
String[] args = {urlString};
win.call("quitAppletAbnormally", args);
}
catch (Exception e) //most likely a null pointer because not running as applet
{
Logger.LogException(e);
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL from quitAppletAbnormally()");
boolean windows = IsWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + urlString;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + urlString + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + urlString;
p = Runtime.getRuntime().exec(cmd);
}
}
catch (InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" + cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch (IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
}
/**
* Initiates an email to the DATAccess Tool Support team. This
* is only useful if we're running as an applet.
*/
public static void SendEmailToSupportTeam(java.applet.Applet anApplet)
{
try
{
JSObject win = JSObject.getWindow(anApplet);
String[] args = {""};
win.call("sendEmailTo", args);
}
catch (Exception e) //most likely a null pointer because not running as applet
{
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL from quitApplet()");
Logger.LogException(e);
}
}
/**
* Display a page in the browser to inform user that we've stopped opening a 2nd
* instance of the Tool.
*/
public static void StopSecondInstance(String htmlPage, dataccesstool.gui.DATAccessToolApplet applet)
{
String pageString = htmlPage;
if (htmlPage == null || htmlPage.equals("") )
pageString = "StopSecondInstance.html";
try
{
JSObject win = JSObject.getWindow(dataccesstool.gui.DATAccessToolApplet.GetSingleton());
String[] args = {pageString};
win.call("jumpTo", args);
}
catch (Throwable e) //most likely a null pointer because not running as applet
{
Logger.LogTraceStatement("Unable to call Applet's showDocument() to present URL");
Logger.LogException(e);
String urlString = "http://elvis5\\MIS_www\\APPL_DEV\\Ctar\\DataAccess\\"+ pageString;
DisplayURL(urlString);
}
}
}