Mustafa Mustafa - 5 months ago 9
Java Question

Run a method when button is clicked

I need a certain function to run when a button (reset button) is clicked.
I get an error: " illegal start of expression" and need help with it:

private void resetActionPerformed(java.awt.event.ActionEvent evt) {

String sDay = day.getText();
String sName = name.getText();
String sCash = cash.getText();
String sReason = reason.getText();
String sCourse = courses.toString();

public static void main(String[] args) throws IOException, WriteException {

String fileName = "E:\\Java Workspace\\file" + day + ".xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
WritableSheet sheet = workbook.createSheet(name, 0);

Label label1 = new Label(0, 0, "A label word record");
sheet.addCell(label1);

jxl.write.Number number = new jxl.write.Number(0, 1, 3.1459);
sheet.addCell(number);

workbook.write();
workbook.close();
}
// TODO add your handling code here:
}

Answer

Follow this code

public class FooExample {

    private void resetActionPerformed(java.awt.event.ActionEvent evt) {                                      

        String sDay = day.getText();
        String sName = name.getText();
        String sCash = cash.getText();
        String sReason = reason.getText();
        String sCourse = courses.toString();

    }  // the fix

    public static void main(String[] args) throws IOException, WriteException {

        try {
            String fileName = "E:\\Java Workspace\\file" + day + ".xls";
            WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
            WritableSheet sheet = workbook.createSheet(name, 0);

            Label label1 = new Label(0, 0, "A label word record");
            sheet.addCell(label1);

            jxl.write.Number number = new jxl.write.Number(0, 1, 3.1459);
            sheet.addCell(number);

            workbook.write();
            workbook.close();
        } catch(Exception ex){ // the fix
            System.out.println("Error " + ex.getMessage());
            // handle Exception as you need
        }
}

}

You have to use try and catch block to handle checked and unchecked Exceptions. Now you can handle the Exception what you got. And more thing you added one TODO to handle Exception, so you must handle the Exception

Comments