java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to record mouse-over action on hyperlinks and shows the link in a status bar
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3946
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to record mouse-over action on hyperlinks and shows the link in a status bar E-mail
User Rating: / 17
PoorBest 

This Java Swing tip illustrates a method of recording mouse-over action on hyperlinks and shows the link in a status bar. The link is displayed at the bottom of the window. This tip also acts in itself as a mini browser. This tip also takes care to make the hyperlink text uneditable so that clicking doesnot change the text.

import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;

public class MiniBrowser extends JFrame {

  private JEditorPane jep;

  public MiniBrowser(String startingUrl) {
    // Ok, first just get a screen up and visible, with an appropriate
    // handler in place for the kill window command.
    super("MiniBrowser");
    setSize(400,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // Now set up our basic screen components, the editor pane, the
    // text field for URLs, and the label for status and link information.
    JPanel urlPanel = new JPanel();
    urlPanel.setLayout(new BorderLayout());
    JTextField urlField = new JTextField(startingUrl);
    urlPanel.add(new JLabel("Site: "), BorderLayout.WEST);
    urlPanel.add(urlField, BorderLayout.CENTER);
    final JLabel statusBar = new JLabel(" ");

    // Here's the editor pane configuration.  It's important to make
    // the "setEditable(false)" call; otherwise, our hyperlinks won't
    // work.  (If the text is editable, then clicking on a hyperlink
    // simply means that you want to change the text, not follow the
    // link.)
    jep = new JEditorPane();
    jep.setEditable(false);

    try {
      jep.setPage(startingUrl);
    }
    catch(Exception e) {
      statusBar.setText("Could not open starting page.  Using a blank.");
    }
    JScrollPane jsp = new JScrollPane(jep)

    // Get the GUI components onto our content pane.
    getContentPane().add(jsp, BorderLayout.CENTER);
    getContentPane().add(urlPanel, BorderLayout.NORTH);
    getContentPane().add(statusBar, BorderLayout.SOUTH);

    // Last but not least, hook up our event handlers.
    urlField.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        try {
          jep.setPage(ae.getActionCommand());
        }
        catch(Exception e) {
          statusBar.setText("Error: " + e.getMessage());
        }
      }
    });
    jep.addHyperlinkListener(new SimpleLinkListener(jep, urlField, 
                                                    statusBar));
  }

  public static void main(String args[]) {
    String url = "";
    if (args.length == 1) {
      url = args[0];
      if (!(url.startsWith("http:"|| url.startsWith("file:"))) {
        // If it's not a fully qualified URL, assume it's a file.
        if (url.startsWith("/")) {
          // Absolute path, so just prepend "file:"
          url = "file:" + url;
        }
        else {
          try {
            // Assume it's relative to the starting point.
            File f = new File(url);
            url = f.toURL().toString();
          }
          catch (Exception e) {
            url = "http://www.java-tips.org/";
          }
        }
      }
    }
    else {
      url = "http://www.java-tips.org";
    }
    new MiniBrowser(url).setVisible(true);
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.