I had done a program with JFrame a year ago and I am trying to convert it to with JApplet so I can put it on the web. So far so good and I am able to see the applet and seems every functions are still working... there is one minor issue, when the below class "showTable" function is called, the applet shows blank until I resize the applet windows.. a tiny resize will fix it and everything will display. it just won't show without resizing the applet windows.

i tried to make the bound bigger but it isn't it.

i would really love to solve this bug. it is not critical but annoying.

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JApplet;

public class ShowTable implements ActionListener
{
	
	private DataHolder dh;
	private JScrollPane sPane;
	private JTable sTable;
	private JApplet theFrame;
	
	
	public ShowTable(JApplet frame)
	{
		theFrame = frame;
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		codeWrapper();
	}// end actionPerformed

	
	
	private void codeWrapper()
	{
		dh = new DataHolder(true);
		
		try
		{
			sTable = new JTable(dh.getRowData(), dh.getColumnNames());
			sTable.setAutoCreateRowSorter(true);
			sTable.setBackground(java.awt.Color.orange);
			dh.shutDown();
		}
	
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
		sPane = new JScrollPane(sTable);
		
		theFrame.getContentPane().removeAll();	
		theFrame.add(sPane, BorderLayout.CENTER);
		theFrame.setBounds(0, 0, 0, 600);
		theFrame.setVisible(true);
		theFrame.repaint();
		
	}
}// end class ShowTable

any suggestion would be great.. thanks

Recommended Answers

All 8 Replies

Try calling validate (and or pack) on the frame before calling setVisible (and then, you probably shouldn't have to call repaint).

yes, it was the validate()... i also had to resize the bound somehow in order to show the content. thanks very much.

now it comes to another question. how do i call the main class from html?
i googled one or two ways doing it but none worked for me so far.

can anyone please show me a good example? i found for JApplet main class isn't needed.. does it mean i can't run the program in cmd with jdk java ? .. eclipse is able to run it without the main class. does it mean i can only call JApplet program from html?

kinda confused.
my code is messy because I don't know where i am heading to... i got an init()... what about start(), stop() and destroy() ? if there is any updated/good site explain all these, please share.

import java.text.MessageFormat;
import java.sql.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.JApplet;

public class TheFrame extends JApplet
{
	private JMenuBar mb;
	private JPanel textPan;	
	private BorderLayout bl = new BorderLayout();
	private ImageIcon icon;
	Image background;
	
	public TheFrame()
	{
		
		
		this.setLayout(bl);
		mb = new MenuBar(this);
		mb.setBackground(java.awt.Color.white);
		
		
		/////////////// middle board //////////////////////

		icon = new ImageIcon("bg.jpg");
		JPanel panel = new JPanel()
		{
			protected void paintComponent(Graphics g)
			{
				g.drawImage(icon.getImage(), 0,0, null);
				super.paintComponent(g);
			}
		};
		
		panel.setOpaque(false);
		panel.setPreferredSize(new Dimension(100, 100));

		//////////// end mid board  //////////////
		
		
		////////////  the credit message ////////////////
		JLabel one = new JLabel("Software Design & Development");
		one.setAlignmentX(0.5f);
		JLabel two = new JLabel("Spring 2010");
		two.setAlignmentX(0.5f);
		
		textPan = new JPanel();
		textPan.setBackground(java.awt.Color.white);
		LayoutManager boxLayout = new BoxLayout(textPan, BoxLayout.Y_AXIS);
		
		textPan.setLayout(boxLayout);
		textPan.setSize(100,300);
		textPan.add(one);
		textPan.add(two);
		//////////// end the center message ////////////////
		
	
		///////////// The Frame setup /////////////////
		
		setJMenuBar(mb);                         
		add(panel, BorderLayout.CENTER);
		add(textPan, BorderLayout.SOUTH);     
		validate();
		setBounds(0,0, 0, 800);
		//setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		//////////////  end frame setup ////////////////
	}
	
	 public void init()
	 {
		 icon = new ImageIcon("bg.jpg");
			JPanel panel = new JPanel()
			{
				protected void paintComponent(Graphics g)
				{
					g.drawImage(icon.getImage(), 0,0, null);
					super.paintComponent(g);
				}
			};
			
			panel.setOpaque(false);
			panel.setPreferredSize(new Dimension(100, 100));
			setSize(894,537);
			panel.validate();
			
			panel.setVisible(true);
	  }

got the idea how to implement my applet to the html and execute it with the browser. basically i have two issues to solve now.

1. without applet i ran below code to specify where the mysql jar is located at

java -classpath .;.\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8-bin.jar TheFrame -classpath .\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8-bin.jar .\TheFrame

by opening it with the broswer, how do i specify the jar file? this error shows on the error msg i pasted at the end. i guess this is the biggest problem i am facing now

2. there is a security exception. it is probably related to the mysql access as well.

import java.text.MessageFormat;
import java.sql.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.JApplet;
import java.net.*;
import java.applet.*;

public class TheFrame extends JApplet
{
	private JMenuBar mb;
	private JPanel textPan;	
	private BorderLayout bl = new BorderLayout();
	private ImageIcon icon;
	Image background;
	
	public void init()
	{
			
		this.setLayout(bl);
		mb = new MenuBar(this);
		mb.setBackground(java.awt.Color.white);
		
		
		/////////////// middle board //////////////////////

		icon = new ImageIcon("bg.jpg");
		JPanel panel = new JPanel()
		{
			protected void paintComponent(Graphics g)
			{
				g.drawImage(icon.getImage(), 0,0, null);
				super.paintComponent(g);
			}
		};
		
		panel.setOpaque(false);
		panel.setPreferredSize(new Dimension(100, 100));

		//////////// end mid board  //////////////
		
		
		////////////  the credit message ////////////////
		JLabel one = new JLabel("Software Design & Development");
		one.setAlignmentX(0.5f);
		JLabel two = new JLabel("Spring 2010");
		two.setAlignmentX(0.5f);
		
		textPan = new JPanel();
		textPan.setBackground(java.awt.Color.white);
		LayoutManager boxLayout = new BoxLayout(textPan, BoxLayout.Y_AXIS);
		
		textPan.setLayout(boxLayout);
		textPan.setSize(100,300);
		textPan.add(one);
		textPan.add(two);
		//////////// end the center message ////////////////
		
	
		///////////// The Frame setup /////////////////
		
		setJMenuBar(mb);                         
		add(panel, BorderLayout.CENTER);
		add(textPan, BorderLayout.SOUTH);     
		validate();
		setBounds(0,0, 0, 800);
		//setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		//////////////  end frame setup ////////////////

		AppletContext ac = getAppletContext();
		String filename= "TheFrameApplet.htm";

		 try
		 {
			 ac.showDocument(new URL(getCodeBase() + filename));
		 }
		 
		 catch(MalformedURLException me)
		 {
			me.printStackTrace(); 
		 }


	}

error

Java Plug-in 1.6.0_18
Using JRE version 1.6.0_18-b07 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\lih
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------


Failed to load JDBC/ODBC driver.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at DataHolder.<init>(DataHolder.java:26)
	at AddRecord.<init>(AddRecord.java:14)
	at MenuBar.<init>(MenuBar.java:47)
	at TheFrame.init(TheFrame.java:25)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: C:\employee_applet\com\mysql\jdbc\Driver.class (The system cannot find the path specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(Unknown Source)
	at java.io.FileInputStream.<init>(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
	at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
	at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
	at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	... 11 more
java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.1)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkExit(Unknown Source)
	at java.lang.Runtime.exit(Unknown Source)
	at java.lang.System.exit(Unknown Source)
	at DataHolder.<init>(DataHolder.java:34)
	at AddRecord.<init>(AddRecord.java:14)
	at MenuBar.<init>(MenuBar.java:47)
	at TheFrame.init(TheFrame.java:25)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.1)

By placing the mysql jarfile in the same place as your jarfile and reference both jars in the html using the archive tag/parameter.

See http://java.sun.com/j2se/1.4.2/docs/guide/misc/applet.html for description of the Archive tag (and other applet tags).

And see http://java.sun.com/docs/books/tutorial/deployment/applet/deployingApplet.html for the current way of using applets using JNLP. And, it would probably do you good to read that entire tutorial.

i read the tutorial (it is pretty good) and tried out a simple HelloWorld applet .. black screen applet showing on browser. applied the same thing to my program and got the same black screen.

i am wondering if something trivial went wrong..

problems:
1. I can load the applet now but it is black and blank..
2. Still not sure if mysql connector driver is loaded .. couldn't google the way to specify

file structure

folder try0 contains ( index.htm , archive.jar , a.jnlp)
archive.jar contains (all the class files and sources) --> no subdir

html file

<html>
<head><title>another try</title></head>
<body>


<script src="http://www.java.com/js/deployJava.js"></script>
    <script> 
        var attributes = { code:'TheFrame',  width:800, height:800} ; 
        var parameters = {jnlp_href: 'a.jnlp'} ;
        <!-- btw, how do i add the mysql connector jar here? -->
        deployJava.runApplet(attributes, parameters, '1.6'); 
    </script>

</body>
</html>

jnlp file

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
	<information>
		<title>another try</title>
		<vendor>bob</vendor>
	</information>
	<resources>
		<!-- Application Resources -->
		<j2se version="1.6+"
		      href="index.htm />
		<jar href="archive.jar" main="true" />
	</resources>

	<applet-desc
		name="sample testing"
		main-class="TheFrame.class"
		width="800"
		height="800"
	</applet-desc>
	<update check="background"/>
</jnlp>

it looks like this is the error

Java Plug-in 1.6.0_19
Using JRE version 1.6.0_19-b04 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\lih

----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------

java.lang.NullPointerException
	at sun.plugin2.applet.JNLP2Manager.prepareToLaunch(Unknown Source)
	at sun.plugin2.applet.JNLP2Manager.initialize(Unknown Source)
	at sun.plugin2.main.client.PluginMain.initManager(Unknown Source)
	at sun.plugin2.main.client.PluginMain.access$300(Unknown Source)
	at sun.plugin2.main.client.PluginMain$2.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NullPointerException
ExitException[ 3]java.lang.NullPointerException
	at sun.plugin2.applet.JNLP2Manager.prepareToLaunch(Unknown Source)
	at sun.plugin2.applet.JNLP2Manager.initialize(Unknown Source)
	at sun.plugin2.main.client.PluginMain.initManager(Unknown Source)
	at sun.plugin2.main.client.PluginMain.access$300(Unknown Source)
	at sun.plugin2.main.client.PluginMain$2.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Error while initializing manager: ExitException[ 3]java.lang.NullPointerException, bail out

it was that i forgot to close the " " causing the black screen...

now back to the first issue.. it can't find the main class.

Java Plug-in 1.6.0_19
Using JRE version 1.6.0_19-b04 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\lih

----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------

load: class TheFrame not found.
java.lang.ClassNotFoundException: TheFrame
	at sun.plugin2.applet.Plugin2ClassLoader$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.plugin2.applet.Plugin2ClassLoader.findClassHelper(Unknown Source)
	at sun.plugin2.applet.JNLP2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: TheFrame

html file

<html>
<head><title>another try</title></head>
<body>


<script src="http://www.java.com/js/deployJava.js"></script>
    <script> 
        var attributes = { code:'TheFrame.class',  width:800, height:800} ; 
        var parameters = {jnlp_href: 'a.jnlp'} ; 
        deployJava.runApplet(attributes, parameters, '1.6'); 
    </script>

</body>
</html>

jnlp file

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
	<information>
		<title>another try</title>
		<vendor>bob</vendor>
	</information>
	<resources>
		<!-- Application Resources -->
		<j2se version="1.6.0"
		      href="index.htm" />
		<jar href="archive.jar" main="true" />
	</resources>

	<applet-desc
		name="sample testing"
		main-class="TheFrame"
		width="800"
		height="800"
	</applet-desc>
	<update check="background"/>
</jnlp>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.