Results 1 to 4 of 4
  1. #1
    zacchia is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default dropping a URL: how to get page title?

    Hello,
    I'm dropping the URL from the address field of the browser into a Java application.
    Using drag and drop I can have the URL, but I would also like to have the title of the page.
    How can I do it?

    For example, when you drop the address on a desktop, you get a shortcut to the webpage and the shortcut displays the name of the web page (instead of the http address).

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Probably have to use the URL to read the HTML page and parse out the title.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    This isn't a Swing problem.

    What you get as a result of the drag and drop is a String. What you need to do is construct the corresponding URL and then connect to the appropriate server and ask it what the title is (providing there is one: ie you are assuming it refers to an html page and has a <title>).

    Check out the Custom Networking section of Oracle's Tutorial.

    Having got the contents of the web page you are left with the task of parsing it to get the title. Swing does have HTML parsing abilities that you could use. Google turned up the following article discussing their use: Parsing HTML with Swing.

    Since that time a *lot* of 3rd party libaries are been developed to parse HTML - google will find them.

    It's a bit yucky but you could roll your own since you are only interested in the first part of the document (up to </head>): look for "<title ?>" and "</title ?/>" and the title will be whatever is in between.

  4. #4
    zacchia is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks,
    since the drag and drop returns about 50 flavors, URL among them, I was hoping there were some flavors that was returning info about the title as well, without the need to download and parse the html file.

    I used regex. Would the HTML parser give different results or would it be more efficient?

    Java Code:
    	  String getPageTitle(URL url) throws Exception {
    
    
    		    BufferedReader in = new BufferedReader(
    					new InputStreamReader(url.openStream()));
    
    		    Pattern pHead = Pattern.compile("(?i)</HEAD>");
    		    Matcher mHead;
    		    Pattern pTitle = Pattern.compile("(?i)</TITLE>");
    		    Matcher mTitle;
    		    
    		    String inputLine;
    		    boolean found=false;
    		    boolean notFound=false;
    		    String html = "";
    		    String title=new String();
    		    try{
    			    while (!(((inputLine = in.readLine()) == null) || found || notFound)){
    			    	html=html+inputLine;
    			    	mHead=pHead.matcher(inputLine);
    			    	if(mHead.find()){
    			    		notFound=true;
    			    		}
    			    	else{
    			    		mTitle=pTitle.matcher(inputLine);
    			    		if(mTitle.find()){
    			    			found=true;
    			    			//System.out.println(inputLine);
    			    		}
    			    	}	    					    		
    			    }
    			    in.close();
    		   
    			    html = html.replaceAll("\\s+", " ");
    			    if(found){
    				    Pattern p = Pattern.compile("(?i)<TITLE.*?>(.*?)</TITLE>");
    				    Matcher m = p.matcher(html);		    
    				    while (m.find() == true) {
    				    	title=m.group(1);
    				      //System.out.println("Title "+title); 
    				    }
    			    }
    		    }catch(Exception e){
    		    }
    		    return title;
    		  }

Similar Threads

  1. How to rename GUI Title
    By rjagan in forum New To Java
    Replies: 5
    Last Post: 06-11-2014, 11:51 AM
  2. Replies: 19
    Last Post: 06-10-2011, 07:58 PM
  3. Change the title
    By AndrewSD in forum NetBeans
    Replies: 1
    Last Post: 01-11-2011, 03:23 AM
  4. set * at tab title
    By keffie91 in forum Advanced Java
    Replies: 7
    Last Post: 10-06-2008, 07:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •