You are not logged in.

sedrok

Beginner

  • "sedrok" started this thread

Posts: 9

Date of registration: Apr 26th 2009

  • Send private message

1

Monday, February 14th 2011, 3:36pm

how call javascript from applet

how call javascript from applet



original java code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.applet.*; 
import java.net.*; 

public class InJava4 extends Applet{ 
public void init(){ 
String msg = "Hello from Java (using javascript alert)"; 
try { 
getAppletContext().showDocument (new URL("javascript:doAlert("" + msg +"")")); 
} 
catch (MalformedURLException me) { 
} 
} 
}






jabaco

Source code

1
Applet1.Parent.getAppletContext().showDocument(New java#net#URL("javascript:DOalert("" + msg +"")") )






html:



Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title> 
Jabaco-Applet 
</title> 


<SCRIPT> 

function DOalert(s) { 
alert(s); 
} 

</SCRIPT> 


</head> 
<body> 
<APPLET ARCHIVE="test5.jar" CODE="Applet1.class" MAYSCRIPT WIDTH="400" HEIGHT="300"> 
<PARAM NAME="Compiler" VALUE="Jabaco"> 
</APPLET> 
</body> 
</html>






it gives Nullpointer exeption error



Why?

sedrok

Beginner

  • "sedrok" started this thread

Posts: 9

Date of registration: Apr 26th 2009

  • Send private message

2

Friday, February 18th 2011, 11:25am

dose anybody know why?

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

3

Friday, February 18th 2011, 3:23pm

To find the error, you should add extra debugging statements.
It is not fully clear to me what you intend to achieve with your Java code. Is that a working original sample you intend to port to Jabaco?

A null pointer exception usually happens if either a variable is not properly initialized or an object creation failed for some reason.
To pin-down what has happened, you should trace the code execution and narrow down the area of failure.
Add code to your catch() part to actually get more information.

You are attempting four or more nested operations in a single line of code.
To isolate the error, you could spread this out to several lines and check for success at every step.

Applets on their own are difficult to trace.
It might be a good idea to try parts of your code in a stand-alone Jabaco module.

This test code should give some valuable hints:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Option Explicit

Public Sub Command1_Click()
   Dim s As String
   Dim msg As String = "Hello!"
   Dim url As java#net#URL
   
   '  Chr(34) is a trick to state quote characters in strings
   s = "javascript:DOalert(" & Chr(34) + msg + Chr(34) & ")"
   
   Debug.Print s
   
   url = New java#net#URL(s) 
   
   Debug.Print url.toString
End Sub


Look here for additional help.

Please post your findings

Greetings

A1880

This post has been edited 3 times, last edit by "A1880" (Feb 18th 2011, 3:44pm)


A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

4

Saturday, February 19th 2011, 6:10pm

The following applet shows how to pass information between applet and javascript in both ways.

The applet Jabaco source:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Option Explicit

'  the applet has a Button "cmdSetLocal"
Public Sub cmdSetLocal_Click()
   '  copy text of textbox "tbLocalInput"
   SetText Me.tbLocalInput.Text 
End Sub

' the applet has a button "cmdSetRemote"
Public Sub cmdSetRemote_Click()    
   Dim s As String
   Dim msg As String
   Dim url As java#net#URL

   On Error Goto ErrHandler
   
   '  take text of "tbLocalInput"
   msg = Me.tbLocalInput.Text 
   
   '  Chr(34) is a trick to state quote characters in strings
   s = "javascript:SetText(" & Chr(34) + msg + Chr(34) & ")"
      
   url = New java#net#URL(s)    
   
   Me.Parent.getAppletContext().showDocument(url)
   
   Exit Sub
   
ErrHandler:
   trace "Error: " & Err.getMessage()
End Sub

'  sub called from javscript
Public Sub SetText(txt As String)
   Me.lblText.Caption = txt
End Sub

Private Sub trace(s As String)
   Me.lblText.Caption = Me.lblText.Caption & vbCrLf & s
End Sub


The HTML page with the embedded applet:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>
			Jabaco-Applet
		</title>
		<SCRIPT LANGUAGE="JavaScript">
			function button_clicked (form) {
				var TestVar = document.myform.inputbox.value;
				
				document.jsalert.SetText(TestVar);
			}
			
			function SetText(s) {
				document.myform.inputbox.value = s;
			}
		</SCRIPT>
	</head>
	<body>
		<APPLET NAME="jsalert" ARCHIVE="jsalert.jar" CODE="Applet1.class" MAYSCRIPT WIDTH="400" HEIGHT="300">
			<PARAM NAME="Compiler" VALUE="Jabaco">
		</APPLET>
		<FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
			<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
			<INPUT TYPE="button" NAME="button" Value="Click" onClick="button_clicked(this.form)">
		</FORM>
	</body>
</html>


I've used Firefox 3.6 and Java 1.6.0_24

Greetings

A1880

sedrok

Beginner

  • "sedrok" started this thread

Posts: 9

Date of registration: Apr 26th 2009

  • Send private message

5

Monday, February 21st 2011, 9:15am

cool it is working

sedrok

Beginner

  • "sedrok" started this thread

Posts: 9

Date of registration: Apr 26th 2009

  • Send private message

6

Monday, February 21st 2011, 9:28am

thank you

Rate this thread
WoltLab Burning Board