Navigating from an Applet
[Home] [Java] [HTML] [Tabs] [About Me] [Disclaimer] [Contact]
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Sorry ! Your browser doesn't support Java applets

 

This is to show you in the most basic way , how to open new browser windows using applets . I'm going to refrain from using image menus ( navigation using images ) , instead I'll simply stick to regular buttons to keep it simple . For comments & suggestions or just about any other crap - mail me .

 

I'll assume that you know how to add buttons to applets & you probably have been able to add applets to web pages . We can make  applets function as navigation menu's . To do so you just need to know a few factory methods & how to piece them together .


For most of the browsers there are two areas you can access -

  • The status line - ( of limited use , since the browser also uses it !)
  • And the display area of the web page

The status line can be used for dumb messages like loading data , loading class files , loading images etc . Do not show important messages in the status line 'cause most users don't bother to see there & the browser is likely to overwrite your important message .

And then there is this other area ( the web page itself ) which the applet can access & effect . This is going to be our target for this session .


For our applet to tell the browser what to do , we will need an object that implements the AppletContext interface . Think of this as a communications bridge between the browser & the applet . To do this we should call the getAppletContext ( ) method from our applet . This method returns an object that implements the AppletContext interface . Remember that AppletContext is an  interface & so can't be instantiated .

The relevant code would be -

	URL u = new URL("http://www.oocities.org");
	AppletContext a = getAppletContext();
  • Line 1 we create an URL object for the page we'd like to show
  • Line 2 we get a reference to the applet's current environment

To show the document all we need to do is run a method on an object that implements the AppletContext interface . Here's how -

	a.showDocument(u);
	//u is an URL object

Note -

  • This will open the URL in the current window
  • The showDocument ( ) method can also take parameters that define where to open the URL ( new window , separate frame , frame marked as particular name , etc )
  • Check the documentation . You will find it here java.applet interface AppletContext

The next step - dumb - if you understand event handling . All you need to do is , when an action is performed , ask the applet to go to a particular page . Here's how -

	public void actionPerformed(ActionEvent ae){
		try
		{
			URL u = new URL("http://www.oocities.org");
			AppletContext a = getAppletContext();
			a.showDocument(u);
		}
		catch (MalformedURLException e){
			//do noting
		}
	}

The three buttons to your right are actually in an applet . To add multiple buttons all you need is some logic . Here's how I add 3 buttons ( google , yahoo , javaranch ) to open the relevant pages -

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;//required for the URL class

/*
<applet code="LinkTest" width=60 height=100>
</applet>
*/

public class LinkTest extends Applet
	implements ActionListener{

	public void init(){
		String [] link_buttons = { "yahoo" , "google" , "javaranch"};
		Color [] c = { Color.yellow , Color.red , Color.orange};
		getParent().setBackground(Color.white);

		//the benefits of for
		//note the Color & String arrays
		//defined up there
		for ( int i = 0 ; i < 3 ; i++ ){
			Button b = new Button(link_buttons[i]);
			b.addActionListener(this);
			b.setBackground(c[i]);
			add(b);
		}
	}

	public void actionPerformed(ActionEvent ae){

		//get the button label
		Button source = (Button)ae.getSource();

		//make a string representation
		//of the URL you wanna hit
		//A little foresight & the labels of the
		//buttons help us complete the URL string
		String link = "http://www."+source.getLabel()+".com";
		try
		{
			AppletContext a = getAppletContext();
			URL u = new URL(link);
			a.showDocument(u,"_blank");
			//_blank to open page in new window		
		}
		catch (MalformedURLException e){
			//do nothin
		}
	}
};

 


Take your time with the above code & mess around with it . There is nothing to it besides factory methods .

If this tutorial didn't explain the concept clearly , let me know . I'll come up with better wordings & detailed explanations . I'm too stoned right now to go any further :-) . Peace !

 


 

 

 

[Home] [Java] [HTML] [Tabs] [About Me] [Disclaimer] [Contact]