Results 1 to 5 of 5
  1. #1
    Learnin is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default [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);
    	}
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    You can use the Desktop class to invoke the default browser of you computer.

  3. #3
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    here a sample code..

    Java Code:
    Desktop desktop=null;
    if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
    }
    if(desktop.isSupported(Desktop.Action.BROWSE))
        desktop.browse("URL");

  4. #4
    Learnin is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    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
    Java 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());
    		         }
    	   }
    Use eclipse to find what you need to import
    Probally JOptionPane

    If you want to use this in a command
    here:
    add this below your jbutton
    Java Code:
    	    Youtube.addActionListener(new ButtonListener());
    Then in your actionlistener
    Add this command
    Java Code:
    		    if (e.getActionCommand().equals("Youtube")) {
    		    	String url = "link here!";
    		    	Gui.openURL(url);
    		        System.out.println("Youtube Initiated!");
    		      }


    From
    Harvey Atwal
    Last edited by Learnin; 10-06-2009 at 04:43 PM.

  5. #5
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    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

  1. problem in opening new url from JSP
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-02-2009, 06:46 AM
  2. opening ports
    By prashant in forum Networking
    Replies: 2
    Last Post: 03-22-2009, 08:23 PM
  3. Opening in Java and .Net
    By techinvo in forum Jobs Offered
    Replies: 0
    Last Post: 03-22-2009, 07:24 AM
  4. opening of an exe in JFrame
    By smartsubroto in forum New To Java
    Replies: 3
    Last Post: 07-16-2008, 05:01 AM
  5. Opening a workspace twice
    By javaplus in forum Eclipse
    Replies: 0
    Last Post: 01-08-2008, 09:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •