Quick Java Questions

Previously this has been quite hard to do if you wanted your program to work on multiple platforms because you had to check which OS was running and determine what browser to launch but since Java 6 there has been a new class added to handle this. Here is how you can use it to launch a html document (or any URI).

Code:
URI address = null;

try {
	address = new URI("c:/index.htm");
} catch (URISyntaxException ex) {
	System.out.println("Invalid URI");
}
    
if (Desktop.isDesktopSupported()) {
try {
        Desktop.getDesktop().browse(address);
} catch (IOException ex) {
	System.out.println("Error launching browser");    
}
}
 
It should be:

Code:
import java.net.URI;
import java.net.URISyntaxException;
import java.io.IOException;
import java.awt.Desktop;

And remember you need to be using Java 6 (1.6).
 
You could save all that dicking around opening of external browsers and the like and use a JEditorPane to parse the HTML...
 
Well it depends on his requirements, a JEditorPane is not always suitable especially if it's just a "visit software homepage" button. It looks complicated but the above code is far easier than all the hoops you had to jump through before Java 6.
 
Well yes, but if it's just to display some HTML then a 'browser' can be written in a JEditorPane in about 10 lines of code.
 
Back
Top Bottom