Results 1 to 5 of 5
- 10-06-2009, 05:05 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
[HELP]JButton opening a browser and opening a website[HELP]
Im new to java
but I made my code so far but i need to make the buttons to open a url
I made this just to practice and help out my dad(who sucks at computers)
Heres my code..
Java Code:import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Gui extends JFrame { private JLabel label; private JLabel ebay; private JLabel craigs; private JLabel line; public Gui() { super("WebMasterX"); setLayout(new FlowLayout(FlowLayout.CENTER, 50, 5)); label = new JLabel ("Welcome to WebMasterX", JLabel.LEFT); label.setToolTipText("This will take you to your websites!"); label.setFont(new Font("Times New Roman", Font.BOLD, 16)); label.setForeground(Color.blue); add(label); ebay = new JLabel ("Ebay", JLabel.RIGHT); ebay.setFont(new Font("Times New Roman", Font.BOLD, 15)); ebay.setForeground(Color.black); add(ebay); JButton ebay1 = new JButton("America"); JButton ebay2 = new JButton("Canada"); add(ebay1); add(ebay2); craigs = new JLabel ("Craigslist", JLabel.CENTER); craigs.setFont(new Font("Times New Roman", Font.BOLD, 15)); craigs.setForeground(Color.black); add(craigs); JButton craig1 = new JButton("Vancouver"); JButton craig2 = new JButton("Seattle"); add(craig1); add(craig2); line = new JLabel ("-----------------------", JLabel.CENTER); line.setFont(new Font("Times New Roman", Font.BOLD, 15)); line.setForeground(Color.black); add(line); JButton Hotmail = new JButton("Hotmail"); JButton Youtube = new JButton("Youtube"); JButton Google = new JButton("Google"); add(Hotmail); add(Youtube); add(Google); } }
- 10-06-2009, 06:29 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
You can use the Desktop class to invoke the default browser of you computer.
- 10-06-2009, 10:39 AM #3
Member
- Join Date
- Sep 2009
- Location
- Italy, Turin
- Posts
- 39
- Rep Power
- 0
here a sample code..
Java Code:Desktop desktop=null; if (Desktop.isDesktopSupported()) { desktop = Desktop.getDesktop(); } if(desktop.isSupported(Desktop.Action.BROWSE)) desktop.browse("URL");
- 10-06-2009, 04:38 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
i just made a new url method
You guys can use it aswell... It works for me :D
Here is it:
It first checks if your using mac or Windows
Then it has a string of browsers for the openURL to open
If you dont have any of the browsers... It will give you an error
Use eclipse to find what you need to importJava Code:private static final String errMsg = "Error attempting to launch web browser"; public static void openURL(String url) { String osName = System.getProperty("os.name"); try { if (osName.startsWith("Mac OS")) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); } else if (osName.startsWith("Windows")) Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); else { //assume Unix or Linux 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]; if (browser == null) throw new Exception("Could not find web browser"); else Runtime.getRuntime().exec(new String[] {browser, url}); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error!:\n" + e.getLocalizedMessage()); } }
Probally JOptionPane
If you want to use this in a command
here:
add this below your jbutton
Then in your actionlistenerJava Code:Youtube.addActionListener(new ButtonListener());
Add this command
Java Code:if (e.getActionCommand().equals("Youtube")) { String url = "link here!"; Gui.openURL(url); System.out.println("Youtube Initiated!"); }
From
Harvey AtwalLast edited by Learnin; 10-06-2009 at 04:43 PM.
- 10-07-2009, 09:14 AM #5
Member
- Join Date
- Sep 2009
- Location
- Italy, Turin
- Posts
- 39
- Rep Power
- 0
Obviously Runtime.getRuntime().exec() works in opening file... but you had to do many checks (you wrote about 20 code lines).. Desktop class instead has those checks already inside his method desktop.browse().. so it's just an easier way... (your way is correct too)... the Desktop class has proved very useful in opening file with ANY extension... for example in my actual application, where i have to handle an "Attachment files list" and an associated "preview" button... it would be a suicide for me to check all the possibilities for any kind of file for every OS...... thanks Desktop class! :)
Similar Threads
-
problem in opening new url from JSP
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-02-2009, 06:46 AM -
opening ports
By prashant in forum NetworkingReplies: 2Last Post: 03-22-2009, 08:23 PM -
Opening in Java and .Net
By techinvo in forum Jobs OfferedReplies: 0Last Post: 03-22-2009, 07:24 AM -
opening of an exe in JFrame
By smartsubroto in forum New To JavaReplies: 3Last Post: 07-16-2008, 05:01 AM -
Opening a workspace twice
By javaplus in forum EclipseReplies: 0Last Post: 01-08-2008, 09:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks