• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

browser

any guidance to pop up a web browser when i click a hyperlink at JEditorPane?

Hope to get expert help..thanks..
0
tloip
Asked:
tloip
  • 20
  • 17
  • 15
  • +1
2 Solutions
 
TimYatesCommented:
http://javaalmanac.com/egs/javax.swing.event/Hyperlink.html

That won't pop up browser though

Hmmm...
0
 
CEHJCommented:
Runtime.getRuntime().exec("start " + link.getURL().toString());
0
 
CEHJCommented:
(where 'link' is really the event)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TimYatesCommented:
>  (where 'link' is really the event)

And you are on Windows ;-)
0
 
CEHJCommented:
>>And you are on Windows ;-)

Yes. (The probability is high ;-))
0
 
TimYatesCommented:
> Yes. (The probability is high ;-))

True, but you'll make us poor Linux users cry ;-)
0
 
CEHJCommented:
Producing a single solution across Windoze versions is hard enough, let alone cross-platform

>>us poor Linux users

What are you running?
0
 
TimYatesCommented:
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
 
CEHJCommented:
uname -a

;-)
0
 
tloipAuthor Commented:
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
 
tloipAuthor Commented:
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
 
tloipAuthor Commented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
>>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
 
tloipAuthor Commented:
Alright, i understand your explanation. Thanks.
0
 
tloipAuthor Commented:

(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
 
TimYatesCommented:
you're probably missing a space:

 Runtime.getRuntime().exec(command + " " + url);
0
 
TimYatesCommented:
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
 
tloipAuthor Commented:
>> 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
 
tloipAuthor Commented:
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
 
TimYatesCommented:
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
 
CEHJCommented:
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
 
TimYatesCommented:
Just found this:

http://browserlauncher.sourceforge.net/

Which looks quite cool :-)
0
 
CEHJCommented:
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
 
TimYatesCommented:
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
 
CEHJCommented:
>>and it seems to work on Linux

I wonder if it just does the same as my naive code example? ;-)
0
 
TimYatesCommented:
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
 
CEHJCommented:
Oh yes, was thinking start may only be a command interpreter command (blush ;-))
0
 
TimYatesCommented:
:-)
0
 
CEHJCommented:
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
 
tloipAuthor Commented:
>> 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
 
TimYatesCommented:
use that library, or the "cmd.exe /c" command above..
0
 
TimYatesCommented:
you missed out on 4 hours of us chatting about this ;-)
0
 
CEHJCommented:
>>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
 
tloipAuthor Commented:

Oh yea, missed out...since my connection got probelm..
0
 
tloipAuthor Commented:
wana catch  up now..:)
0
 
tloipAuthor Commented:
you means using this

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

how to use this?

0
 
tloipAuthor Commented:
Oh..i got it!!!
0
 
CEHJCommented:
Change the earlier

>>command = "start ";

to

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

0
 
CEHJCommented:
As i mentioned, that will fail on < Win NT
0
 
tloipAuthor Commented:
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
 
CEHJCommented:
>>Thus, should be no problem

start will fail on Win 98

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

No
0
 
CEHJCommented:
>>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
 
tloipAuthor Commented:
>> 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
 
CEHJCommented:
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
 
VenabiliCommented:
Question reopened per request from the awarded expert

Venabili
Java Page Editor
0
 
tloipAuthor Commented:
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
 
tloipAuthor Commented:
>> Question reopened per request from the awarded expert

What is the purpose of using it?
0
 
VenabiliCommented:
Now you can make the split.

Venabili
0
 
tloipAuthor Commented:
Oh...thanks!!!
0
 
CEHJCommented:
;-)
0
 
TimYatesCommented:
:-)  Glad you got it sorted :-)

And thanks CEHJ :-D  *bow*
0
 
CEHJCommented:
;-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 20
  • 17
  • 15
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now