Results 1 to 19 of 19
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Java application to inlude a link

    The application has a help button, which displays a help screen but I also want it to link to my help online at BMI Help Page Contents.

    Which is the best way of doing this, the code contained within the button so far is
    Java Code:
     if(evt.getSource()==cmdHelp);
                          optPane etc etc etc
    I want to add so that if they press help on the application that they can view the web page(s) as above.

    Which is the best way?

    Thanks in advance

  2. #2
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    Do you mean when they click on a link a web browser opens with the target url?
    If so, they're a number of ways to do this, but i found that the simplest way is the following:

    Java Code:
    	JLabel lblWebsiteLink = new JLabel("<html><u>" + "google" + "</u></html>");
    	lblWebsiteLink.addMouseListener(new MouseAdapter() {
    		public void mouseClicked(MouseEvent arg0) {
    			Desktop desktop = Desktop.getDesktop();
    			try {
    				if (desktop.isSupported(Desktop.Action.BROWSE)) {
    					URI uri = new URI("http:www.google.com");
    					desktop.browse(uri);
    					}
    				} catch (URISyntaxException e) {
    					
    					e.printStackTrace();
    				} catch (IOException e) {
    					
    					e.printStackTrace();
    			}
    
    		}
    	});
    This creates a JLabel, which when clicked opens the users default web browser with a target url of "http://www.google.com". Additional features include the label being underlined and the label can have a different text string to the actual link, so in this case the label is "google", but the actual target is "http://www.google.com".

    Hope this helps.
    They're 10 people in the world, those who speak binary, and those who dont.
    ^ Lame i know, but i can actually count in it nowadays! :eek:

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Netbeans 6.1

    Ok thanks for that, can you explain it a little bit

    such as what is the URI?

    I assume I have to decalre this within the other variables?

    I am working within netbeans so have just taken the following

    Java Code:
    Desktop desktop = Desktop.getDesktop();
    			try {
    				if (desktop.isSupported(Desktop.Action.BROWSE)) {
    					URI uri = new URI("http:www.google.com");
    					desktop.browse(uri);
    					}
    				} catch (URISyntaxException e) {
    					
    					e.printStackTrace();
    				} catch (IOException e) {
    					
    					e.printStackTrace();
    			}
    
    		}
    	});
    I have several errors but i think that may be down to braces and brackets but currently the errors are saying

    Java Code:
    cannot find symbol
    
    browse(java.net.URI) in java.awt.Desktop cannot be applied to (URI)
    
    cannot find symbol
    symbol class URISyntaxException
    
    cannot find symbol printStackTrace
    
    
    exception java.io.IOException is never thrown in body of corresponding try statement.
    From these errors am I looking at a little more than brackets and braces?

    Thanks in advance

  4. #4
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    Sorry, I forgot to show the imports.

    Here it is again, just copy and paste this and run it.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Desktop;
    import java.awt.EventQueue;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    
    
    public class LinkExample extends JFrame {
    
    	private JPanel m_contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					LinkExample frame = new LinkExample();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public LinkExample() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(200, 100);
    		m_contentPane = new JPanel();
    		m_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		m_contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(m_contentPane);
    		{
    			final String strgLabel = "google";
    			final String strgTarget = "http://www.google.com";
    				
    			//create underlined JLabel
    			JLabel lblWebsiteLink = new JLabel("<html><u>" + strgLabel + "</u></html>");
    				
    			//add action listener to detect mouse click
    			lblWebsiteLink.addMouseListener(new MouseAdapter() {
    				public void mouseClicked(MouseEvent arg0) {
    						
    					Desktop desktop = Desktop.getDesktop();
    					try {
    						if (desktop.isSupported(Desktop.Action.BROWSE)) {
    							URI uri = new URI(strgTarget);
    							desktop.browse(uri);
    							}
    					} catch (URISyntaxException e) {
    						e.printStackTrace();
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    
    				}
    			});
    			m_contentPane.add(lblWebsiteLink, BorderLayout.CENTER);
    		}
    	}
    
    }
    Here is another example of how to create a link using the same method:
    How to add hyperlink in JLabel - Stack Overflow

    I would suggest changing the mouse cursor when the mouse hovers over the JLabel, however you'l have to find out how to do that yourself. (Just add 2 events, one to change the cursor, another to change it back)
    Last edited by sirwiggles; 04-27-2009 at 06:55 PM.
    They're 10 people in the world, those who speak binary, and those who dont.
    ^ Lame i know, but i can actually count in it nowadays! :eek:

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    Thanks for the help, I may be pushing my luck here but I am using netbeans edi so have my form all designed within netbeans.

    I have the code in place but still have 1 error, the code causing the error is

    Java Code:
    m_contentPane.add(lblwebsiteLink, BorderLayout.CENTER);
    From my understanding about this line which isnt much but is this something that I need, I have tried deleting it but nothing happens so do I need something else in its place?

    The error at the side flags up the following.



    Thanks in advance.

    :D

  6. #6
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    argh, netbeans..
    All you need is the below imports..

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Desktop;
    import java.awt.EventQueue;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import javax.swing.JLabel;
    and this JLabel..

    Java Code:
    	final String strgLabel = "google";
    	final String strgTarget = "http://www.google.com";
    				
    	//create underlined JLabel
    	JLabel lblWebsiteLink = new JLabel("<html><u>" + strgLabel + "</u></html>");
    				
    	//add action listener to detect mouse click
    	lblWebsiteLink.addMouseListener(new MouseAdapter() {
    		public void mouseClicked(MouseEvent arg0) {
    						
    			Desktop desktop = Desktop.getDesktop();
    			try {
    				if (desktop.isSupported(Desktop.Action.BROWSE)) {
    					URI uri = new URI(strgTarget);
    					desktop.browse(uri);
    					}
    			} catch (URISyntaxException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    
    		}
    	});
    Simply add the imports to the class, then add the JLabel.

    The line that was throwing an error added the label to the frame, duno why it gave an error.
    If you have any more problems, screw you.
    .. nah, keep asking haha.
    They're 10 people in the world, those who speak binary, and those who dont.
    ^ Lame i know, but i can actually count in it nowadays! :eek:

  7. #7
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Thumbs up I will

    If you have any more problems, screw you.
    LOL, if only life was that simple.

    Thanks for all your help

    :D

  8. #8
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    The code that I pasted in

    Java Code:
    final String strgLabel = "BMI Online Help";
    	final String strgTarget = "http://www.ictfd.com/bmiassess3/helpmovies.html";
    
    	//create underlined JLabel
    	JLabel lblWebsiteLink = new JLabel("<html><u>" + strgLabel + "</u></html>");
    
    	//add action listener to detect mouse click
    	lblWebsiteLink.addMouseListener(new MouseAdapter() {
    		public void mouseClicked(MouseEvent arg0) {
    
    			Desktop desktop = Desktop.getDesktop();
    			try {
    				if (desktop.isSupported(Desktop.Action.BROWSE)) {
    					URI uri = new URI(strgTarget);
    					desktop.browse(uri);
    					}
    			} catch (URISyntaxException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    
    		}
    	});
    Has the system thinking about doing something but nothing does happen, everything validates fine, I get an icon at the side of

    Java Code:
    Add @override annotation
    I tried it but still nothing, I have to admit that I lost with this code but as I say the processor kicks in when I click on the button but it doesnt open a browser window????

    Thanks in advance

  9. #9
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    So it has no errors but when you click on the link nothing happens, no exceptions or anything?

    Add some prints above and beneath the desktop.browse(uri) line.

    If these appear fine.. I don't know whats wrong.

    The only thing (i guess) your lost with is the desktop object, just google it, I have no idea what it is either.. but i don't need to know, for me it works :P

    If your lost with anything else, you should try some other smaller projects, then come back to this one.

    Hope this helps.
    They're 10 people in the world, those who speak binary, and those who dont.
    ^ Lame i know, but i can actually count in it nowadays! :eek:

  10. #10
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Well thanks for your help, I am lost with more than the desktop but I will pick it up somewhere along the line.

  11. #11
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    I did as you suggested and put a few println in, I put 1 directly under the line with the url in, that worked

    I put 2 others where you suggested, neither worked.

    Any suggestions?

  12. #12
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    and no there are no errors at all apart from where it asks me to

    Add @override annotation

  13. #13
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    What OS are you running?
    Desktop may not be supported, thats why it is neither throwing an exception or opening an browser.

    As taken from:
    How to Integrate with the Desktop Class (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Other Swing Features)
    On the Solaris Operating System and the Linux platform, this API is dependent on Gnome libraries. If those libraries are unavailable, this method will return false.
    Add an else to the if statement, if the else is triggered (you will now if you add some prints inside it) then Desktop is not supported on your OS.
    They're 10 people in the world, those who speak binary, and those who dont.
    ^ Lame i know, but i can actually count in it nowadays! :eek:

  14. #14
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Running Vista

  15. #15
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question xp vista and java

    Through your comments abt os I decided to test some code I have just managed to find, my desktop runs XP and running the following code worked great.

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    
    public class hyperlink extends JFrame
    {
        public static void main(String arg[])throws Exception
        {
            new hyperlink();
        }
        public hyperlink() throws Exception
        {
            String s = "http://www.ictfd.com/bmiassess3/helpmovies.html";
            JEditorPane pane = new JEditorPane(s);;
            pane.setEditable(false);
            final JEditorPane finalpane = pane;
            pane.addHyperlinkListener(new HyperlinkListener()
            {
                public void hyperlinkUpdate(HyperlinkEvent r)
                {
                    try
                    {
                 if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                 finalpane.setPage(r.getURL());
                    }catch(Exception e)
                    {}
                }
            });
    
             setContentPane(new JScrollPane(pane));
             setSize(800,600);
             setVisible(true);
        }
    }


    However when I tried to run it on vista that has same jdk etc etc I got bucket loads of errors.

    The XP system opened the url within java.

    Can someone tell me why this would be?
    :confused::confused::confused:

  16. #16
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    vista sucks?... no idea.
    They're 10 people in the world, those who speak binary, and those who dont.
    ^ Lame i know, but i can actually count in it nowadays! :eek:

  17. #17
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Can you recheck whether you are using the same version of jdk in both vista and XP?

  18. #18
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Because version might make a difference at times.

  19. #19
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    the jdk on the xp pc is .1 off the version on vista,

    when run java -version
    javac -version same

    vista 1.06.0.07
    xp 1.06.0.06

Similar Threads

  1. jsf command link
    By Srikala in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 06-29-2010, 02:38 PM
  2. Link to specific part of a Java Servlet
    By jwilley44 in forum Java Servlet
    Replies: 2
    Last Post: 02-03-2009, 01:51 AM
  3. Link To Java Projects
    By mystified in forum New To Java
    Replies: 9
    Last Post: 09-24-2008, 11:01 AM
  4. Link for 64 bit JDK.
    By aparna in forum New To Java
    Replies: 0
    Last Post: 03-19-2008, 02:15 PM
  5. Link List
    By one198 in forum New To Java
    Replies: 0
    Last Post: 10-14-2007, 01:33 PM

Posting Permissions

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