How to Create a Java Applet With Drop-Down Lists
Applets are special programs written in Java which, instead of running directly on a user's desktop or command prompt like normal programs, are instead embedded inside web pages using the <APPLET> HTML tag. This allows programmers to build complex applications in the powerful Java language and insert them easily into their web pages. Whenever the user visits the page, his browser automatically downloads and runs the Java program within the page.
Instructions
-
-
1
Create a new Java file named \"DropDownApplet.java.\" If you have an integrated Java development environment (IDE) on your system, there will be a \"New Class\" option in your File menu. However, if you don't use an IDE, you can just save a text file with the \".java\" extension using Notepad.
-
2
Add the following import statements to the top of the Java file you just created:<br /><br />import java.applet.Applet<br />GO<br />import java.awt.event.ActionEvent<br />GO<br />import java.awt.event.ActionListener<br />GO<br />import javax.swing.JComboBox<br />GO<br />import javax.swing.JLabel;
-
-
3
Create a class named DropDownApplet. It will extend the Applet class that comes with the Java standard library, allowing it to be run from within web pages. It will also implement the ActionListener interface from the Java library, allowing it to respond to user actions. Do this by pasting the following code:<br /><br /><br />/**<br />* This class demonstrates a simple Java applet that contains a drop down box.<br />* @author Kevin Walker<br />*/<br />public class DropDownApplet extends Applet implements ActionListener {<br /><br />}<br /><br /><br />All other code will go inside the brackets.
-
4
Create two components by adding the following code inside the class:<br /><br /> JComboBox comboBox = new JComboBox()<br />GO<br /> JLabel label = new JLabel()<br />GO<br /><br /> The first is a JComboBox component which holds your drop down list. The second is a JLabel component which changes to reflect the contents of the JComboBox.
-
5
Create a \"constructor\" method which will be performed as soon as the DropDownApplet is created by pasting the following code:<br /><br /> public DropDownApplet() {<br /> super()<br />GO<br /> this.add(comboBox)<br />GO<br /> this.add(label)<br />GO<br /> this.setVisible(true)<br />GO<br /> comboBox.addItem(\"Item 1\")<br />GO<br /> comboBox.addItem(\"Item 2\")<br />GO<br /> comboBox.addItem(\"Item 3\")<br />GO<br /> comboBox.addItem(\"Item 4\")<br />GO<br /> comboBox.removeItem(\"Item 4\")<br />GO<br /> comboBox.addActionListener(this)<br />GO<br /> label.setText(comboBox.getSelectedItem().toString())<br />GO<br /><br /> }<br /><br />Going line by line, this code first calls the command \"super\" to run the constructor of the base Applet class which the DropDownApplet class is being derived from. Next, it adds the JComboBox and the JLabel created in step 4 to the applet's GUI. Then, it tells the applet to become visible. <br /><br />After that, the JComboBox is populated with a list of items and, just for the sake of example, one of the items is immediately removed. <br /><br />The line \"comboBox.addActionListener(this)\" tells the JComboBox to report back to the main program anytime a change occurs. <br /><br />Finally, the last line sets the text of the JLabel to whatever the selected item from the JComboBox is. At the start of the program, it will be \"Item 1.\"
-
6
Create an \"actionPerformed\" method. In step 5, you told the JComboBox to report back to the main program whenever the user performs some action upon it. This is the method JComboBox will report to. Since this is a simple example, it will just alter the text of the label anytime the user changes it:<br /><br /><br /> public void actionPerformed(ActionEvent e) {<br /> <br /> label.setText(comboBox.getSelectedItem().toString())<br />GO<br /><br /> }
-
1