// TEST CODES TO CATCH EXCEPTION (APPLET VERSION) /* A simple applet that accepts a number and prints it at System.out. If no input is given, aN error message is displayed in the status bar. If the entry is non-numeric, a catch block catches the error and another error message is displayed. */ /* To test the code, compile it using the command javac ExApplet.java. Create a html file that has an applet tag apecifying code as ""ExApplet.class" and height and width both as 200. Save the file as ExApplet.html and run it using the command appletviewer ExApplet.html. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ExApplet extends JApplet implements ActionListener { JPanel panel; JLabel labQty; JTextField txtQty; JButton btnOk; GridBagLayout gbl; GridBagConstraints gbc; public ExApplet() // Build the panel and add to frame { panel= new JPanel(); labQty = new JLabel("Qty"); txtQty = new JTextField(5); btnOk = new JButton("OK"); gbl = new GridBagLayout(); gbc = new GridBagConstraints(); panel.setLayout(gbl); getContentPane().add(panel); gbc.anchor = GridBagConstraints.NORTHWEST; gbc.gridx = 0; gbc.gridy = 0; gbl.setConstraints(labQty, gbc); panel.add(labQty); gbc.gridx = 1; gbc.gridy = 0; gbl.setConstraints(txtQty, gbc); panel.add(txtQty); gbc.gridx = 1; gbc.gridy = 1; gbl.setConstraints(btnOk, gbc); panel.add(btnOk); btnOk.addActionListener(this); } public void checkQty() // Check what value we got { int num; String tmp = txtQty.getText(); if(tmp.length() == 0) // Is Qty empty? { getAppletContext().showStatus("Qty should not be empty."); } else // Check if input entered is not numeric { try { num = Integer.parseInt(tmp); System.out.println("\nQty is " + num); getAppletContext().showStatus(""); } catch(NumberFormatException x) { getAppletContext().showStatus("Qty should be numeric."); } } } public void actionPerformed(ActionEvent e) // OK button listener { if(e.getSource() == btnOk) { checkQty(); } } public void init() { ExApplet exApp = new ExApplet(); } } /* Modify the code so that the message "Command successful" shows in the applet status bar if no error was detected. */