dcsimg
converting an application to an applet
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, June 7, 2002 07:23 AM

Hi, I have a small application which is composed of a few forms, that take user input and place it in an access database, and also retrieves information from the database. I already have put a copy of the database on the server. The problem that i'm having in is in the actual converting of the main class of the application to an applet. For some reason i'm getting this error- java.lang.ClassCastException: myApplet at sun.applet.AppletPanel.createApplet(AppletPanel.java:548) at sun.applet.AppletPanel.runLoader(AppletPanel.java:477) at sun.applet.AppletPanel.run(AppletPanel.java:290) at java.lang.Thread.run(Thread.java:536) I have included both vers   More>>

Hi,


I have a small application which is composed of a few forms, that take user input and place it in an access database, and also retrieves information from the database. I already have put a copy of the database on the server. The problem that i'm having in is in the actual converting of the main class of the application to an applet. For some reason i'm getting this error-




java.lang.ClassCastException: myApplet


at sun.applet.AppletPanel.createApplet(AppletPanel.java:548)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:477)

at sun.applet.AppletPanel.run(AppletPanel.java:290)
at java.lang.Thread.run(Thread.java:536)


I have included both versions of the code below...
the main differences are that I have moved all of the stuff in the constructor of the application to the public void init() method of the applet, added the start, stop, and destroy methods, and deleted the main method.


original application file


			

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.sql.*;
import java.util.*;
import javax.swing.text.*;


public class gui3 extends JPanel implements ActionListener, ListSelectionListener
{
private JFrame functionFrame;
public static DefaultListModel functions;
public JList functionList;
public JTextField functionName;
public JPanel functionPanel;
public static JPanel infoPanel;
public JInternalFrame functionInternalFrame;
public JScrollPane sfunctionList;
public JScrollPane sfunctionPanel;
public JLabel function;
public JButton newFunction;
public JButton delete;
public String name;
public static String staticName;
public gui3()
{
functionFrame = new JFrame("Welcome to the U_Fec function database");

functionFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

functions = new DefaultListModel();

//initializes label
function = new JLabel("Function Name");

//initializes button
newFunction = new JButton("Create New Function");
delete = new JButton ("Delete Function");

//initializes string
name = new String();
staticName = new String();

//connects to database
String dataSourceName = "db1";
String dbURL = "jdbc:odbc:" + dataSourceName;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Could not connect to driver.");}
try{
Connection con = DriverManager.getConnection(dbURL, "","");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT Function FROM function_description ORDER BY Function ASC");

//fills vector with all existing function names from database
while(result.next()){
functions.addElement(result.getString("Function"));
}
}catch (SQLException e){System.out.println(e.getMessage());}

//defines list of function names
functionList = new JList (functions);

// functionList.setEditable(true);
functionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
functionList.setSelectedIndex(0);
sfunctionList = new JScrollPane(functionList);
sfunctionList.setAlignmentX(LEFT_ALIGNMENT);

//initializes text field
functionName = new JTextField();
functionName.setAlignmentX(LEFT_ALIGNMENT);
functionName.addActionListener(this);
String name = functions.getElementAt(
functionList.getSelectedIndex()).toString();
functionName.setText(name);


//calls method to build internalFrame
buildFunctionPanel();
infoPanel = new JPanel();
infoPanel.setBackground(Color.white);
//fills scrollpanes
sfunctionPanel = new JScrollPane(functionPanel);
sfunctionPanel.setAlignmentX(LEFT_ALIGNMENT);
functionFrame.getContentPane().setLayout (new BorderLayout());
functionFrame.getContentPane().add(sfunctionPanel, BorderLayout.WEST);
functionFrame.getContentPane().add(infoPanel, BorderLayout.CENTER);

functionFrame.setBackground(Color.white);
functionFrame.pack();
functionFrame.setVisible(true);
}
private void buildFunctionPanel()
{
functionPanel = new JPanel();
functionPanel.setBackground(Color.white);

functionPanel.setBorder(BorderFactory.createTitledBorder("Pick a Function"));

functionPanel.setLayout(new BoxLayout(functionPanel, BoxLayout.Y_AXIS));
functionPanel.add(Box.createRigidArea(new Dimension(0,200)));
functionPanel.add(function);
functionPanel.add(functionName);
functionPanel.add(Box.createRigidArea(new Dimension(0,25)));
functionPanel.add(sfunctionList);
functionPanel.add(Box.createRigidArea(new Dimension(0,25)));
functionPanel.add(newFunction);
functionPanel.add(Box.createRigidArea(new Dimension(0,25)));
functionPanel.add(delete);
newFunction.setBorder(BorderFactory.createRaisedBevelBorder());
delete.setBorder(BorderFactory.createRaisedBevelBorder());
newFunction.addActionListener(this);
delete.addActionListener(this);
functionList.addListSelectionListener(this);
}

public void actionPerformed(java.awt.event.ActionEvent evt)
{

if (evt.getSource()==newFunction){
newFunctionForm2 newForm2 = new newFunctionForm2();
infoPanel.removeAll();
infoPanel.add(newForm2.smgframe);
infoPanel.repaint();
}
if (evt.getSource()==functionName){
name = functionName.getText();
staticName = name;
infoPanel.removeAll();
functionForm2 funcForm = new functionForm2();
infoPanel.add(funcForm.smgframe);
infoPanel.repaint();
}
if (evt.getSource() == delete)
{
try{
String dataSourceName = "db1";
String dbURL = "jdbc:odbc:" + dataSourceName;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch (Exception e){System.out.println("Couldn't connect to driver.");}
Connection con = DriverManager.getConnection(dbURL, "","");
Statement stmt = con.createStatement();
con.setAutoCommit(false);
name = functionName.getText();
PreparedStatement fcDelete = con.prepareStatement("Delete from Function_Called where Function_Name = ?");
fcDelete.setString(1, name);
int deleteCount4 = fcDelete.executeUpdate();
PreparedStatement aDelete = con.prepareStatement("Delete from Arg where Function_Name = ?");
aDelete.setString(1, name);
int deleteCount3 = aDelete.executeUpdate();
PreparedStatement rDelete = con.prepareStatement("Delete from return_code_description where Function_Name = ?");
rDelete.setString(1, name);
int deleteCount2 = rDelete.executeUpdate();
PreparedStatement fDelete = con.prepareStatement("Delete from function_description where Function =? ");
fDelete.setString(1, name);
int deleteCount = fDelete.executeUpdate();
con.commit();

con.setAutoCommit(true);
}catch (SQLException e) {System.out.println(e.getMessage());}

functions.removeAllElements();
String dataSourceName = "db1";
String dbURL = "jdbc:odbc:" + dataSourceName;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Could not connect to driver.");}
try{
Connection con = DriverManager.getConnection(dbURL, "","");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT Function FROM function_description ORDER BY Function ASC");

//fills vector with all existing function names from database
while(result.next()){
functions.addElement(result.getString("Function"));
}
}catch (SQLException e){System.out.println(e.getMessage());}

functionList.setSelectedIndex(0);
}
}

public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {

if (functionList.getSelectedIndex() == -1) {
//No selection
functionName.setText("");

} else {
//Selection, update text field.
name = functionList.getSelectedValue().toString();
functionName.setText(name);
staticName = name;
infoPanel.removeAll();
functionForm2 funcForm = new functionForm2();
infoPanel.add(funcForm.smgframe);
infoPanel.repaint();
}
}
}



public static void main (String [] args)
{
gui3 fff = new gui3();

}


}


//Closes class


The applet



			
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.sql.*;
import java.util.*;
import javax.swing.text.*;


public class myApplet extends JPanel implements ActionListener, ListSelectionListener
{

private JApplet functionFrame;
public static DefaultListModel functions;
public JList functionList;
public JTextField functionName;
public JPanel functionPanel;
public static JPanel infoPanel;
public JScrollPane sfunctionList;
public JScrollPane sfunctionPanel;
private JLabel function;
public JButton newFunction;
public JButton delete;
public String name;
public static String staticName;

public myApplet()
{

}
private void buildFunctionPanel()
{
functionPanel = new JPanel();
functionPanel.setBackground(Color.white);

functionPanel.setBorder(BorderFactory.createTitledBorder("Pick a Function"));

functionPanel.setLayout(new BoxLayout(functionPanel, BoxLayout.Y_AXIS));
functionPanel.add(Box.createRigidArea(new Dimension(0,200)));
functionPanel.add(function);
functionPanel.add(functionName);
functionPanel.add(Box.createRigidArea(new Dimension(0,25)));
functionPanel.add(sfunctionList);
functionPanel.add(Box.createRigidArea(new Dimension(0,25)));
functionPanel.add(newFunction);
functionPanel.add(Box.createRigidArea(new Dimension(0,25)));
functionPanel.add(delete);
newFunction.setBorder(BorderFactory.createRaisedBevelBorder());
delete.setBorder(BorderFactory.createRaisedBevelBorder());
newFunction.addActionListener(this);
delete.addActionListener(this);
functionList.addListSelectionListener(this);
}

public void actionPerformed(java.awt.event.ActionEvent evt)
{

if (evt.getSource()==newFunction){
newFunctionForm2 newForm2 = new newFunctionForm2();
infoPanel.removeAll();
infoPanel.add(newForm2.smgframe);
infoPanel.repaint();
}
if (evt.getSource()==functionName){
name = functionName.getText();
staticName = name;
infoPanel.removeAll();
functionForm2 funcForm = new functionForm2();
infoPanel.add(funcForm.smgframe);
infoPanel.repaint();
}
if (evt.getSource() == delete)
{
try{
String dataSourceName = "\fiji.agere.com\C\FDD\db1";
String dbURL = "jdbc:odbc:" + dataSourceName;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch (Exception e){System.out.println("Couldn't connect to driver.");}
Connection con = DriverManager.getConnection(dbURL, "","");
Statement stmt = con.createStatement();
con.setAutoCommit(false);
name = functionName.getText();
PreparedStatement fcDelete = con.prepareStatement("Delete from Function_Called where Function_Name = ?");
fcDelete.setString(1, name);
int deleteCount4 = fcDelete.executeUpdate();
PreparedStatement aDelete = con.prepareStatement("Delete from Arg where Function_Name = ?");
aDelete.setString(1, name);
int deleteCount3 = aDelete.executeUpdate();
PreparedStatement rDelete = con.prepareStatement("Delete from return_code_description where Function_Name = ?");
rDelete.setString(1, name);
int deleteCount2 = rDelete.executeUpdate();
PreparedStatement fDelete = con.prepareStatement("Delete from function_description where Function =? ");
fDelete.setString(1, name);
int deleteCount = fDelete.executeUpdate();
con.commit();

con.setAutoCommit(true);
}catch (SQLException e) {System.out.println(e.getMessage());}

functions.removeAllElements();
String dataSourceName = "\fiji.agere.com\C\FDD\db1";
String dbURL = "jdbc:odbc:" + dataSourceName;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Could not connect to driver.");}
try{
Connection con = DriverManager.getConnection(dbURL, "","");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT Function FROM function_description ORDER BY Function ASC");

//fills vector with all existing function names from database
while(result.next()){
functions.addElement(result.getString("Function"));
}
}catch (SQLException e){System.out.println(e.getMessage());}

functionList.setSelectedIndex(0);
}
}

public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {

if (functionList.getSelectedIndex() == -1) {
//No selection
functionName.setText("");

} else {
//Selection, update text field.
name = functionList.getSelectedValue().toString();
functionName.setText(name);
staticName = name;
infoPanel.removeAll();
functionForm2 funcForm = new functionForm2();
infoPanel.add(funcForm.smgframe);
infoPanel.repaint();
}
}
}



public void init()
{
functionFrame = new JApplet();

functions = new DefaultListModel();
//initializes label
function = new JLabel("Function Name");

//initializes button
newFunction = new JButton("Create New Function");
delete = new JButton ("Delete Function");

//initializes string
name = new String();
staticName = new String();

//connects to database
String dataSourceName = "\fiji.agere.com\C\FDD\db1";
String dbURL = "jdbc:odbc:" + dataSourceName;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Could not connect to driver.");}
try{
Connection con = DriverManager.getConnection(dbURL, "","");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT Function FROM function_description ORDER BY Function ASC");

//fills vector with all existing function names from database
while(result.next()){
functions.addElement(result.getString("Function"));
}
}catch (SQLException e){System.out.println(e.getMessage());}

//defines list of function names
functionList = new JList (functions);

// functionList.setEditable(true);
functionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
functionList.setSelectedIndex(0);
sfunctionList = new JScrollPane(functionList);
sfunctionList.setAlignmentX(LEFT_ALIGNMENT);

//initializes text field
functionName = new JTextField();
functionName.setAlignmentX(LEFT_ALIGNMENT);
functionName.addActionListener(this);
String name = functions.getElementAt(
functionList.getSelectedIndex()).toString();
functionName.setText(name);


//calls method to build internalFrame
buildFunctionPanel();
infoPanel = new JPanel();
infoPanel.setBackground(Color.white);
//fills scrollpanes
sfunctionPanel = new JScrollPane(functionPanel);
sfunctionPanel.setAlignmentX(LEFT_ALIGNMENT);
functionFrame.getContentPane().setLayout (new BorderLayout());
functionFrame.getContentPane().add(sfunctionPanel, BorderLayout.WEST);
functionFrame.getContentPane().add(infoPanel, BorderLayout.CENTER);

functionFrame.setBackground(Color.white);
functionFrame.setSize(999,999);
functionFrame.setVisible(true);

functionFrame.init();
functionFrame.start();
}

public void start()
{
}

public void stop()
{

}

public void destroy()
{}

}


//Closes class

   <<Less

Re: converting an application to an applet

Posted By:   Anonymous  
Posted On:   Friday, June 7, 2002 11:17 AM

In myApplet there are a few mistakes- I shoudl have extended JApplet. And even if i delete functionFrame.setVisible(true), functionFrame.init() and functionFrame.start() from the public void init thread, my applet starts but it DOESN'T SHOW ANYTHING!!!
please please help,
thanks!
About | Sitemap | Contact