yatinbc
yatinbc

Reputation: 625

Embed java swing in html

I have following code for my java swing application which is executing fine in eclipse IDE but when I am embedding it in HTML then not executing in browser,just showing blank box.

Java swing code:

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

public class Form extends JApplet{

public void init()
{
JFrame frame = new JFrame("Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();

JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
//JButton button1 = new JButton("OK");
//JButton button2 = new JButton("Cancel");
Container c;
c=frame.getContentPane();
c.setLayout(null);
JLabel name=new JLabel("Name :");
JLabel compcode=new JLabel("Company Code :");
JLabel cardno=new JLabel("Card Number: ");
JLabel cardtype=new JLabel("Card Type :");
JLabel pin=new JLabel("Pin :");
JLabel bldgrp=new JLabel("Blood Group :");
JLabel empcode=new JLabel("Employee Code :");
JLabel dob=new JLabel("DOB :");
JLabel valupto=new JLabel("Valid Upto :");
JLabel jdate=new JLabel("Joining Date :");
JLabel dept=new JLabel("Department :");
JLabel uid=new JLabel("UID :");

JTextField nametxt=new JTextField(10);
JComboBox compcodetxt=new JComboBox();
JTextField cardnumtxt=new JTextField(10);
JTextField cardtypetxt=new JTextField(10);
JTextField pintxt=new JTextField(10);
JComboBox bldgrptxt=new JComboBox();
JTextField empcodetxt=new JTextField(10);
JTextField dobtxt=new JTextField(10);
JTextField valuptotxt=new JTextField(10);
JTextField jdatetxt=new JTextField(10);
JTextField depttxt=new JTextField(10);
JTextField uidtxt=new JTextField(10);



name.setBounds(10, 10, 100, 25);
nametxt.setBounds(110, 10, 100, 25);
compcode.setBounds(10, 40, 100, 25);
compcodetxt.setBounds(110, 40, 100, 25);
cardno.setBounds(10, 70, 100, 25);
cardnumtxt.setBounds(110, 70, 100, 25);
pin.setBounds(10, 110, 100, 25);
pintxt.setBounds(110, 110, 100, 25);
bldgrp.setBounds(10, 140, 100, 25);
bldgrptxt.setBounds(110, 140, 100, 25);
empcode.setBounds(10, 170, 100, 25);
empcodetxt.setBounds(110, 170, 100, 25);
dob.setBounds(10, 200, 100, 25);
dobtxt.setBounds(110, 200, 100, 25);
valupto.setBounds(10, 230, 100, 25);
valuptotxt.setBounds(110, 230, 100, 25);
jdate.setBounds(10, 260, 100, 25);
jdatetxt.setBounds(110, 260, 100, 25);
dept.setBounds(10, 290, 100, 25);
depttxt.setBounds(110, 290, 100, 25);
uid.setBounds(10, 320, 100, 25);
uidtxt.setBounds(110, 320, 100, 25);

//button1.setBounds(10, 50, 75, 25);
//button2.setBounds(10, 70, 75, 25);

c.add(name); c.add(nametxt);
c.add(compcode); c.add(compcodetxt);
c.add(cardno); c.add(cardnumtxt);
c.add(pin); c.add(pintxt);
c.add(bldgrp); c.add(bldgrptxt);
c.add(empcode); c.add(empcodetxt);
c.add(dob); c.add(dobtxt);
c.add(valupto); c.add(valuptotxt);
c.add(jdate); c.add(jdatetxt);
c.add(dept); c.add(depttxt);
c.add(uid); c.add(uidtxt);


//panel.add(button1);
//panel.add(button2);
//frame.add(panel);
frame.setSize(350,400);
//frame.pack();
frame.setVisible(true);
  }
}

HTML code for embedding it in is as follows:

<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

    <applet Archive ="Form.jar" Code="com.vms.util.Form" WIDTH="250" HEIGHT="300" >
    </applet>


</html>

I generated JAR for my swing class Form.jar with package com.vms.util I kept it in my D: drive form directory and put all html and jar in form directory.

I am able to run my swing application in HTML, how do I run it?

When I am running above code using appletviewer I am getting following error

D:\form>appletviewer Form2.html
java.security.AccessControlException: access denied (java.lang.RuntimePermission
 exitVM.0)
        at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:374)
        at java.security.AccessController.checkPermission(AccessController.java:
546)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
        at java.lang.SecurityManager.checkExit(SecurityManager.java:744)
        at javax.swing.JFrame.setDefaultCloseOperation(JFrame.java:372)
        at Form.init(Form.java:10)
        at sun.applet.AppletPanel.run(AppletPanel.java:424)
        at java.lang.Thread.run(Thread.java:662)

Upvotes: 1

Views: 6927

Answers (2)

Sagar
Sagar

Reputation: 1335

comment following line

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Applets are not allowed (except by modifying directly the Java Security Policy on the client machine) to perform some critical calls. Even if using signed applets.

In your case, at javax.swing.JFrame.setDefaultCloseOperation is triggering the exception

Upvotes: 3

Sagar
Sagar

Reputation: 1335

it seems to be a small mistake, just enable java console and run the application.

it will help you to know what is the issue

PS: to enable java console on windows

  • go to control panel
  • select java
  • go to advance tab
  • Select applet lifecycle exception under Debugging section
  • Select Show console under Java Console Section.

Hope this will help you

Upvotes: 1

Related Questions