displaying image on the button
hi there,
well i wanna display image on my applet button from the relative path how do i do.
here is my code which i tried gave me nullpointer exception and i did put this code my applets constructor:
URL url1 = new URL(getDocumentBase().toString()+"b.gif");
ImageIcon image1 = new ImageIcon(url1);
JButton button = new JButton(image1);
[380 byte] By [
ppdhans] at [2007-9-26 3:36:02]

Hi...
you shouldn't use .toString()... and I think getCodeBase() is better to use... it loads the images from the same directory as the applet is loaded from... and if you should use a JAR file... (which I would highly recommend!!) you should use getResource() to access the images in the JAR file...
example 1: for a simple applet with getCodeBase()
ImageIcon im1 = new ImageIcon(getImage(getCodeBase(),"b.gif"));
JButton but = new JButton(im1);
example 2: for a JAR file
URL url = this.getClass().getResource("b.gif");
JButton but = new JButton(getImage(url));
well I hope this helps!
Sander
SanderSander
the code u have given failed to work well here is the code which i tried :
ImageIcon image1 = new ImageIcon(getImage(getCodeBase(),"b.gif"));
JButton button = new JButton(image1);
and here is the html code for applet :
<!--"CONVERTED_APPLET"-->
<!-- CONVERTER VERSION 1.3 -->
<SCRIPT LANGUAGE="JavaScript"><!--
var _info = navigator.userAgent; var _ns = false;
var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>
<COMMENT><SCRIPT LANGUAGE="JavaScript1.1"><!--
var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3.5") < 0) || (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0)));
//--></SCRIPT></COMMENT>
<SCRIPT LANGUAGE="JavaScript"><!--
if (_ie == true) document.writeln('<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = "500" HEIGHT = "300" codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0"><NOEMBED><XMP>');
else if (_ns == true) document.writeln('<EMBED type="application/x-java-applet;version=1.3" CODEBASE="http://192.168.1.50/paddy/ns/ns" CODE = "GPTextEditor" MAYSCRIPT="true" WIDTH = "500" HEIGHT = "300" scriptable=false pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"><NOEMBED><XMP>');
//--></SCRIPT>
<APPLET CODE = "GPTextEditor" WIDTH = "500" HEIGHT = "300"></XMP>
<PARAM NAME = CODE VALUE = "GPTextEditor" >
<PARAM NAME = CODEBASE VALUE = "http://192.168.1.50/paddy/ns/ns" >
<PARAM NAME=MAYSCRIPT VALUE=TRUE>
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
<PARAM NAME="scriptable" VALUE="true">
</APPLET>
</NOEMBED></EMBED></OBJECT>
i m using swing applet.....