Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JavaScript with JEditorPane?

843806Jun 29 2009 — edited Jun 29 2009
I am messing around with some URL stuff at the moment and I was trying to mimic a web browser,
I may not succeed but its worth doing,

I am stuck on getting JavaScript to work, is it possible to get JavaScript showing up.

Also I am having a problem with links not working properly but this maybe because its JavaScript links and not <a href=" links

I have to go for a while but i'll be back to read the answers.

Here's my program.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class WebGUI extends JFrame
{
  public static String myURL = null;
  public static JEditorPane area;
  public static JButton button;
  public static JPanel panel;
  public static JTextField field;
  public static JScrollPane scrollPane;
  boolean urlChanged = false;

  public void setUpGUI()
  {
    area = new JEditorPane();
    button = new JButton("Go");
    panel = new JPanel();
    field = new JTextField(20);
    scrollPane = new JScrollPane(area);
    
    panel.add(field);
    panel.add(button);
    
    button.addActionListener(new Action());
    field.addActionListener(new Action());

    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    
    area.setContentType("text/html");
    
    this.getContentPane().add(BorderLayout.NORTH,panel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800,500);
    this.setVisible(true);
    
    while(true)
    {
      try
      {
        Thread.sleep(12);
      }
      catch (InterruptedException e)
      {
        System.out.println("It should work");
      }
      
      if (myURL != null && urlChanged) // Updates URL
      {      
        urlChanged = false;
      
        try
        {
          area = new JEditorPane(myURL);
          scrollPane = new JScrollPane(area);
          this.getContentPane().add(BorderLayout.CENTER,area);
        }
        catch (IOException e)
        {
          System.out.println("URL invalid");
        }
      }
    }
  }
  public class Action implements ActionListener
  {
    public void actionPerformed(ActionEvent e) // Gets URL from JTextField and makes it valid when not given the http:// or http://www.
    {
      urlChanged = true;
      
      if(field.getText().charAt(0) == 'h' && field.getText().charAt(1) == 't' && field.getText().charAt(2) == 't' && field.getText().charAt(3) == 'p' && field.getText().charAt(4) == ':' && field.getText().charAt(5) == '/' && field.getText().charAt(6) == '/')
      {
        myURL = field.getText();
      }
      else if(field.getText().charAt(0) == 'w' && field.getText().charAt(1) == 'w' && field.getText().charAt(2) == 'w' && field.getText().charAt(3) == '.')
      {
        myURL = "http://" + field.getText();
      }
      else
      {
        myURL = "http://www." + field.getText();
      }
    }
  }
  
  public static void main(String[] args) throws Exception
  {
    WebGUI webGUI = new WebGUI();
    webGUI.setUpGUI();
  }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 27 2009
Added on Jun 29 2009
2 comments
192 views