Create a simple browser in Swing : Text EditorPane « Swing JFC « Java






Create a simple browser in Swing

   

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

public class MiniBrowser extends JFrame implements HyperlinkListener {
  private JButton backButton = new JButton("<"), forwardButton = new JButton(">");

  private JTextField locationTextField = new JTextField(35);

  private JEditorPane displayEditorPane = new JEditorPane();

  private ArrayList pageList = new ArrayList();

  public MiniBrowser() {
    setSize(640, 480);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel buttonPanel = new JPanel();
    backButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        actionBack();
      }
    });
    backButton.setEnabled(false);
    buttonPanel.add(backButton);
    forwardButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        actionForward();
      }
    });
    forwardButton.setEnabled(false);
    buttonPanel.add(forwardButton);
    locationTextField.addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
          actionGo();
        }
      }
    });
    buttonPanel.add(locationTextField);
    JButton goButton = new JButton("GO");
    goButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        actionGo();
      }
    });
    buttonPanel.add(goButton);
    displayEditorPane.setContentType("text/html");
    displayEditorPane.setEditable(false);
    displayEditorPane.addHyperlinkListener(this);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(new JScrollPane(displayEditorPane), BorderLayout.CENTER);
  }

  private void actionBack() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
      showPage(new URL((String) pageList.get(pageIndex - 1)), false);
    } catch (Exception e) {
    }
  }

  private void actionForward() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
      showPage(new URL((String) pageList.get(pageIndex + 1)), false);
    } catch (Exception e) {
    }
  }

  private void actionGo() {
    URL verifiedUrl = verifyUrl(locationTextField.getText());
    if (verifiedUrl != null) {
      showPage(verifiedUrl, true);
    } else {
      System.out.println("Invalid URL");
    }
  }

  private URL verifyUrl(String url) {
    if (!url.toLowerCase().startsWith("http://"))
      return null;

    URL verifiedUrl = null;
    try {
      verifiedUrl = new URL(url);
    } catch (Exception e) {
      return null;
    }

    return verifiedUrl;
  }

  private void showPage(URL pageUrl, boolean addToList) {
    try {
      URL currentUrl = displayEditorPane.getPage();
      displayEditorPane.setPage(pageUrl);
      URL newUrl = displayEditorPane.getPage();
      if (addToList) {
        int listSize = pageList.size();
        if (listSize <= 0) {
          return;
        }
        int pageIndex = pageList.indexOf(currentUrl.toString());
        if (pageIndex >= listSize - 1) {
          return;
        }
        for (int i = listSize - 1; i > pageIndex; i--) {
          pageList.remove(i);
        }
        pageList.add(newUrl.toString());
      }
      locationTextField.setText(newUrl.toString());
      updateButtons();
    } catch (Exception e) {
      System.out.println("Unable to load page");
    }
  }

  private void updateButtons() {
    if (pageList.size() < 2) {
      backButton.setEnabled(false);
      forwardButton.setEnabled(false);
    } else {
      URL currentUrl = displayEditorPane.getPage();
      int pageIndex = pageList.indexOf(currentUrl.toString());
      backButton.setEnabled(pageIndex > 0);
      forwardButton.setEnabled(pageIndex < (pageList.size() - 1));
    }
  }

  public void hyperlinkUpdate(HyperlinkEvent event) {
    HyperlinkEvent.EventType eventType = event.getEventType();
    if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
      if (event instanceof HTMLFrameHyperlinkEvent) {
        HTMLFrameHyperlinkEvent linkEvent = (HTMLFrameHyperlinkEvent) event;
        HTMLDocument document = (HTMLDocument) displayEditorPane.getDocument();
        document.processHTMLFrameHyperlinkEvent(linkEvent);
      } else {
        showPage(event.getURL(), true);
      }
    }
  }

  public static void main(String[] args) {
    MiniBrowser browser = new MiniBrowser();
    browser.setVisible(true);
  }
}

   
    
    
  








Related examples in the same category

1.Viewing RTF formatViewing RTF format
2.EditorPane SampleEditorPane Sample
3.Setting the Font and Color of Text in a JTextPane Using Styles: Makes text red
4.Setting the Font and Color of Text in a JTextPane Using Styles: Inherits from "Red"; makes text red and underlined
5.Setting the Font and Color of Text in a JTextPane Using Styles: Makes text 24pts
6.Setting the Font and Color of Text in a JTextPane Using Styles: Makes text italicized
7.JEditorPane and the Swing HTML PackageJEditorPane and the Swing HTML Package
8.JEditorPane and the Swing HTML Package 2JEditorPane and the Swing HTML Package 2
9.JEditorPane and the Swing HTML Package 3JEditorPane and the Swing HTML Package 3
10.JEditorPane and the Swing HTML Package 4JEditorPane and the Swing HTML Package 4
11.JEditorPane and the Swing HTML Package 5JEditorPane and the Swing HTML Package 5
12.JEditorPane and the Swing HTML Package 6JEditorPane and the Swing HTML Package 6
13.JEditorPane and the Swing HTML Package 7JEditorPane and the Swing HTML Package 7
14.JEditorPane and the Swing HTML Package 8JEditorPane and the Swing HTML Package 8
15.JEditorPane and the Swing HTML Package 9JEditorPane and the Swing HTML Package 9
16.EditorPane Example 10EditorPane Example 10
17.JEditorPane Example 10 - using getIteratorJEditorPane Example 10 - using getIterator
18.JEditorPane Example 11JEditorPane Example 11
19.JEditorPane Example 12JEditorPane Example 12
20.JEditorPane Example 13JEditorPane Example 13
21.JEditorPane Example 14JEditorPane Example 14
22.JEditorPane Example 15JEditorPane Example 15
23.JEditorPane Example 16JEditorPane Example 16
24.JEditorPane Example 17JEditorPane Example 17
25.JEditorPane Example 18JEditorPane Example 18
26.JEditorPane Example 19JEditorPane Example 19
27.JEditorPane Example 20JEditorPane Example 20
28.JEditorPane and HyperlinkListener Demo
29.Create a right-aligned tab stop at 200 pixels from the left margin
30.Create a center-aligned tab stop at 300 pixels from the left margin
31.Create a decimal-aligned tab stop at 400 pixels from the left margin
32.Use SimpleAttributeSet with JTextPane
33.Show html
34.Show web page
35.Change mouse cursor during mouse-over action on hyperlinks
36.Add colored text to the document
37.Returns the offset of the bracket matching the one at the specified offset of the document
38.Return the leading whitespace of that string, for indenting subsequent lines
39.Locates the end of the word at the specified position.
40.Locates the start of the word at the specified position.
41.Get Trailing White Space
42.This program demonstrates how to display HTML documents in an editor pane.This program demonstrates how to display HTML documents in an editor pane.