advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Step by Step :
Chapter 3 :

Contents
Applet Functions
Statusbar Messages
Opening Web Pages
Playing Sound Files
Using HTML Parameters

Chapter3 - Applet Functions

2. Opening Web Pages


/* We will open Web pages with this applet.
The user will be able to type in an address and the applet
will try to open it. If the adress is malformed we will try to fix
it or show an error.
This example will show you also about String methods, which you can use
to check user text input.

*/

import java.awt.*;
import java.net.*;
import java.applet.*;
import java.awt.event.*;
 

public class ShowDocumentExample extends Applet implements ActionListener 
{
     TextField urlField;
     Button goButton;
 // This varable will determine if the URL should be loaded
 // or if an error message should appear.
     boolean UrlOnError;
 // The URL that we want to display
     URL userUrl;

     public void init() 
     {
          setLayout(new FlowLayout());
          urlField = new TextField("RealApplets.com");
          goButton = new Button("Go!");
          urlField.addActionListener(this);
          goButton.addActionListener(this);
          add(urlField);
          add(goButton);
     }

     public void paint(Graphics g)
     {
          // Will display when correct URL are entered and when the applet starts.
          // !UrlOnError means "do if UrlOnError is false";
          if (!UrlOnError) 
               g.drawString("Type your URL and click go!",20,80);

          // A help message to display when a bad URL has been typed.
          // In this case an URL without a ".com" domain.
          else 
          {
               g.drawString("Malformed URL: "+userUrl,20,80);
               g.drawString("This Applet only allows .com domains",20,100); 
          }
     }

     public void actionPerformed(ActionEvent act) 
     {
          // Start with good hope...
           UrlOnError = false;
          // Save the data of the textfield.
          String temp = urlField.getText();

          // If the URL is longer than 6 characters. 
          if (temp.length() > 6) 
          {
               // Then check if it starts with "http://"
               if (!temp.substring(0,7).toUpperCase().equals("HTTP://"))
                // if not then append it to it.
                temp = "Http://" + temp;
          }
          // if it's not longer than 6 chars then it will surely miss
          // the "http://" part. So we'll fix that.
          else temp = "Http://" + temp; 

          // Now that the Http:// is there we'll check if it's a .com URL
          // If the index of ".com" is -1 that means it isn't there.
          if (temp.indexOf(".com") == -1)
           // So we'll show an error message later
               UrlOnError = true;

          // Now it's time to transform the String to a real URL
          try 
          { 
           // This will do that.
               userUrl = new URL(temp);
          }
          // You MUST try-catch this method.
          // If it is still wrong then we'll show an error message too.
          catch (Exception e) 
          {
               UrlOnError = true;
          } 
          // Show the user what you have done with his typed in URL
          urlField.setText(userUrl.toString());

          // And finally load a new browser window and show the page.
          // If it was right of course.
           if (!UrlOnError) 
           // This will do so. Note that the "_blank" part can be 
           // replaced with "_self" or "_parent" like in HTML.
           // even frame targets are possible.
           getAppletContext().showDocument(userUrl,"_blank");
          // Now show the error message or nothing if it was right.
          repaint();
     }

}

/* Note that you only need 
      Url userUrl = new URL(String)
   and
      getAppletContext().showDocument(userUrl,"_blank");
   to do the trick. The rest is an example of checking user input.
*/

// Next is using Sounds, once again not hard at all.
 

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

The Pitfalls of Open Source Litigation
LiMo Open to Working With Google on Mobile
Google Gadgets Under Attack at Black Hat
IBM, Linux and the Microsoft-Free PC
Opengear's Open Source Odyssey
Sun Moves JavaFX Closer to Primetime
Will LSB 4 Standardize Linux?
Making a Case for an Android-Symbian Merger
Ubuntu Launchpad Opens Up Development
BT Buys Voice Software Startup

An Introduction to F# for Functional Programming
The Basics of REALbasic, Cross-platform RAD Tool in the Mold of VB
Silverlight Streaming: Free Video Hosting for All
What's New in ASP.NET 3.5?
Putting SharePoint to Work for You
Putting Intel® Threading Building Blocks to Work
Achieve the Best of Two Worlds with Behavior-Driven Development
Understanding Windows Mobile Security
RODCs Transform Branch Office Security
SQL Server 2008 Express: The Free Database from Microsoft

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers