Results 1 to 1 of 1
Thread: Manipulating URLs
- 08-03-2009, 05:01 PM #1
Manipulating URLs
This is an example of manipulating urls. it is supposed to display a list of urls and when i select one of them, it displays this site. but the list appears empty when i run the applet. plz don't tell me that the links u put in the html document are invalid, no i used valid links but i can't put links here as i 'm a new member.:(
HTML document to load SiteSelector applet.
Java Code:<html> <title>Site Selector</title> <body> <applet code = "SiteSelector.class" width = "300" height = "75"> <param name = "title0" value = "Java Home Page"> <param name = "location0" value = "valid link"> <param name = "title1" value = "Deitel"> <param name = "location1" value = "valid link"> <param name = "title2" value = "JGuru"> <param name = "location2" value = "valid link"> <param name = "title3" value = "JavaWorld"> <param name = "location3" value = "valid link"> </applet> </body> </html>
Loading a document from a URL into a browser
Java Code:import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.ArrayList; import java.awt.BorderLayout; import java.applet.AppletContext; import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class SiteSelector extends JApplet { private HashMap< Object, URL > sites; // site names and URLs private ArrayList< String > siteNames; // site names private JList siteChooser; // list of sites to choose from // read HTML parameters and set up GUI public void init() { sites = new HashMap< Object, URL >(); // create HashMap siteNames = new ArrayList< String >(); // create ArrayList // obtain parameters from HTML document getSitesFromHTMLParameters(); // create GUI components and layout interface add( new JLabel( "Choose a site to browse" ), BorderLayout.NORTH ); siteChooser = new JList( siteNames.toArray() ); // populate JList siteChooser.addListSelectionListener( new ListSelectionListener() // anonymous inner class { // go to site user selected public void valueChanged( ListSelectionEvent event ) { // get selected site name Object object = siteChooser.getSelectedValue(); // use site name to locate corresponding URL URL newDocument = sites.get( object ); // get applet container AppletContext browser = getAppletContext(); // tell applet container to change pages browser.showDocument( newDocument ); } // end method valueChanged } // end anonymous inner class ); // end call to addListSelectionListener add( new JScrollPane( siteChooser ), BorderLayout.CENTER ); } // end method init // obtain parameters from HTML document private void getSitesFromHTMLParameters() { String title; // site title String location; // location of site URL url; // URL of location int counter = 0; // count number of sites title = getParameter( "title" + counter ); // get first site title // loop until no more parameters in HTML document while ( title != null ) { // obtain site location location = getParameter( "location" + counter ); try // place title/URL in HashMap and title in ArrayList { url = new URL( location ); // convert location to URL sites.put( title, url ); // put title/URL in HashMap siteNames.add( title ); // put title in ArrayList } // end try catch ( MalformedURLException urlException ) { urlException.printStackTrace(); } // end catch counter++; title = getParameter( "title" + counter ); // get next site title } // end while } // end method getSitesFromHTMLParameters } // end class SiteSelector
Similar Threads
-
Web Spider - Extract URLS
By heveen in forum NetworkingReplies: 2Last Post: 07-09-2009, 01:15 PM -
Manipulating XML
By JosephMConcepcion in forum XMLReplies: 2Last Post: 04-26-2009, 12:01 AM -
Manipulating String Tokenizer
By Bomber_Will in forum New To JavaReplies: 2Last Post: 04-19-2009, 11:54 PM -
getting URLs
By Shiv in forum NetworkingReplies: 3Last Post: 04-16-2009, 05:48 PM -
manipulating a string (Urgent)
By ariz in forum New To JavaReplies: 4Last Post: 03-31-2009, 05:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks