Help with excel to applet
don't know why i have a error
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import java.text.*;
import java.awt.Choice;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
publicclass MortgageAppletGUIextends Applet{
Button calculateButton;
Button clearButton;
Button exitButton;
Button excelButton;
TextField principalField;
TextField interestField;
TextField termField;
TextArea displayArea;
Label principal;
Label term;
Label interest;
Image house;
Font title =new Font("TimesRoman", Font.BOLD, 16);
Font text =new Font("Courier", Font.BOLD, 12);
double monthlyInt;
double monthlyPayment;
int months=12;
double interestPayment;
double principalPayment;
double remainingBalance;
Choice principalChoice;
Choice interestChoice;
Choice termChoice;
publicstaticfinal String DRIVER_NAME =
"sun.jdbc.odbc.JdbcOdbcDriver";
publicstaticfinal String DATABASE_URL ="jdbc:odbc:employee_xls";
publicvoid init(){
MortgageAppletLayout customLayout =new MortgageAppletLayout();
setFont(new Font("Helvetica", Font.PLAIN, 12));
setLayout(customLayout);
calculateButton =new Button("Calculate");
calculateButton.addActionListener(new CalculateActionListener());
add(calculateButton);
displayArea =new TextArea();
add(displayArea);
principal =new Label("Principal");
add(principal);
interest =new Label("Interest");
add(interest);
term =new Label("Term");
add(term);
clearButton =new Button("Clear");
clearButton.addActionListener(new CancelActionListener());
add(clearButton);
principalField =new TextField(20);
add(principalField);
interestField =new TextField(20);
add(interestField);
termField =new TextField(20);
add(termField);
principalChoice =new Choice();
principalChoice.addItem("200000");
principalChoice.addItemListener(new ChoicePrincipal());
add(principalChoice);
interestChoice =new Choice();
interestChoice.addItem("5.35");
interestChoice.addItem("5.5");
interestChoice.addItem("5.75");
interestChoice.addItemListener(new ChoiceInterest());
add(interestChoice);
termChoice =new Choice();
termChoice.addItem("7");
termChoice.addItem("15");
termChoice.addItem("30");
termChoice.addItemListener(new ChoiceTerm());
add(termChoice);
exitButton =new Button("Exit");
exitButton.addActionListener(new ExitActionListener());
add(exitButton);
excelButton =new Button("Chart");
excelButton.addActionListener(new ExcelActionListener());
add(excelButton);
setSize(getPreferredSize());
house = getImage(getCodeBase(),"house.jpg");
}
class CalculateActionListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent e){
NumberFormat Currency = NumberFormat.getCurrencyInstance();
String userInputprincipal = principalField.getText();
String userInputinterest = interestField.getText();
String userInputterm = termField.getText();
double userInputprincipalD=0;
double userInputinterestD=0;
double userInputtermD=0;
try{
userInputprincipalD = Double.parseDouble(userInputprincipal);
}catch (Exception ex){
JOptionPane.showMessageDialog(null,"Error principalField","Exception 01", JOptionPane.ERROR_MESSAGE);
principalField.requestFocus();
return;
}
try{
userInputinterestD = Double.parseDouble(userInputinterest);
}catch (Exception ex){
JOptionPane.showMessageDialog(null,"Error interestField","Exception 02", JOptionPane.ERROR_MESSAGE);
interestField.requestFocus();
return;
}
try{
userInputtermD = Double.parseDouble(userInputterm);
}catch (Exception ex){
JOptionPane.showMessageDialog(null,"Error termField","Exception 03", JOptionPane.ERROR_MESSAGE);
termField.requestFocus();
return;
}
// Display principal, interest, and loan balance of entire term
double monthlyInt;
double monthlyPayment;
double interestPayment;
double principalPayment;
double remainingBalance;
double months = userInputtermD * 12;
monthlyInt = (userInputinterestD / 100) / 12;
monthlyPayment = (userInputprincipalD * monthlyInt) / (1 - Math.pow(1 / (1 + monthlyInt), userInputtermD * 12));
for (int i = 1; i <= months; ++i){
interestPayment = userInputprincipalD * monthlyInt;
principalPayment = monthlyPayment - interestPayment;
remainingBalance = userInputprincipalD - principalPayment;
displayArea.append("\n#" + i +"\tP=" + Currency.format(principalPayment) +"\tI=" + Currency.format(interestPayment)
+"\tB=" + Currency.format(remainingBalance) +"\n");
userInputprincipalD -= principalPayment;// calculate until payments are done
}
}
}
class ExcelActionListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent e){
Class.forName(DRIVER_NAME);
Connection con =null;
try{
con = DriverManager.getConnection(DATABASE_URL);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("select lastname, firstname, id from [Sheet1$]");
while (rs.next()){
String lname = rs.getString(1);
String fname = rs.getString(2);
int id = rs.getInt(3);
displayArea.append(fname +" " + lname +" id : " + id);
}
rs.close();
stmt.close();
}
catch(Exception ex){
}
finally{
try
{
if (con !=null)
{
con.close();
}
}
catch(SQLException ignored){
}
}
}
class CancelActionListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent e){
principalField.setText("");
interestField.setText("");
termField.setText("");
displayArea.setText("");
}
}
class ExitActionListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent e){
System.exit(0);
}
}
class ChoicePrincipalimplements ItemListener{
publicvoid itemStateChanged(ItemEvent ie){
Choice cb1 = (Choice)ie.getSource();// the source of the event is the combo box
String principalC = (String)cb1.getSelectedItem();
principalField.setText(principalC);
}
}
class ChoiceInterestimplements ItemListener{
publicvoid itemStateChanged(ItemEvent ie){
Choice cb1 = (Choice)ie.getSource();// the source of the event is the combo box
String interestC = (String)cb1.getSelectedItem();
interestField.setText(interestC);
}
}
class ChoiceTermimplements ItemListener{
publicvoid itemStateChanged(ItemEvent ie){
Choice cb1 = (Choice)ie.getSource();// the source of the event is the combo box
String termC = (String)cb1.getSelectedItem();
termField.setText(termC);
}
}
publicvoid paint(Graphics g){
// Display program info
g.drawImage(house, 10, 5,this);
g.setColor(Color.green);
g.drawLine(20, 4, 230, 4);
g.setColor(Color.red);
g.setFont(title);
g.drawString("\tMortgage Loan Calculator", 10, 17);
g.setColor(Color.black);
g.setFont(text);
g.drawString("\tCreated by Me", 10, 30);
}
publicstaticvoid main(String args[]){
MortgageAppletGUI applet =new MortgageAppletGUI();
Frame window =new Frame("MortgageApplet");
window.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
applet.init();
window.add("Center", applet);
window.pack();
window.setVisible(true);
}
}
class MortgageAppletLayoutimplements LayoutManager{
public MortgageAppletLayout(){
}
publicvoid addLayoutComponent(String name, Component comp){
}
publicvoid removeLayoutComponent(Component comp){
}
public Dimension preferredLayoutSize(Container parent){
Dimension dim =new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 648 + insets.left + insets.right;
dim.height = 375 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent){
Dimension dim =new Dimension(0, 0);
return dim;
}
publicvoid layoutContainer(Container parent){
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+280,72,24);}
c = parent.getComponent(1);
if (c.isVisible()){c.setBounds(insets.left+248,insets.top+88,328,216);}
c = parent.getComponent(2);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+104,72,24);}
c = parent.getComponent(3);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+152,72,24);}
c = parent.getComponent(4);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+128,72,24);}
c = parent.getComponent(5);
if (c.isVisible()){c.setBounds(insets.left+88,insets.top+280,72,24);}
c = parent.getComponent(6);
if (c.isVisible()){c.setBounds(insets.left+80,insets.top+104,72,24);}
c = parent.getComponent(7);
if (c.isVisible()){c.setBounds(insets.left+80,insets.top+128,72,24);}
c = parent.getComponent(8);
if (c.isVisible()){c.setBounds(insets.left+80,insets.top+152,72,24);}
c = parent.getComponent(9);
if (c.isVisible()){c.setBounds(insets.left+152,insets.top+104,72,24);}
c = parent.getComponent(10);
if (c.isVisible()){c.setBounds(insets.left+152,insets.top+128,72,24);}
c = parent.getComponent(11);
if (c.isVisible()){c.setBounds(insets.left+152,insets.top+152,72,24);}
c = parent.getComponent(12);
if (c.isVisible()){c.setBounds(insets.left+172,insets.top+280,50,24);}
c = parent.getComponent(13);
if (c.isVisible()){c.setBounds(insets.left+172,insets.top+380,50,24);}
}
}
[20817 byte] By [
JavaBean5a] at [2007-11-27 4:51:05]

... what's your error? Can you paste the output of when you ran or compiled it?
java:258: illegal start of expressionpublic static void main(String args[]) {^1 errorTool completed with exit code 1
Nevermind, I misread your brackets because there's no indentation. I'll continue to look at it =P
This is an applet. You don't use "public static void main(String[] args)" for applets. You use init. And you run it using appletviewer or the like.
wait, that part has always worked, its part of the applet menus.
One more thing: Unless they changed something in Java6, applets need to be signed in order to make remote connections, e.g. a remote connection to a SQL database. And signed applets are really annoying...
what do you mean by that?
Oh, I see, you do have an init method. I've never done it that way (starting the applet up through an application). Does it work if you just run it with appletviewer and move the code in the main method to the init method?
its messy if i do thatbut the way its step has been working
The only concern I would have in that is that sometimes there are preparatory or cleanup methods invoked behind the scenes before or after the method you write. For example, you never call myThread.run(), you call myThread.start();
I tried pasting your code into Eclipse but Eclipse gave lots of errors. And in order to investigate these errors, I'd have to be able to read your code, but you have no indentations so it's a bit hard to do.
sorry, i'm still a beginner.
I just tested the "running an applet through an application" method out and it seems to work, so nevermind that first paragraph.
I think the main problem with my code is this area:
class ExcelActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Class.forName(DRIVER_NAME);
Connection con = null;
try {
con = DriverManager.getConnection(DATABASE_URL);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("select lastname, firstname, id from [Sheet1$]");
while (rs.next()) {
String lname = rs.getString(1);
String fname = rs.getString(2);
int id = rs.getInt(3);
displayArea.append(fname + " " + lname + " id : " + id);
}
rs.close();
stmt.close();
}
catch(Exception ex) {
}
finally {
try
{
if (con != null)
{
con.close();
}
}
catch(SQLException ignored) {
}
}
}
I cleaned up your code, and now it runs. I suspect it was a few bad braces or something like that.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import java.text.*;
import java.awt.Choice;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
public class MortgageAppletGUI extends Applet {
public static void main(String args[]) {
MortgageAppletGUI applet = new MortgageAppletGUI();
Frame window = new Frame("MortgageApplet");
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
applet.init();
window.add("Center", applet);
window.pack();
window.setVisible(true);
}
class CalculateActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
NumberFormat Currency = NumberFormat.getCurrencyInstance();
String userInputprincipal = principalField.getText();
String userInputinterest = interestField.getText();
String userInputterm = termField.getText();
double userInputprincipalD=0;
double userInputinterestD=0;
double userInputtermD=0;
try {
userInputprincipalD = Double.parseDouble(userInputprincipal);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error principalField", "Exception 01", JOptionPane.ERROR_MESSAGE);
principalField.requestFocus();
return;
}
try {
userInputinterestD = Double.parseDouble(userInputinterest);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error interestField", "Exception 02", JOptionPane.ERROR_MESSAGE);
interestField.requestFocus();
return;
}
try {
userInputtermD = Double.parseDouble(userInputterm);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error termField", "Exception 03", JOptionPane.ERROR_MESSAGE);
termField.requestFocus();
return;
}
// Display principal, interest, and loan balance of entire term
double monthlyInt;
double monthlyPayment;
double interestPayment;
double principalPayment;
double remainingBalance;
double months = userInputtermD * 12;
monthlyInt = (userInputinterestD / 100) / 12;
monthlyPayment = (userInputprincipalD * monthlyInt) / (1 - Math.pow(1 / (1 + monthlyInt), userInputtermD * 12));
for (int i = 1; i <= months; ++i) {
interestPayment = userInputprincipalD * monthlyInt;
principalPayment = monthlyPayment - interestPayment;
remainingBalance = userInputprincipalD - principalPayment;
displayArea.append("\n#" + i + "\tP=" + Currency.format(principalPayment) + "\tI=" + Currency.format(interestPayment)
+ "\tB=" + Currency.format(remainingBalance) + "\n");
userInputprincipalD -= principalPayment; // calculate until payments are done
}
}
}
class ExcelActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try { Class.forName(DRIVER_NAME); } catch (Exception exc) {}
Connection con = null;
try {
con = DriverManager.getConnection(DATABASE_URL);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("select lastname, firstname, id from [Sheet1$]");
while (rs.next()) {
String lname = rs.getString(1);
String fname = rs.getString(2);
int id = rs.getInt(3);
displayArea.append(fname + " " + lname + " id : " + id);
}
rs.close();
stmt.close();
}
catch(Exception ex) {}
finally {
try {
if (con != null) {
con.close();
}
}
catch(SQLException ignored) {}
}
}
}
class CancelActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
principalField.setText("");
interestField.setText("");
termField.setText("");
displayArea.setText("");
}
}
class ExitActionListener implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class ChoicePrincipal implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
Choice cb1 = (Choice)ie.getSource(); // the source of the event is the combo box
String principalC = (String)cb1.getSelectedItem();
principalField.setText(principalC);
}
}
class ChoiceInterest implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
Choice cb1 = (Choice)ie.getSource(); // the source of the event is the combo box
String interestC = (String)cb1.getSelectedItem();
interestField.setText(interestC);
}
}
class ChoiceTerm implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
Choice cb1 = (Choice)ie.getSource(); // the source of the event is the combo box
String termC = (String)cb1.getSelectedItem();
termField.setText(termC);
}
}
Button calculateButton;
Button clearButton;
Button exitButton;
Button excelButton;
TextField principalField;
TextField interestField;
TextField termField;
TextArea displayArea;
Label principal;
Label term;
Label interest;
//Image house;
Font title = new Font("TimesRoman", Font.BOLD, 16);
Font text = new Font("Courier", Font.BOLD, 12);
double monthlyInt;
double monthlyPayment;
int months=12;
double interestPayment;
double principalPayment;
double remainingBalance;
Choice principalChoice;
Choice interestChoice;
Choice termChoice;
public static final String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
public static final String DATABASE_URL = "jdbc:odbc:employee_xls";
public void init() {
MortgageAppletLayout customLayout = new MortgageAppletLayout();
setFont(new Font("Helvetica", Font.PLAIN, 12));
setLayout(customLayout);
calculateButton = new Button("Calculate");
calculateButton.addActionListener(new CalculateActionListener());
add(calculateButton);
displayArea = new TextArea();
add(displayArea);
principal = new Label("Principal");
add(principal);
interest = new Label("Interest");
add(interest);
term = new Label("Term");
add(term);
clearButton = new Button("Clear");
clearButton.addActionListener(new CancelActionListener());
add(clearButton);
principalField = new TextField(20);
add(principalField);
interestField = new TextField(20);
add(interestField);
termField = new TextField(20);
add(termField);
principalChoice = new Choice();
principalChoice.addItem("200000");
principalChoice.addItemListener(new ChoicePrincipal());
add(principalChoice);
interestChoice = new Choice();
interestChoice.addItem("5.35");
interestChoice.addItem("5.5");
interestChoice.addItem("5.75");
interestChoice.addItemListener(new ChoiceInterest());
add(interestChoice);
termChoice = new Choice();
termChoice.addItem("7");
termChoice.addItem("15");
termChoice.addItem("30");
termChoice.addItemListener(new ChoiceTerm());
add(termChoice);
exitButton = new Button("Exit");
exitButton.addActionListener(new ExitActionListener());
add(exitButton);
excelButton = new Button("Chart");
excelButton.addActionListener(new ExcelActionListener());
add(excelButton);
setSize(getPreferredSize());
//house = getImage(getCodeBase(), "house.jpg");
}
public void paint(Graphics g) {
// Display program info
super.paint(g);
//g.drawImage(house, 10, 5, this);
g.setColor(Color.green);
g.drawLine(20, 4, 230, 4);
g.setColor(Color.red);
g.setFont(title);
g.drawString("\tMortgage Loan Calculator", 10, 17);
g.setColor(Color.black);
g.setFont(text);
g.drawString("\tCreated by Me", 10, 30);
}
}
class MortgageAppletLayout implements LayoutManager {
public MortgageAppletLayout() {}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 648 + insets.left + insets.right;
dim.height = 375 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+280,72,24);}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+248,insets.top+88,328,216);}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,72,24);}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+152,72,24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+128,72,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+280,72,24);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+80,insets.top+104,72,24);}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+80,insets.top+128,72,24);}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+80,insets.top+152,72,24);}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+152,insets.top+104,72,24);}
c = parent.getComponent(10);
if (c.isVisible()) {c.setBounds(insets.left+152,insets.top+128,72,24);}
c = parent.getComponent(11);
if (c.isVisible()) {c.setBounds(insets.left+152,insets.top+152,72,24);}
c = parent.getComponent(12);
if (c.isVisible()) {c.setBounds(insets.left+172,insets.top+280,50,24);}
c = parent.getComponent(13);
if (c.isVisible()) {c.setBounds(insets.left+172,insets.top+380,50,24);}
}
}
N.B.: I commented out the house image b/c I don't have it in my code base.
In case that's hard to copy/paste in your environment: http://www.rolyata.com/~andrew/tmp/MortgageAppletGUI.java
Any reason why the displayarea doesn't work for the excel data?
I spent a lot of time cleaning up your code =p I'm not sure how much it helps but it does get it to run. And yet I get no thanks =( *cries*
oh so sorry, thank you very very much.