wood burning stoves 2.0*
The moose likes Java in General and the fly likes How I put an event in the JButton to open my browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How I put an event in the JButton to open my browser" Watch "How I put an event in the JButton to open my browser" New topic
Author

How I put an event in the JButton to open my browser

DanielCosta Sobrinho
Greenhorn

Joined: Jun 06, 2003
Posts: 19
Hi
I have a programa in Java and i have a Jbutton, but I wanted that when i click in the button opened my broswer, and i dont know how i do...
Exemple:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class URLs extends JFrame
{
JButton botao;
URLs()
{
Container c = getContentPane();
c.setLayout(null);
botao = new JButton("Visitar");
botao.setBounds(10,50,150,30);
c.add(botao);
botao.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Acessado");
try
{
URL url = new URL ("http://www.google.com");
//here should have some thing and i dont kown...
}
catch(MalformedURLException mue)
{
System.out.println("URL mal formede exception");
}
}
});
}
public static void main (String args[])
{
URLs app = new URLs();
app.setSize(200,150);
app.setTitle("Clique e acessa o site");
app.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
app.show();
}
}
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Isn't opening a program (most likely native) a platform-dependant operation?
For example, in Windows you'd probably have to look up a registry setting to find the directory of the chosen browser and then execute the IExplore.exe file (if MSIE), passing the URL as the first command line parameter, for instance.
One way you could achieve this is by using a properties file for the user to configure the location of their browser program, and then run a System.exec() command. That way your code would be more platform-independant...
[ June 06, 2003: Message edited by: Charles Lyons ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
DanielCosta Sobrinho
Greenhorn

Joined: Jun 06, 2003
Posts: 19
the platform will be Windows, but the program will be installed in some computers and can be that the way of the InternetExplorer is different....
it will be that it gives pra to make this...
as it would be in my code above opening internetexplorer...
Thanks...
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
If some computers will be using different browsers than others, then a configurable properties file is definately what you need, and you'll need to enter the brower's executable location on each version of the file (on each machine).
URLs to be loaded are normally passed as the first argument on the command-line.
See the Properties class API for more information on creating property files.
DanielCosta Sobrinho
Greenhorn

Joined: Jun 06, 2003
Posts: 19
for example, if my code above had the InternetExplorer in c:/arquivos/IE.exe
as would be to open the IE.exe above passsando the URL???
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
The code (snippet) might look like this:

That should do the trick, modifying the browser location and url variables as necessary.
DanielCosta Sobrinho
Greenhorn

Joined: Jun 06, 2003
Posts: 19
Tanks very much peoples my doubt was clarified
Tanks...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here is a technique using rundll32. Give the name of a file and this launches the program associated with the file extension in Windows. You get the default browser for html, MS Word for doc, Excel for xls and so on. You don't have to make any attempt to know the actual exe that will execute.
This article makes things look more complicated than they are. Work through and figure out what the command string looks like when he's done. You can type the command right into a DOS command box to test it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How I put an event in the JButton to open my browser