1

I need help with calling methods from within a class of a Java or a Java applet .jar file locally stored. The idea behind using HTML as interface is that I want to make a quick good looking GUI using jQuery and link it to other classes for my application.

A basic java applet:


package appletHW;

import java.applet.Applet;
import javax.swing.*;

public class HWapplet extends Applet {

    static JFrame frame;
    /**
     * @param args
     */
      public void init() {

        System.out.println("Hello world!");

        JOptionPane.showMessageDialog(frame,
                "Eggs are not supposed to be green.");

        }
}

I hope nothing's wrong with the above code. I am new to all of this! :) I created a jar file of the above class named as aHW.jar

A HTML page for my interface:


<head>
<title>INTERFACE</title>
</head>

<body>

<object     
classid="java:appletHW.HWapplet.class"     
type="application/x-java-applet"     
archive="aHW.jar"> 

</object>
<!--not sure of the above code... -->
&nbsp;<form action="">
<input type="button" value="a" onclick="init()">
</BR>
</BR>
</BR>
<input type="button" value="b">
</BR>
</BR>
</BR>
<input type="button" value="c">
</form>
</body>

Could someone please help me with the above sample codes? I am new to all this and I would really appreciate some hand holding guidance on accomplishing the same.

Regards, Archit

6
  • @userNNNN "Could someone please help me with the above sample codes?" Quite possibly. What is your specific question? Some notes while I am here. 1) Don't see the advantage of the HTML interface. Use Java Web Start to launch a JFrame directly off the site & you will save lots of trouble. 2) If continuing with the applet, the JFrame is not necessary. a) Without setting the frame location, you might as well hand null as the parent of the JOptionPane. b) The applet itself could be used as the parent component. Commented Mar 27, 2011 at 7:54
  • @userNNNN 3) The HTML is invalid. I recommend during testing you stick to HTML 3.2 (or 4.01 Transitional) & use the applet element instead of object. Validate that HTML. For deployment, write the applet(/object/embed) element using deployJava.js. Most programmers have no respect for HTML and presume that 'any old crap' will work. Don't presume that, validate it then test it in a number of browsers. Commented Mar 27, 2011 at 8:04
  • Hi Andrew! Thank you for replying so quickly! The idea behind using HTML as interface is that I want to make a quik good looking GUI using jQuery and link it to other classes for my application. I want to be able to click on an HTML button and launch certain methods from my java based application. So the java code is basically the logic and core application running behind the HTML interface. However, I dont really have much of an idea on how to do this! :( Could you please guide me on this? THe above code is just something I put together as a trial to test if I can accomplish this... Commented Mar 27, 2011 at 9:06
  • @Archit 1) IF the link to Real's site is no good for you, I don't know what could help you. 2) It would be advantageous to ask more specific questions than "Can you help me?". Commented Mar 28, 2011 at 23:51
  • Im still figuring out how I can use a local jar file based on the Real's How to site info.. Its basically the same issue. I made an applet and html file as per the link: rgagnon.com/javadetails/java-0173.html. But the html wont work as its unable to find my class. I have kept the jar file in the same directory as the html page. Also tried keeping it in the jre lib folder. But the applet doesnt load. Commented Mar 29, 2011 at 6:10

1 Answer 1

1

For tips & examples, see Java/Javascript interaction - Real's HowTo.

Sign up to request clarification or add additional context in comments.

9 Comments

Hi Andrew. If you dont mind sparing some time to help me clear my concepts, I wanted to know if there is any way to use java application classes instead of making applets to work with an HTML interface? I am making a Home automation application. As told earlier, I want to use HTML interface as its easy to make and looks cool with jquery effects. However this means I probably have to use applets only. Would you say that this is a bad Idea?
@Archit I would say it is a bad idea to use an HTML interface simply because it looks groovy. Maybe what this application really needs is a Swing free-floating GUI (e.g. JFrame) with a custom PLAF.
Please clarify/confirm my understanding... Is it that applets will not be able to access the com ports of a local m/c due to its security model?
Probably a sand-boxed applet would be not allowed (I have not tested it, but it sounds like it might be something that is restricted). A trusted applet should be able to do it. Note that the best way to launch a JFrame (if you distribute the app. from a site) is using Java Web Start. The same security environment applies to JWS launched apps. by default.
@Andrew, I got the applet to work. Its executing the required com port based commands on the modem when run from the eclipse environment. But from the browser it doesnt perform the action. I even signed the applet and tried. Is there any specific permissions I need to give to allow such operations? I really appreciate your guidance! Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.