Java Webbrowser HELP PLease

S

shnaplesit

Hi i m new in java language and right now i m working on a webbrowser
project that has functions including history and forward and back
button i would appreciate any suggestions and solutions from you guys.
The problem with the history is that i m using jcombobox which
retrieves urls from the jetexfield but when i select a url that is
added to historycombo it doesnt work at all.
with that said here is my code:

import javax.swing.*;
import java.net.*;
import java.io.*;
import javax.swing.text.html.*;
import javax.swing.event.*;
import java.awt.*;
/*
* Browser.java
*
* Created on March 29, 2004, 8:36 AM
*/

/**
*
* @author mpenderg
*/
public class Browser extends javax.swing.JFrame implements
HyperlinkListener{

/** Creates new form Browser */
public Browser() {
initComponents();

setSize(700,400);
setTitle("Browser Demo");
viewerPane.addHyperlinkListener(this);

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
northPanel = new javax.swing.JPanel();
urlLabel = new javax.swing.JLabel();
urlField = new javax.swing.JTextField();
browseButton = new javax.swing.JButton();
goButton = new javax.swing.JButton();
sourceButton = new javax.swing.JButton();
historyCombo = new javax.swing.JComboBox();
centerPane = new javax.swing.JScrollPane();
viewerPane = new javax.swing.JEditorPane();

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt)
{
exitForm(evt);
}
});

northPanel.setLayout(new
java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

urlLabel.setFont(new java.awt.Font("Dialog", 1, 10));
urlLabel.setText("Enter URL or filename: ");
northPanel.add(urlLabel);

urlField.setColumns(30);
urlField.setFont(new java.awt.Font("Dialog", 1, 10));
urlField.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
urlFieldActionPerformed(evt);
}
});

northPanel.add(urlField);

browseButton.setFont(new java.awt.Font("Dialog", 1, 10));
browseButton.setText("Browse");
browseButton.setMargin(new java.awt.Insets(2, 3, 2, 3));
browseButton.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
browseButtonActionPerformed(evt);
}
});

northPanel.add(browseButton);

goButton.setFont(new java.awt.Font("Dialog", 1, 10));
goButton.setText("Go");
goButton.setMargin(new java.awt.Insets(2, 3, 2, 3));
goButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
goButtonActionPerformed(evt);
}
});

northPanel.add(goButton);

sourceButton.setFont(new java.awt.Font("Dialog", 1, 10));
sourceButton.setText("Source");
sourceButton.setMargin(new java.awt.Insets(2, 3, 2, 3));
sourceButton.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
sourceButtonActionPerformed(evt);
}
});

northPanel.add(sourceButton);

historyCombo.setToolTipText("History");
historyCombo.setPreferredSize(new java.awt.Dimension(100,
25));
historyCombo.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
historyComboActionPerformed(evt);
}
});

northPanel.add(historyCombo);

getContentPane().add(northPanel, java.awt.BorderLayout.NORTH);

viewerPane.setEditable(false);
centerPane.setViewportView(viewerPane);

getContentPane().add(centerPane,
java.awt.BorderLayout.CENTER);

pack();
}

private void historyComboActionPerformed(java.awt.event.ActionEvent
evt) {


String urlstr1 = historyCombo.getSelectedItem().toString();

urlField.setText(urlstr1);

/*
try{
viewerPane.setPage(urlstr);
}
catch ( IOException e ) {
}
*/
}

private void urlFieldActionPerformed(java.awt.event.ActionEvent
evt) {
String urlstr = historyCombo.getSelectedItem().toString();
// urlField.setText(urlstr2);
String = urlField.setText("go to"+urlstr);
}

private void sourceButtonActionPerformed(java.awt.event.ActionEvent
evt) {
// Add your handling code here:
String urlstr = urlField.getText();


if(urlstr.length() == 0)
return;
//
// check to see if its a remote page
//
String urllc = urlstr.toLowerCase();
if(!urllc.startsWith("http://") && !urllc.startsWith("ftp://")
&&
!urllc.startsWith("www.")) {
urlstr = "file:///"+urlstr; // note you must have 3
slashes !
}
else // http missing ?
if(urllc.startsWith("www.")) {
urlstr = "http://"+urlstr; // tack on http://
}

try{
URL url = new URL(urlstr); // create a url
InputStream in = url.openStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
String line;
line = br.readLine();
while(line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();

}
catch(MalformedURLException ex) {
JOptionPane.showMessageDialog(this,"Bad URL :
"+urlstr,"Error",JOptionPane.ERROR_MESSAGE);
System.out.println(ex.getMessage());
return;
}
catch(IOException e) {
JOptionPane.showMessageDialog(this,"IOException :
"+urlstr,"Error",JOptionPane.ERROR_MESSAGE);
System.out.println(e.getMessage());
return;
}

}

private void browseButtonActionPerformed(java.awt.event.ActionEvent
evt) {
// Add your handling code here:
JFileChooser jc = new JFileChooser();
int result = jc.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION) {
File f = jc.getSelectedFile();
String path = f.getPath();
urlField.setText(path);
}

}
public void hyperlinkUpdate(HyperlinkEvent evt) {
try{
URL url = evt.getURL();
HyperlinkEvent.EventType type = evt.getEventType();
//
// this event if you click on it
//
if(type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
System.out.println("going to
....."+evt.getDescription());
viewerPane.setPage(url);
}
//
// this event if the mouse goes over the link
//
if(type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("ENTERED");
}
//
// this event once the mouse goes off the link
//
if(type == HyperlinkEvent.EventType.EXITED) {
System.out.println("EXITED");
}

}
catch(IOException e) {
System.out.println("Unable to follow
hyperlink"+e.getMessage());
}
}

private void goButtonActionPerformed(java.awt.event.ActionEvent
evt) {
// Add your handling code here:
String urlstr = urlField.getText();
historyCombo.addItem(urlstr);
if(urlstr.length() == 0)
return;
//
// check to see if its a remote page
//
String urllc = urlstr.toLowerCase();
if(!urllc.startsWith("http://") && !urllc.startsWith("ftp://")
&&
!urllc.startsWith("www.")) {
urlstr = "file:///"+urlstr; // note you must have 3
slashes !
}
else // http missing ?
if(urllc.startsWith("www.")) {
urlstr = "http://"+urlstr; // tack on http://
}

try{
URL url = new URL(urlstr); // create a url
viewerPane.setPage(url); // ask the viewer to display it
}
catch(MalformedURLException ex) {
JOptionPane.showMessageDialog(this,"Bad URL :
"+urlstr,"Error",JOptionPane.ERROR_MESSAGE);
System.out.println(ex.getMessage());
return;
}
catch(IOException e) {
JOptionPane.showMessageDialog(this,"IOException :
"+urlstr,"Error",JOptionPane.ERROR_MESSAGE);
System.out.println(e.getMessage());
return;
}

}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Browser().show();
}


// Variables declaration - do not modify
private javax.swing.JButton browseButton;
private javax.swing.JScrollPane centerPane;
private javax.swing.JButton goButton;
private javax.swing.JComboBox historyCombo;
private javax.swing.JPanel northPanel;
private javax.swing.JButton sourceButton;
private javax.swing.JTextField urlField;
private javax.swing.JLabel urlLabel;
private javax.swing.JEditorPane viewerPane;
// End of variables declaration

}
 
A

Andrew Thompson


I posted a response advising c.l.j.help
to this thread when it appeared on c.l.j.h
<http://google.com/[email protected]>

Unfortunately it does not seem to have
appeared in the Google archive yet, and
the poster is apparently posting from
Google. :-(

I have sent a response to the address
shown at the top (WTEO - Be Patient!),
and just hope it is valid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top