Applet example: testing the IntArrayBag
Note: all code except "remove" part comes from textbook chapter 3 section 4.
Running the applet
Compile as usual. Applet is activated through "appletviewer" command or from browser. In either case, an HTML <applet></applet> tag pair is required, so you need a little HTML file containing at minimum the following content: <applet code="BagApplet.class" width=480 height=340></applet>
The width and height indicate size of applet window in pixels. Save this short bit of code in BagApplet.html, then point your browser or appletview to it. Run the BagApplet here.
Components of the applet
The GUI components
Three types are demonstrated here: Button, TextField, and TextArea.
Each component is declared and created as an instance variable. Important: it is not yet displayed at this time.
The init() method
When your applet is run, the init() method in your class is executed. In that sense, it is like main(), except it is not static. Main is not used for applets, but by defining both an init and a main, it is possible for a class to be run either as an applet or an application.
This method serves three major functions:
1. place the GUI components onto the UI window.
2. associate action listeners with active GUI components (buttons).
3. When execution reaches the end of init, the UI window is displayed and user can interact with the applet.
The action listeners
One class is defined for each button, and one object of this class is instantiated and associated with the button in init().
This class is defined inside the BagApplet class. This is called "inner class" and is necessary to allow your code to access the GUI components.
Each class has a standard template:
Class action_listener_class_name_here implements ActionListener { public void actionPerformed(ActionEvent event) { // your code here } }
Look at the code associated with "add" button
1. In declaration of BagApplet class components. elementText is a TextField into which user will type in the value to be added to the bag.
Button addButton = new Button("add( )"); TextField elementText = new TextField(10);
2. Adding the button and textfield to UI, in init:
add(addButton); add(elementText);
3. Associating button with action listener, in init:
addButton.addActionListener(new AddListener( ));
4. Define AddListener inner class:
class AddListener implements ActionListener { public void actionPerformed(ActionEvent event) { try { String userInput = elementText.getText( ); int element = Integer.parseInt(userInput); b.add(element); feedback.append(element + " has been added to the bag.\n"); } catch (NumberFormatException e) { feedback.append("Type an integer before clicking button.\n"); elementText.requestFocus( ); elementText.selectAll( ); } } }
How to add a working "remove" button to BagApplet.java
1. In declaration of class components, add two more instance variables:
Button removeButton = new Button("remove( )"); TextField removeElemText = new TextField(10);
2. Add those GUI components to UI layout, in init method.
add(removeButton); add(removeElemText);
3. Associate a listener object (class specified below) with the button, in init method.
removeButton.addActionListener(new RemoveListener( ));
4. Develop listener inner class to be used with this button. In this case, you can copy the AddListener class and make a couple modifications.
class RemoveListener implements ActionListener { public void actionPerformed(ActionEvent event) { try { String userInput = removeElemText.getText( ); int element = Integer.parseInt(userInput); b.remove(element); feedback.append(element + " has been removed from the bag.\n"); } catch (NumberFormatException e) { feedback.append("Type an integer before clicking button.\n"); removeElemText.requestFocus( ); removeElemText.selectAll( ); } } }
Run the modified BagAppletRemove here.
[ Lectures | CSC 132 | Peter Sanderson | Computer Science | SMSU ]
Last reviewed: 18 September 2000
Peter Sanderson (
PeteSanderson@smsu.edu )