welshspoon
Technical User
I'm creating my own web browser but i'm having some problems. I want to be able to enter the address into a JTextField but when i use .getText() i either get a MalformedURL Exception or an IOException and i don't understand why or how to correct it.
I know the code below is reading sites from the hard drive but this is all i've managed it to do successfully. Also, the lines of code about stacks I haven't finished yet so please ignore them unless they are causing the problem.
All help will be greatfully received
Thanks
I know the code below is reading sites from the hard drive but this is all i've managed it to do successfully. Also, the lines of code about stacks I haven't finished yet so please ignore them unless they are causing the problem.
All help will be greatfully received
Thanks
Code:
import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class WebExplorer extends JFrame implements HyperlinkListener {
private JEditorPane html;
private JPanel topPanel;
private JToolBar toolbar;
private JTextField address;
private JLabel addressLabel;
private JButton home;
private JButton back;
private JButton forward;
private JButton visit;
private JScrollPane scrollPane;
private JMenuBar menu;
private JMenu menuProperty;
private JMenu menuFile;
private JMenu menuEdit;
private JProgressBar statusbar;
final String sPath = System.getProperty( "user.dir" ) + "/";
final Stack fStack = new Stack();
final Stack bStack = new Stack();
public WebExplorer() {
setTitle( "Web Browser" );
setSize( 600, 400 );
setBackground( Color.gray );
getContentPane().setLayout( new BorderLayout() );
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel, BorderLayout.CENTER);
toolbar = new JToolBar();
getContentPane().add(toolbar, BorderLayout.NORTH);
home = new JButton("Home");
back = new JButton("Back");
forward = new JButton("Forward");
addressLabel = new JLabel("URL");
address = new JTextField(40);
String text = address.getText();
visit = new JButton("Go");
statusbar = new JProgressBar();
toolbar.add(home);
toolbar.add(Box.createHorizontalStrut(10));
toolbar.add(back);
toolbar.add(Box.createHorizontalStrut(10));
toolbar.add(forward);
toolbar.add(addressLabel);
toolbar.add(address);
toolbar.add(Box.createHorizontalStrut(10));
toolbar.add(visit);
topPanel.add(statusbar, BorderLayout.SOUTH);
home.setMnemonic('H');
back.setMnemonic('B');
forward.setMnemonic('F');
visit.setMnemonic('G');
addressLabel.setDisplayedMnemonic('U');
addressLabel.setLabelFor(address);
menu = new JMenuBar();
setJMenuBar( menu );
JMenuItem open = new JMenuItem("Open", new ImageIcon("Openicon.gif"));
open.setMnemonic('O');
JMenuItem save = new JMenuItem("Save", new ImageIcon("Saveicon.gif"));
save.setMnemonic('S');
JMenuItem print = new JMenuItem("Print");
print.setMnemonic('P');
menuProperty = new JMenu( "Properties" );
menuProperty.setMnemonic('r');
JMenuItem exit = new JMenuItem("Exit");
exit.setMnemonic('E');
menuFile = new JMenu(" File" );
menuFile.setMnemonic('F');
menuFile.add(open);
menuFile.add(save);
menuFile.add(print);
menuFile.addSeparator();
menuFile.add( menuProperty );
menuFile.addSeparator();
menuFile.add(new AbstractAction("Exit")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
menu.add( menuFile );
JMenuItem cut = new JMenuItem("Cut");
cut.setMnemonic('X');
JMenuItem copy = new JMenuItem("Copy");
copy.setMnemonic('C');
JMenuItem paste = new JMenuItem("Paste");
paste.setMnemonic('V');
menuEdit = new JMenu( "Edit" );
menuEdit.setMnemonic('E');
menuEdit.add(cut);
menuEdit.add(copy);
menuEdit.add(paste);
menu.add( menuEdit );
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
address.getText();
}
catch(Exception e) {
html.setText("Exception: " + e);
}
}
};
visit.addActionListener(listener);
address.addActionListener(listener);
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
fStack.push("[URL unfurl="true"]http://"[/URL] + address.getText().toLowerCase());
// doc_win.setPage((String) bStack.pop());
} catch( Exception ex ) { new JOptionPane("Illegal URL, try again");}
}});
forward.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
bStack.push("[URL unfurl="true"]http://"[/URL] + address.getText().toLowerCase());
// doc_win.setPage((String) fStack.pop());
} catch( Exception ex ) { new JOptionPane("Illegal URL, try again");}
}});
visit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
address.getText();
}
catch(Exception ex) {
html.setText("Exception: " + e);
}
}
});
try {
URL url = new URL( "file:///" + sPath + "home.htm");
html = new JEditorPane( );
html.setEditable( false );
html.setPage(url);
scrollPane = new JScrollPane();
scrollPane.getViewport().add( html, BorderLayout.CENTER);
topPanel.add( scrollPane, BorderLayout.CENTER );
html.addHyperlinkListener(this);
}
catch ( MalformedURLException e ) {
System.out.println( "Malformed URL: " + e );
}
catch( IOException e ) {
System.out.println( "IOException: " + e );
}
}
public void hyperlinkUpdate( HyperlinkEvent event ) {
if( event.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) {
try {
Cursor cursor = html.getCursor();
Cursor waitCursor = Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR );
html.setCursor( waitCursor );
SwingUtilities.invokeLater( new PageLoader( html, event.getURL(), cursor));
html.setPage(event.getURL());
}
catch(IOException e) {
html.setText("Exception: " + e);
}
}
}
public static void main( String args[]) {
WebExplorer mainFrame = new WebExplorer();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible( true );
}
}
Code:
import java.io.*;
import java.lang.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
class PageLoader implements Runnable
{
private JEditorPane html;
private URL url;
private Cursor cursor;
PageLoader( JEditorPane html, URL url, Cursor cursor )
{
this.html = html;
this.url = url;
this.cursor = cursor;
}
public void run()
{
if( url == null )
{
html.setCursor( cursor );
Container parent = html.getParent();
parent.repaint();
}
else
{
Document doc = html.getDocument();
try
{
html.setPage( url );
}
catch( IOException ioe )
{
html.setDocument( doc );
}
finally
{
url = null;
SwingUtilities.invokeLater( this );
}
}
}
}