0

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");
        }

    }

}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Raj
  • 203
  • 1
  • 2
  • 8
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 24 '14 at 03:14

1 Answers1

1
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");
    }

}

Paint will be called whenever the JRE thinks it is necessary to do so. It might be triggered by (our code) changing the components in the applet (i.e. adding or removing components), or changing the values displayed in the fields. It might be triggered by minimizing and restoring the browser. It might be triggered by bringing another window in front of the browser, then moving it off..

The best place to do such things is in the init() method, which is only called once.


BTW - do you know of the following method (which makes an OS 'chooser' unnecessary)?

System.getProperty("os.name");

What are you actually trying to achieve from these user choices?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433