Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT opening internet browser in java program
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


omkar
opening internet browser in java program
1 Answer(s)      5 years and 9 months ago
Posted in : Swing AWT



Advertisement
View Answers

April 4, 2009 at 12:30 PM


Hi Omkar,

Use the following code:

1) 'OpenBrowser.java'
import javax.swing.*;
import java.lang.reflect.Method;

public class OpenBrowser {
public static void openURL(String url) {
String osName = System.getProperty("os.name");
try {
if (osName.startsWith("Windows"))
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
else {
String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
if (Runtime.getRuntime().exec(new String[] {"which", browsers[count]}).waitFor() == 0)
browser = browsers[count];
Runtime.getRuntime().exec(new String[] {browser, url});
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage());
}
}
}

2) 'Browser.java'
import javax.swing.*;
import java.awt.event.*;

public class Browser {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
final JTextField url =new JTextField(20);
JButton button = new JButton("Open Browser");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OpenBrowser.openURL(url.getText().trim()); }
} );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(new JLabel("URL:"));
panel.add(url);
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}

}

Thanks


Related Tutorials/Questions & Answers:
opening internet browser in java program - Swing AWT
opening internet browser in java program  i want a code to open the internet browser in swing program using JFrame.  Hi Omkar, Use...) { JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage
Opening a browser only with body from a java program
Opening a browser only with body from a java program  Hi all, I got following requirement, Opening a browser window only with body and title bar. And based on some condition, after some time, i have to close the browser window
Advertisements
opening new browser with new JSESSIONID using java
opening new browser with new JSESSIONID using java  I am facing following problem, I am trying to open a new browser using java. First i have opened one IE browser and manually. And i ran my LaunchURL.java file, it is opening new
Opening files with .jnlp extension on browser!?!?!
Opening files with .jnlp extension on browser!?!?!  I working... for Runtime.getRuntime().exec("..........."); I came across the java code.... Rather i want to open it in my browser(Mozilla/IE) itself. Kindly provide me
Delete Internet Explorer History in java program
Delete Internet Explorer History in java program  code for Delete Internet Explorer History in java program
Java - Opening a URL from an Applet
Java - Opening a URL from an Applet  ... document or browser window. In this program you will see that many features have been used for opening url from an applet. This program is using two functions
Java - Opening a url in new window from an applet
Java - Opening a url in new window from an applet       This is the example of opening a url from an applet. This program shows that how a url is opened in a new document or browser
Opening IE from Java Swing Application in Kiosk mode
Opening IE from Java Swing Application in Kiosk mode  Hi All, I want...) { String browserPath = "C:/Program Files/Internet Explorer...(); } } }   Here is a simple example of opening browser
Help with opening a text file upon closing a program.
Help with opening a text file upon closing a program.  I need some helping in figuring out something I want to do. I have this program... that file to open whenever the user exits the program. My code so far
opening new window - Java Beginners
opening new window  Hi All, I have two classes with me , lets say A and B.when I press a button in class A , the class B should open in a new window.i.e., I want to know, how a new window(B) can be opened by clicking a button
Java for Internet Explorer
browser as Internet Explorer 7. To enable or disable Java in Internet Explorer 4... Java for Internet Explorer      ... and select Internet Options. Click on the Programs tab. Click Manage Add-ons
Error Opening File - Java Interview Questions
Error Opening File  Dear, Please if help me to resolve this problem. I have an application in java that works correctly under Eclipse... this generated file, a message was displayed: Error opening file. But If I
problem in swing program for opening two windows with same login credentials
problem in swing program for opening two windows with same login credentials  I Face two problems while writing the code in swing program 1.i developed one application using swings that has username,password when i login
Java Web Browser Problem
Java Web Browser Problem  Hello When run my web browser...."); setTitle("NextVision Browser"); setSize(600,400); Container...); objMenu.add(objMenuExit); JMenuItem Internet=new JMenuItem
finding java code on internet
finding java code on internet  Is there anybody to help me? i want to some java coding web sites.it is prerequest for me
Browser making by java coding
Browser making by java coding  import javax.swing.*; import...*; import java.io.*; public class Browser extends JFrame implements...[]) { if(args.length==0) new Browser ("http:google.com"#; else new Browser#args#0
JAVA SCRIPT CODE FOR OPENING A WEBSITE IN A NEW WINDOW - Java Beginners
JAVA SCRIPT CODE FOR OPENING A WEBSITE IN A NEW WINDOW   PLS TELL ME? HOW CAN I DEFINE A WINDOW.OPEN FUNCTION WITHIN ANOTHER FUNCTION e.g IF THERE ARE THREE FIELDS OF A FORM, AND AFTER CKECKING THAT ALL THE FIELDS HAVING
Toolbar to a browser - Java Beginners
to display toolbar on the browser? Thanks  I want to create a website's... browser, some site ask us to download the application and install it. so i wanted
browser
browser  how to make a browser
Opening Notepad
Opening Notepad  Sir/madam I want to open notepad from java applications and save the contents on it. Can someone please help me to do...}; Runtime.getRuntime().exec(commands); }   Java Create
Java Swing Open Browser
Java Swing Open Browser Java provides different methods to develop useful... that will open the specified url on the browser using java swing. This example will really...) { JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n
browser
in the swing because I am working on my project Browser and one more problem is that when I entered the URL then some page open which made in java and the page
java code to get the internet explorer version
java code to get the internet explorer version  Hi, Could u plz help me in finding the internet explorer version using java code...i have the javascript but i want this in java code... Thanks, Pawan
Applet in Java
Applet is a Java program designed for execution within the web browser. They are embedded into HTML pages and run on Java enabled web browsers like Mozilla... the Internet and run them. An Applet is a Java subclass that extends
Browser tire - Java Interview Questions
Browser tire  If we use multithreading in my application then which tire is needed on clients browser so that application run successfully? Plz help me ass soon as possible
browser
browser  how to add menu items in the menu in java swing through java coding
Cross platform browser question - Java Beginners
Cross platform browser question  Can you tell me what this is specifially asking for? I have my program that works fine, but the instructor said...-browser (works in Firefox and MS IE) web page using anonymous JS methods
Embed Default Browser within Java Application
Embed Default Browser within Java Application  After some googling i... to embed browser with or without using Netbeans I ve already tried this 2 minutes Browser But i m getting the error in netbeans as : isDefaultBrowserMozilla
How to download and save a file from Internet using Java?
How to download and save a file from Internet using Java?  How to download and save a file from Internet using Java
Java - Opening a url in new window from an applet
Java - Opening a url in new window from an applet  ... in this program to open any url by replacing the value of url. In this example our applet will open new browser window and then show you the web page. Opening a new
detecting browser time zone - Java Interview Questions
detecting browser time zone  Hi! Is it possible to detect the user browser time zone that means his country. if possible please let me know how to do it... thanks in advance
Advertisements
 

 

 

DMCA.com