I have created an applet with two choice controls. One of the choice control is the OS name and the other is the browser name. I have added the item listeners for both these choice controls, so that we can detect the option selected for each of the choice control. The code adds another OS entry ("Windows 10")in the OS choice control if the browser selected in Firefox.
Somehow when I select the browser as firefox , multiple entries of "Windows 10' are added in the OS choice control.
Can someone please help me?
SingleClick
package p1;
import java.applet.Applet;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class SingleClick implements ItemListener {
Applet currentApplet;
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
this.currentApplet.repaint();
}
public void appletLink(Applet test) {
this.currentApplet = test;
}
}
OSBrowserSelector
package p1;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Label;
import java.awt.List;
import java.awt.event.ItemListener;
public class OSBrowserSelector extends Applet {
Choice os;
Choice browser;
public void init() {
os = new Choice();
browser = new Choice();
// Set size of the applet window
this.setSize(500, 500);
// Add the operating systems
os.add("XP");
os.add("Windows 7");
os.add("Macos");
//Add the browsers
browser.add("Chrome");
browser.add("firefox");
browser.add("ie");
//Make the controls visible
this.add(os);
this.add(browser);
// Set the listener for the selection
//SingleClick class implements the ItemListener interface
SingleClick l = new SingleClick();
l.appletLink(this);
// Add the listener
os.addItemListener(l);
browser.addItemListener(l);
}
public void start() {
}
public void paint(Graphics g) {
//If the firefox is selected as the browser , add another entry in the os for "Windows 10"
if (browser.getSelectedItem().equalsIgnoreCase("firefox")) {
this.os.add("Windows 10");
}
}
}