1

I am trying to create a simple web browser but when i run it and hover over an URL the URL gets run even though i gave event.getEventType()==HyperlinkEvent.EventType.ACTIVATED why does it behave like event.getEventType()==HyperlinkEvent.EventType.ENTERED

Here is the full Code

package gui;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
public class WebBrowser extends JFrame{
private JTextField addressbar;
private JEditorPane display;
public WebBrowser(){
    super("Sagar Browser");
    addressbar = new JTextField("Enter a URL");
    addressbar.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    load(event.getActionCommand());                     
                }
            }           
            );
    add(addressbar,BorderLayout.NORTH); 
    display = new JEditorPane();
    display.setEditable(false);
    display.addHyperlinkListener(
            new HyperlinkListener(){
                public void hyperlinkUpdate(HyperlinkEvent event){
                    if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED);
                    load(event.getURL().toString());
                }
            }
            );
    add(new JScrollPane(display),BorderLayout.CENTER);      
  }
private void load(String usertext){
    try{
        display.setPage(usertext);
        addressbar.setText(usertext);
    }catch(Exception e){
        System.out.println("Enter Full URL");
    }
}
public static void main(String[] args){
    WebBrowser w = new WebBrowser();
    w.setSize(500,500);
    w.setVisible(true);
}
} 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sagar
  • 405
  • 1
  • 4
  • 16
  • Why you don't use JavaFX? WEBVIEW: http://docs.oracle.com/javafx/2/webview/WebViewSample.java.htm JAVAFX-INSTALL: http://docs.oracle.com/javafx/2/installation_2-1/javafx-installation-windows.htm – Stefan Sprenger Aug 22 '14 at 08:32

1 Answers1

3

Your listener ignores the relevant predicate. You probably meant this:

new HyperlinkListener(){
    public void hyperlinkUpdate(HyperlinkEvent event){
        if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {
            load(event.getURL().toString());
        }
    }
}

Related examples are examined here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045