Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code (snippet) might look like this:

That should do the trick, modifying the browser location and url variables as necessary.
 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanks very much peoples my doubt was clarified
Tanks...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic