Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. if(text.length() > wrapLength)
  2. {
  3. //break the text into substrings and insert a <br /> at wrapLength
  4. //the difficulty is in figuring out the value of wrapLength
  5. }
  6.  
  7. import java.awt.Color;
  8. import java.awt.Desktop;
  9. import java.awt.Font;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.io.IOException;
  13. import java.net.URI;
  14. import java.net.URISyntaxException;
  15. import javax.swing.JLabel;
  16.  
  17. public class Hyperlink extends JLabel
  18. {
  19. private URI uri;
  20. private String baseText;
  21.  
  22. public Hyperlink(String text, String uri, Font font, Color textColor)
  23. {
  24. super(text);
  25. baseText = text;
  26. setFont(font);
  27. setForeground(textColor);
  28. setOpaque(false);
  29.  
  30. if(Desktop.isDesktopSupported())
  31. {
  32. try
  33. {
  34. setToolTipText(uri);
  35. this.uri = new URI(uri);
  36. addMouseListener(new LinkMouseListener());
  37. }
  38. catch(URISyntaxException ex)
  39. {
  40. }
  41. }
  42. }
  43.  
  44. private class LinkMouseListener extends MouseAdapter
  45. {
  46. @Override
  47. public void mouseClicked(MouseEvent e)
  48. {
  49. Desktop desktop = Desktop.getDesktop();
  50. try
  51. {
  52. desktop.browse(uri);
  53. }
  54. catch(IOException ioe)
  55. {
  56. System.out.println("Something went wrong..."); //DELETE
  57. }
  58. }
  59.  
  60. @Override
  61. public void mouseEntered(MouseEvent e)
  62. {
  63. setText("<html><u>" + baseText + "</u></html>");
  64. }
  65.  
  66. @Override
  67. public void mouseExited(MouseEvent e)
  68. {
  69. setText(baseText);
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement