• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a simple browser

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic