Java Applet With Web Access Example
A simple applet embeded within a HTML page that once clicked will open selected URL.
------------------------------------CODE----------------------------------
start.html
<html>
<head>
</head>
<applet code="start.class" width="500" Height="250">
<param name="title0" value="Google">
<param name="address0" value="http://www.Google.com">
<param name="title1" value="Yahoo">
<param name="address1" value="http://www.yahoo.com">
</applet>
<body>
</body>
</html>
----------------------------------------------------------------------
start.java
import java.net.*;
import java.util.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.event.*;
public class start extends JApplet
{
private HashMap<String,URL> websiteInfo;
private ArrayList<String> titles;
private JList mainList;
public void init()
{
websiteInfo = new HashMap<String,URL>();
titles = new ArrayList<String>();
grabHtmlInfo();
add(new JLabel("What website do you want to visit"),BorderLayout.NORTH);
mainList= new JList(titles.toArray());
mainList.addListSelectionListener
(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
Object object = mainList.getSelectedValue();
URL newDocument = websiteInfo.get(object);
AppletContext browser = getAppletContext();
browser.showDocument(newDocument);
}
}
);
add(new JScrollPane(mainList),BorderLayout.CENTER);
}
public void grabHtmlInfo()
{
String title;
String address;
URL url;
int counter=0;
title=getParameter("title"+counter);
while(title!=null)
{
address=getParameter("address"+counter);
try
{
url = new URL(address);
websiteInfo.put(title,url);
titles.add(title);
}
catch(MalformedURLException urlException )
{
urlException.printStackTrace();
}
++counter;
title=getParameter("title"+counter);
}
}
}
-------------------------------OUTPUT-----------------------------------
No comments:
Post a Comment