package ch06;

//listing 7
// A Swing-based applet that uses the showDocument()
// method defined by AppletContext to display a
// a web page. It takes two parameters. The first
// specifies the URL of the primary web site and the second
// specifies the URL of a secondary web site.
// The user selects which site to display by checking
// or clearing a check box. Pressing the Show Page Now
// button causes the page to be displayed.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

/*
<applet code="ShowURL" width=220 height=100>
<param name=primarySite value=HerbSchildt.com>
<param name=SecondarySite value=McGrawHill.com>
<param name=default value=0>
</applet>
 */
public class ShowURL extends JApplet {

    JLabel jlab;
    JCheckBox jcbPrimary;
    String primary;
    String secondary;

    /**
     * Initialization method that will be called after the applet is
loaded
     * into the browser.
     */
    @Override
    public void init() {
        // TODO start asynchronous download of heavy resources
        primary = getParameter("primarySite");
        secondary = getParameter("secondarySite");

        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
                    makeGUI();
                }
            });
        } catch (Exception exc) {
            System.out.println("Can't create because of " + exc);
        }
    }

    // Initialize the GUI.
    private void makeGUI() {

        // Set the content pane's layout to flow layout.
        setLayout(new FlowLayout());

        // Note: If you are using a version of Java prior to
        // JDK 5, then you will need to use getContentPane()
        // to explicitly set the content pane's layout,
        // as shown here:
        //
        //   getContentPane().setLayout(new FlowLayout());

        // Create the label that will display the target
        // web site.
        jlab = new JLabel("Transfer to " + primary);

        // Create a check box.
        jcbPrimary = new JCheckBox("Use Primary Site", true);

        // Handle check box item events.
        jcbPrimary.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent ie) {

                // Toggle between primary and secondary sites.
                if (jcbPrimary.isSelected()) {
                    jlab.setText("Transfer to " + primary);
                } else {
                    jlab.setText("Transfer to " + secondary);
                }
            }
        });

        // Add a button that transfers to selected web site.
        JButton jbtnJump = new JButton("Show Page Now");

        // Create the action listener for the button.
        jbtnJump.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                showStatus("Transferring to selected site.");

                // Transfer to the desired site.
                try {
                    if (jcbPrimary.isSelected()) {
                        getAppletContext().showDocument(
                                new URL("http://www."; + primary));
                    } else {
                        getAppletContext().showDocument(
                                new URL("http://www."; + secondary));
                    }
                } catch (MalformedURLException exc) {
                    showStatus("Error in URL.");
                }
            }
        });

        // Add the label, check boxes, and button to the
        // applet's content pane.
        add(jcbPrimary);
        add(jlab);
        add(jbtnJump);

    // Note: If you are using a version of Java prior to
    // JDK 5, then you will need to use getContentPane()
    // to explicitly add compponents to the content pane,
    // as shown here.
    //
    //   getContentPane().add(jcbPrimary);
    //
    // and so on.
    }

    // TODO overwrite start(), stop() and destroy() methods
}

Enjoy!

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to