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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs