Results 1 to 13 of 13
  1. #1
    tsb
    tsb is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default 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.

    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>
    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.
    Last edited by Fubarable; 05-28-2011 at 12:27 AM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    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

  3. #3
    tsb
    tsb is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    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)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    It works for me.
    What else is going on?
    What OS, browser and version of JSK?

  5. #5
    tsb
    tsb is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    did you add the archive file of the first applet to the html file?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    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>

  7. #7
    tsb
    tsb is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    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>

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    What happens with both applets in the jar file?

  9. #9
    tsb
    tsb is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    i get the same error

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    It worked with both the class files in the jar file with this HTML:
    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>
    There were NO .class files in the folder with the html file and the jar file.

  11. #11
    tsb
    tsb is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    thanks for that it worked. appreciated

  12. #12
    satya169 is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default

    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.......

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Java Applet to Applet communication
    By jsman in forum Java Applets
    Replies: 3
    Last Post: 04-05-2011, 02:23 PM
  2. Replies: 2
    Last Post: 09-29-2009, 03:33 PM
  3. applet servlet communication
    By viswamadhavi in forum Java Applets
    Replies: 0
    Last Post: 04-06-2009, 01:12 PM
  4. applet servlet communication
    By hardc0d3r in forum Java Applets
    Replies: 1
    Last Post: 07-12-2007, 06:58 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •