File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Creating a simple browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating a simple browser" Watch "Creating a simple browser" New topic
Author

Creating a simple browser

Leela murthy
Greenhorn

Joined: Sep 12, 2006
Posts: 6
Actually i have been asked to create a simple browser using Swing i have most of that here i am putting the whole code

mport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;


public class SimpleBrowser implements ActionListener {

JFrame sBrowser;
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
JFileChooser fileChooser;
JPanel addressPanel;
JTextField addressField;
JLabel addressLabel;
JButton goButton;
JPanel browserPanel;
JScrollPane editorScrollPane;
JEditorPane editorPane;
URL url;
Dimension d;

public SimpleBrowser(){

sBrowser= new JFrame("Simple Browser");
sBrowser.setLayout(new BorderLayout());
sBrowser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("Screen size is="+d.toString());
sBrowser.setSize(d);
sBrowser.setResizable(true);
menuBar=new JMenuBar();
menu= new JMenu("File");
menuBar.add(menu);
menuItem=new JMenuItem("Open");
menuItem.addActionListener(this);
menu.add(menuItem);

addressPanel = new JPanel(new BorderLayout(5,0));
browserPanel=new JPanel(new BorderLayout());
addressPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
browserPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
addressLabel = new JLabel("Address", SwingConstants.LEFT);
addressField = new JTextField(70);
goButton = new JButton("Go");
goButton.addActionListener(this);
addWidgets();

}

protected void addWidgets(){

System.out.println("inside addwidgets");
sBrowser.setJMenuBar(menuBar);
addressPanel.add(addressLabel,BorderLayout.WEST);
addressPanel.add(addressField,BorderLayout.CENTER);
addressPanel.add(goButton,BorderLayout.EAST);
sBrowser.getRootPane().setDefaultButton(goButton);
sBrowser.getContentPane().add(addressPanel,BorderLayout.NORTH);

editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
editorScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
browserPanel.add(editorScrollPane,BorderLayout.CENTER) sBrowser.getContentPane().add(browserPanel,BorderLayout.CENTER);
sBrowser.setVisible(true);
}

public void actionPerformed(ActionEvent ae) {

System.out.println("Handling Action Event");
System.out.println(ae.getActionCommand());
if(ae.getActionCommand().equals("Open")){

fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
int returnValue= fileChooser.showOpenDialog(sBrowser);

if(returnValue==JFileChooser.APPROVE_OPTION){

java.io.File selectedFile=fileChooser.getSelectedFile();
System.out.println("Selected Files path is= "+selectedFile.getAbsolutePath());
String filePath=fileChooser.getSelectedFile().getAbsolutePath();
String fileSeperator = System.getProperty("file.separator");
System.out.println("File Seperator is= "+fileSeperator);
char seperator = fileSeperator.charAt(0);
filePath = filePath.replace(seperator, '/');
filePath = "file://localhost/" + filePath;
addressField.setText(filePath);
}

}

if(ae.getActionCommand().equals("Go")){
String urlPath= addressField.getText();
System.out.println("urlPath is= "+urlPath);
try{
url= new URL(urlPath);
}
catch (MalformedURLException e) { // Invalid URL
System.out.println("Cannot create url for: " + urlPath);
}
browserPanel.removeAll();
editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setOpaque(true);
editorPane.setEnabled(true);
try {
System.setProperty("proxyHost","10.10.10.254");
System.setProperty("proxyPort","8080");
editorPane.setPage(url);
}
catch (IOException e) {
System.out.println("Attempted to read a bad URL: " +url);
}
editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
editorScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
browserPanel.add(editorScrollPane,BorderLayout.CENTER);
sBrowser.validate();
}
}


private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
SimpleBrowser sb = new SimpleBrowser();
}


public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

}


The above code is working fine for normal HTML Documents, but if i try to render url's Like http://www.google.co.in, JEditorpane is failing to render the things properly. So i want some help regarding this.
I am Actually trying to use HTMLEditorkit class and HTMLDocument class but i could not make a complete code

please any body help modify above code so that it works for all URL's as the Internet explorer does



thanks in advance
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8907
    
    8

HTMLEditorKit is a poor choice for implementing a general-purpose browser. The HTMLEditorKit is based on the HTML 3.2 reference specification and browsers have moved on to HTML 4+. HTMLEditorKit does not support Javascript. These classes are intended to help make simple applications like a documentation browser, not fully functional web browsers.
There are more complete java-based browser components, like Ice Browser available.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41843
    
  63
I think HTMLEditorKit only supports HTML 3.2, and no CSS or JavaScript or other fany stuff (check its documentation for details). So it's unlikely to display modern web sites exactly as a standards-compliant browser (or even IE) would display it.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a simple browser