0

Possible Duplicate:
Getting java gui to open a webpage in web browser - This is totally not a duplicate. I want to be able to reference a tab in the browser not just launch the browser.

I have a desktop application that has a button with an action that opens up a new browser with a specific URL. Every time I press the button it opens up a new window/tab. I want it to refresh the window/tab, if it is already open and if the URL is the same, instead of always opening a new one. The code to launch the initial window is pretty straight forward:

final String errMsg = "Error attempting to launch web browser";
String osName = System.getProperty("os.name"); 
try{ 
    if (osName.startsWith("Windows")) 
        Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler " + "www.stackoverflow.com");
}catch(Exception e){
    JOptionPane.showMessageDialog(null, errMsg + "\n" + e.toString());
}

I was thinking maybe I could name the window/tab so when the code is launched it tries to find the name and refresh it if exists. But I am having troubles finding a solution like that. Additionally, is the way I am currently doing it acceptable?

Community
  • 1
  • 1
northpole
  • 10,244
  • 7
  • 35
  • 58
  • @plasma147 - I should mention I am using Java 5, so that solution won't work for me. – northpole Jun 20 '12 at 18:43
  • @plasma147 It's not an exact dup, the OP wants the same browser tab to be refreshed every time it's launched from the Java GUI app. – Michael Jun 20 '12 at 19:01
  • 2
    @plasma147 By the way I love tardigrades too, any animal that can survive in space gets my vote. :D – Michael Jun 20 '12 at 19:12

2 Answers2

0

Add a webserver component to your application. Then start a browser pointing to a page generated by your webserver and use AJAX to refresh browser content upon button click.

Andy
  • 1,618
  • 11
  • 13
0

You could use this as a basis :

Getting java gui to open a webpage in web browser

to open a browser in a non-os specific way.

It probably doesn't give you that level of control over a browser.

Have you considered using JeditorPane or if you need extra javascript support possible including a javafx webview (which is based on webkit): Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application

Community
  • 1
  • 1
plasma147
  • 2,191
  • 21
  • 35
  • Ah, I guess javafx integration won't work with java s.e 6. JEditorPane would work - you could download the html and render it as you want. The renderer isn't great on it though – plasma147 Jun 20 '12 at 18:53
  • I am not sure rendering it myself will meet the requirements. I wish we were already Java 6 but we are still a year or so away from moving to that :/ – northpole Jun 20 '12 at 18:59