Results 1 to 13 of 13
- 05-27-2011, 10:31 PM #1
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
applet to applet communication: classcastexception
Hi,
I get the class cast exception when I attempt to do inter applet communication. Please see the attached code.
Now the above code runs fine but if I add a jar file for applet1 containing the class file applet1_0022.class it gives a classcastexception.Could someone help me with this.Java Code:Applet1_0022 import java.awt.*; public class Applet1_0022 extends java.applet.Applet { TextField inputText; Button b; public void init() { setLayout(new FlowLayout()); inputText = new TextField( "", 15 ); b = new Button("Send to Applet 2"); add(inputText); add(b); } // JDK 1.02 style evant handling public boolean action(Event ev, Object arg) { if (ev.target instanceof Button) { String textMsg = inputText.getText().trim(); Applet2_0022 applet2 = (Applet2_0022)getAppletContext().getApplet("applet2"); if ( applet2 != null ) { applet2.append( textMsg ); return true; } else { System.out.println("Applet2 not found?"); return false; } } return false; } } Applet2_0022 import java.awt.*; public class Applet2_0022 extends java.applet.Applet { TextArea textBox; public void init() { setLayout(new FlowLayout()); textBox = new TextArea( 5, 40 ); add( textBox ); } public void append( String msg ) { textBox.append( msg ); textBox.append( "\n" ); } } the associated html file.: <HTML><HEAD></HEAD><BODY> <APPLET CODE="Applet1_0022.class" NAME="applet1" HEIGHT=200 WIDTH=150> </APPLET> <APPLET CODE="Applet2_0022.class" NAME="applet2" HEIGHT=200 WIDTH=400> </APPLET> </BODY></HEAD>Last edited by Fubarable; 05-28-2011 at 12:27 AM. Reason: code tags added
- 05-28-2011, 02:11 PM #2
Please copy and paste here the full text of the error message.
Also add some printlns to show the values of variables that are being cast
- 05-31-2011, 08:01 PM #3
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
this is the error message i get when i click the button to send a message from one applet to the other applet text box. :
Exception in thread "AWT-EventQueue-2" java.lang.ClassCastException: Applet2_0022 cannot be cast to Applet2_0022
at Applet1_0022.action(Applet1_0022.java:18)
at java.awt.Component.handleEvent(Unknown Source)
at java.awt.Component.postEvent(Unknown Source)
at java.awt.Component.postEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 05-31-2011, 08:27 PM #4
It works for me.
What else is going on?
What OS, browser and version of JSK?
- 05-31-2011, 08:34 PM #5
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
did you add the archive file of the first applet to the html file?
- 05-31-2011, 09:03 PM #6
Here is the HTML I used:
PHP Code:<HTML><HEAD></HEAD><BODY> <APPLET CODE="Applet1_0022" NAME="applet1" HEIGHT=200 WIDTH=150> </APPLET> <APPLET CODE="Applet2_0022" NAME="applet2" HEIGHT=200 WIDTH=400> </APPLET> </BODY></HEAD>
- 05-31-2011, 09:06 PM #7
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
i am also able to run it error free if I do not have the first applet in a jar file. I need this because I have to scale up the code and the first applet contains lot of classes which would be in a jar file. That is when the error/classcastexception occurs.
try with this HTML:
<HTML><HEAD></HEAD><BODY>
<APPLET CODE="Applet1_0022"
ARCHIVE="applet.jar"
NAME="applet1"
HEIGHT=200 WIDTH=150>
</APPLET>
<APPLET CODE="Applet2_0022"
NAME="applet2"
HEIGHT=200 WIDTH=400>
</APPLET>
</BODY></HEAD>
- 05-31-2011, 09:16 PM #8
What happens with both applets in the jar file?
- 05-31-2011, 09:18 PM #9
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
i get the same error
- 05-31-2011, 09:26 PM #10
It worked with both the class files in the jar file with this HTML:
There were NO .class files in the folder with the html file and the jar file.PHP Code:<HTML><HEAD></HEAD><BODY> <APPLET CODE="Applet1_0022" archive=TestApplets.jar NAME="applet1" HEIGHT=200 WIDTH=150> </APPLET> <APPLET CODE="Applet2_0022" archive=TestApplets.jar NAME="applet2" HEIGHT=200 WIDTH=400> </APPLET> </BODY></HEAD>
- 06-03-2011, 12:03 AM #11
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
thanks for that it worked. appreciated
- 06-18-2011, 04:35 PM #12
Member
- Join Date
- Jun 2011
- Posts
- 1
- Rep Power
- 0
sir....how can i connect jframe with oracle database.......
my code is like this....
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MasterFrame extends JFrame {
final static String[] labelStr = {"Just a test", "Dummy label", "to fill", "the form"};
// this the master frame... we just fill it with labels an button to show how it works
MasterFrame() {
super("MasterFrame");
// build a dummy panel with dummy labels just to fill the GUI
JPanel panel = new JPanel(new GridLayout(labelStr.length, 2));
for(int i = 0; i < labelStr.length; i++) {
panel.add(new JLabel(labelStr[i]));
panel.add(new JButton("Button " + i));
}
add(panel);
setSize(400, 200);
// create and show the Login form
new Login(this);
}
// to test the whole thing
public static void main(String[] args) {
new MasterFrame();
}
class Login extends JFrame implements ActionListener
{
final JButton submitBtn;
final JPanel panel;
final JLabel usernameLabel, passwordLabel;
final JTextField usernameField;
final JPasswordField passwordField;
JFrame father;
Login(JFrame masterForm)
{
super("Login form");
father = masterForm;
usernameLabel = new JLabel("Username:");
usernameField = new JTextField(15);
passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField(15);
submitBtn =new JButton("LOGIN");
submitBtn.addActionListener(this);
panel = new JPanel(new GridLayout(4,6));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(submitBtn);
// to fill the GridLayout
panel.add(new JLabel(""));
add(panel,BorderLayout.CENTER);
setTitle("LOGIN FORM");
setSize(300,100);
setVisible(true);
}
public void actionPerformed(ActionEvent ee)
{
// validate here the username from usernameField.getText()
// and the password form passwordField.getPassword();
// ususally that would be done from a database but here
// (for testing purpose) we will hardcode the value
// username must be "abc" password "xyz"
if(usernameField.getText().equals("abc")) {
// getPassword returns an array of char[] we have to make it a String
if(new String(passwordField.getPassword()).equals("xyz")) {
this.setVisible(false); // hide myself
father.setVisible(true); // show my father
this.dispose(); // clean my resource
return;
}
}
// send an option pane that does not match
JOptionPane.showMessageDialog(this, "Sorry wrong Username Password\n Try again");
}
}
}
please help......m in a trouble.......
-
Satya: Please see the private message I have sent to you. I am locking this thread now.
Similar Threads
-
Java Applet to Applet communication
By jsman in forum Java AppletsReplies: 3Last Post: 04-05-2011, 02:23 PM -
Pass parameter from one applet to another applet in different webpages...
By anubhavranjan in forum Java AppletsReplies: 2Last Post: 09-29-2009, 03:33 PM -
applet servlet communication
By viswamadhavi in forum Java AppletsReplies: 0Last Post: 04-06-2009, 01:12 PM -
applet servlet communication
By hardc0d3r in forum Java AppletsReplies: 1Last Post: 07-12-2007, 06:58 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks

Bookmarks