A perfect java blog

Open Default Browser in Javafx

9 February,2011Narayan G. Maharjan

Hello I’ve seen many of the javafx programmer really likes the facilities of JAVA as everybody likes too. Javafx has capability to run the Java files by using JavaTaskBase, RunnableFuture (Interface). So today I like to present you guys, how can we open the default browser of system from javafx. Let’s begin.

Requirement:

Netbeans 6.5 or >

Javafx 1.2 or >

Ok Now let’s start with a new project from Netbeans. Start a new project of Java (I’m telling java not javafx). Then create a Java Project name as you want but at here I’m naming it as ‘Browser Starter’ with Java class name ‘BrowserStarter’. Here you have to add a very tiny Java code which opens the Default Browser of System .

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

packagebrowseropener;

import java.net.URI;

/**

*

* @author Narayan

*/

publicclassBrowserOpener{

/**

*

* @param url

*/

publicvoidopen(Stringurl)throwsException{

URIu=newURI(url);

java.awt.Desktop.getDesktop().browse(u);

}

}

Develop Java Jar file

Now that’s all for the Java. You have to do some stuffs . Here we go. Go to properties of your java project and do something like below picture . It will compress your project in jar form then click OK and press “Ctrl F11” for clean build of your project. Now you will see the jar file being made by Netbeans under <project_directory>/dist . Then copy that jar into specific place where you can use it easily for javafx later.

Create new Javafx project

Now let’s make a new javafx project from Netbeans As shown in figure below. Here we have some tricks which helps to import the jar file we’ve created just before ‘BrowserStarter.jar’ into javafx project.

Creating Library in Netbeans

In this step we are going to create a library so that Netbeans would easily get the java jar file i.e. ‘BrowserStarter.jar’ worked in our javafx project.Let’s see how it works. Go to Project Properties as shown in below.

Now click on Library Category > Add Library. The dialog would appear on window which looks something like this.

Now goto Manage Libraries > New Library again there would come a dialog like below. Here I named the Library Name as ‘BrowserOpener’ and clicked OK.

Ok now it’s time for adding jar files to our Library ‘BrowserOpener’ . Click on Add Jar Folders and specify location where you have kept the Java Project jar file i.e. BrowserStarter.jar

Now you can see our custom Library available in Netbeans Library. Simply click Add Library and it will add the library to your javafx project . Now it’s almost done. You can use this library any time you like in javafx project. Let’s see how javafx works our library.(Create a main class file if not exist) I’ve a main file named ‘Main.fx’ in my Javafx project and now i’m going to implement my library to javafx.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

packagebrowsertest;

import javafx.stage.Stage;

import javafx.scene.Scene;

import browseropener.BrowserOpener;

import javafx.scene.control.Button;

import javafx.scene.control.TextBox;

import javafx.scene.layout.HBox;

/**

* @author WEBNEP

*/

def textBox=TextBox{

text:"http://google.com"

columns:12

selectOnFocus:true

};

Stage{

title:"Application title"

scene:Scene{

width:250

height:80

content:[

HBox{

content:[textBox,

Button{

text:"Button"

action:function(){

varopener:BrowserOpener;

opener=newBrowserOpener();

opener.open(textBox.text);

}

}]

}

]

}

}

It’s time to compile and run our application. Please run the project by pressing ‘Shift+ F6’ and now there must be application window like below. Click on Button and you ‘ll be able to browse the url on System default browser.

Thanks for watching this blog.
Please don’t hesitate to comment. 🙂 Good day