Solved

browser

Posted on 2004-10-26
487 Views
Last Modified: 2010-07-27
any guidance to pop up a web browser when i click a hyperlink at JEditorPane?

Hope to get expert help..thanks..
0
Question by:tloip
    54 Comments
     
    LVL 35

    Expert Comment

    by:TimYates
    http://javaalmanac.com/egs/javax.swing.event/Hyperlink.html

    That won't pop up browser though

    Hmmm...
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Runtime.getRuntime().exec("start " + link.getURL().toString());
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    (where 'link' is really the event)
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    >  (where 'link' is really the event)

    And you are on Windows ;-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>And you are on Windows ;-)

    Yes. (The probability is high ;-))
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    > Yes. (The probability is high ;-))

    True, but you'll make us poor Linux users cry ;-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Producing a single solution across Windoze versions is hard enough, let alone cross-platform

    >>us poor Linux users

    What are you running?
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    I'm at work, so it's RedHat linux of some variety (probably Enterprise)...

    I've got Windows at home though ;-)  I was just being cheeky ;-) hehehe
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    uname -a

    ;-)
    0
     

    Author Comment

    by:tloip
    Oh, currently my program using the code that almost same as in this link
    http://javaalmanac.com/egs/javax.swing.event/Hyperlink.html
    but i want to work it to pop up browser.

    I would like to ask, following code used to pop up web browser?
    Runtime.getRuntime().exec("start " + link.getURL().toString());

    If yes so, is it possible for me to place within "try" part??
       
        class MyHyperlinkListener implements HyperlinkListener {
            public void hyperlinkUpdate(HyperlinkEvent evt) {
                if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    JEditorPane pane = (JEditorPane)evt.getSource();
                    try {
                        // Show the new page in the editor pane.
                        pane.setPage(evt.getURL());
          >>>>  Runtime.getRuntime().exec("start " + link.getURL().toString());
                    } catch (IOException e) {
                    }
                }
            }
        }
    0
     

    Author Comment

    by:tloip
    opss..i'm so confuse with the comment about you people discussing about either using window or linux. haha..

    after i go through this link,
    http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21138518.html

    I'm trying to guess that it is different to pop up a browser when running under window or linux. Is that right? Or there is other meaning??  
    0
     

    Author Comment

    by:tloip
    For me, i need to run this at both platform. Thus, is it i need to follow the guidance in this follwing link:

    http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21138518.

    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>If yes so, is it possible for me to place within "try" part??

    Yes, just do

    Runtime.getRuntime().exec("start " + evt.getURL().toString());

    Linux itself is not a homogeneous platform. There is no certaintly what browser is going to be installed

       
    0
     
    LVL 86

    Assisted Solution

    by:CEHJ
    >>There is no certaintly what browser is going to be installed

    =

    There is no certainty as to what browser is going to be installed. You can end up writing *a lot* of code and still not have this bulletproof. A useful start may be the following:

    String command = null;
    String url = evt.getURL().toString();
    if (File.separatorChar == '/') {
      command = "/usr/bin/netscape ";
    }
    else {
      command = "start ";
    }
    Runtime.getRuntime().exec(command + url);
    0
     

    Author Comment

    by:tloip
    Alright, i understand your explanation. Thanks.
    0
     

    Author Comment

    by:tloip

    (running in window) when i clicked the link in the JEditorPane, it doesn't pop up a browser  but error occured. What is the matter? Am i placing the code in wrong place?


                    try {
              String command = null;
           String url = e.getURL().toString();
            if(File.separatorChar == '/'){
            command ="/usr/bin/netscape";
            }else{
            command = "start";
            }
            Runtime.getRuntime().exec(command + url);
                    }
                    catch (Exception ev){
                          ev.printStackTrace();
           
                    }
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    you're probably missing a space:

     Runtime.getRuntime().exec(command + " " + url);
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    Also, you might want to add a properties file to your application, so Linux users can set the location of their browser of choice...  :-)
    0
     

    Author Comment

    by:tloip
    >> add a properties file

    For my case, the url doesn't fixed because my program allow client
    to create a hyperlink by using html code their own.For example, client type:

    <a href="http://www.yahoo.com">GO</a>

    Then, a link, GO, created in JEditorPane.
    When the link clicked, yahoo page will displayed on a pop up browser.

    0
     

    Author Comment

    by:tloip
    I have added space on it but result still the same.
    This my actual code. Am i placing the correct place???

        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                sourcePane = (JEditorPane) e.getSource();
          if (e instanceof HTMLFrameHyperlinkEvent) {
                    HTMLFrameHyperlinkEvent event = (HTMLFrameHyperlinkEvent) e;
          System.out.println(event.getTarget());
                   HTMLDocument doc = (HTMLDocument) sourcePane.getDocument();
          doc.processHTMLFrameHyperlinkEvent(event);
          }
                else {
                    try {
              String command = null;
           String url = e.getURL().toString();
            if(File.separatorChar == '/'){
            command ="/usr/bin/netscape";
            }else{
            command = "start";
            }
            Runtime.getRuntime().exec(command + " " + url);
                    }
                    catch (Exception ev){
                          ev.printStackTrace();
           
                    }
                      }
                }
          }
          
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    Change

           Runtime.getRuntime().exec(command + " " + url);
     
    to

           String cmd = command + " " + url ;
           System.out.println( "Executing '" + cmd + "'" ) ;
           Runtime.getRuntime().exec( cmd );
     
    what does it print out?

    > For my case, the url doesn't fixed because my program allow client
    > to create a hyperlink by using html code their own.For example, client type:

    Yeah, I meant a properties file to define the:

           command ="/usr/bin/netscape";

    bit ;-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Look at the difference between my code and yours (in that order)

    >>command = "start ";

    >>command = "start";

    You can also try

    command = "C:/Program Files/Internet Explorer/IExplore.exe ";
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    Just found this:

    http://browserlauncher.sourceforge.net/

    Which looks quite cool :-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Yes. Kinda knew about that, but didn't mention it as it seems a bit over the top. Still, may be worth looking at
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    It's just one class...and it seems to work on Linux (well, it launches netscape, which isn't my main browser I use -- FireFox -- but at least it's a browser) ;-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>and it seems to work on Linux

    I wonder if it just does the same as my naive code example? ;-)
    0
     
    LVL 35

    Accepted Solution

    by:
    Basically, yes...  But there's LOADS of code for handling Macs in there...  

    And the windows call seems to run:

        cmd.exe /c start "" "http://www.yoururl.com"

    on Windows NT, and

        cmd.exe /c start "http://www.yoururl.com"

    without the "" parameter for Win 98/95 :-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Oh yes, was thinking start may only be a command interpreter command (blush ;-))
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    :-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Personally, if you want a short version, my second Windows suggestion (iexplore) should work on < NT (i.e. 98, 95) as well.

    cmd.exe /c start "" "http://www.yoururl.com"

    definitely won't, but i guess the launcher boys have coded for < NT somewhere
    0
     

    Author Comment

    by:tloip
    >> command = "C:/Program Files/Internet Explorer/IExplore.exe ";
    After i placed this at my try part, it able to pop up a browser and able to display the page i want.

    but..

    I was wondering, how if IExplore.exe not detected at C drive at another PC?
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    use that library, or the "cmd.exe /c" command above..
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    you missed out on 4 hours of us chatting about this ;-)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>I was wondering, how if IExplore.exe not detected at C drive at another PC?

    Well you've hit on another problem. The registry would possibly have to be read for this whole thing to be solid. I must say though - how likely is it that it won't be installed on that drive, especially (and M$ loves to claim this) since it's pretty much integrated into the OS?
    0
     

    Author Comment

    by:tloip

    Oh yea, missed out...since my connection got probelm..
    0
     

    Author Comment

    by:tloip
    wana catch  up now..:)
    0
     

    Author Comment

    by:tloip
    you means using this

    >>cmd.exe /c start "" "http://www.yoururl.com"

    how to use this?

    0
     

    Author Comment

    by:tloip
    Oh..i got it!!!
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Change the earlier

    >>command = "start ";

    to

    >>command = "cmd.exe /C start ";

    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    As i mentioned, that will fail on < Win NT
    0
     

    Author Comment

    by:tloip
    great!

    >>As i mentioned, that will fail on < Win NT
    em...alright. I think my program might only be used in window XP/98 and Linux.
    Thus, should be no problem


    this code being called for Linux OS??
     command ="/usr/bin/netscape";

    Do i need to do same like " cmd.exe "  for linux ?
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>Thus, should be no problem

    start will fail on Win 98

    >>Do i need to do same like " cmd.exe "  for linux ?

    No
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>start will fail on Win 98

    To be more precise, it's the combination of start and cmd.exe that will fail. I would recommend iexplore, and also testing the launcher program if you have difficulties
    0
     

    Author Comment

    by:tloip
    >> start will fail on Win 98
    main platform will be used is Linux. I might mention my program about start will fail on win98.

    >>I would recommend iexplore, and also testing the launcher program if you have difficulties
    Anyway, thanks for your recommend, i got it.

    Thanks for such a long long guidance!
    This is the best and clear guidance and fastest reply i ever received.
    Fantastic and excellent !!

    Your guidance is highly appreciated. Again, thanks! :-)

    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)

    Thanks, but i'd be happier if you gave Tim Yates some points too. He *did* mention my blunder over cmd.exe for one thing. I'll get the question reopened - you don't need to do anything for now.
    0
     
    LVL 20

    Expert Comment

    by:Venabili
    Question reopened per request from the awarded expert

    Venabili
    Java Page Editor
    0
     

    Author Comment

    by:tloip
    Frankly...you both are friendly and good guidance expert!
    Yea..i was wondering to give Tim Yates..but just not i clicked the "comment" too fast.
    I don't notice with the Split point..since i never did this before.
    Actually i just started often using EE recently even registered earlier. Thus, not so familiar with giving points, whatelse split point. haha..
    Is it possible to give point to T Y since i have given to you?


    0
     

    Author Comment

    by:tloip
    >> Question reopened per request from the awarded expert

    What is the purpose of using it?
    0
     
    LVL 20

    Expert Comment

    by:Venabili
    Now you can make the split.

    Venabili
    0
     

    Author Comment

    by:tloip
    Oh...thanks!!!
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    ;-)
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    :-)  Glad you got it sorted :-)

    And thanks CEHJ :-D  *bow*
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    ;-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now