Hi everybody,

Tryed to make this program: Simple browser: you type the URL at the top and then get a new window with this web page.
But my output is just JFrame(Even color did not get changed)

help me please to find the problem.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;



public class ReadServerFile extends JFrame{
	
	private JTextField enterField;
	private JEditorPane contentsArea;

	public ReadServerFile() {
		super("Simple Web Browser");
		setSize(400,500);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(300,100);
		setBackground(Color.WHITE);
	
		enterField = new JTextField("Enter URL here");
		enterField.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						getThePage(e.getActionCommand());
					}
				}
		);
		add(enterField, BorderLayout.NORTH);
		
		contentsArea = new JEditorPane();
		contentsArea.setEditable(false);
		contentsArea.addHyperlinkListener(
				new HyperlinkListener() {
					public void hyperlinkUpdate(HyperlinkEvent e) {
						if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
							getThePage(e.getURL().toString());
					}
				}
		);
		add(new JScrollPane(contentsArea), BorderLayout.CENTER);
	}
	
	public void getThePage(String location) {
		try {
			contentsArea.setPage(location);
			enterField.setText(location);
		}
		catch(IOException e) {
				JOptionPane.showMessageDialog(this, "Bad URL",
						"Error with this URL", JOptionPane.ERROR_MESSAGE);		
		}
	}
	
	public static void main(String[] args) {
		ReadServerFile application = new ReadServerFile();
	}

}

Look creating a web browser isnt that simple, u can use already deployed project to understand the working or extend them like http://lobobrowser.org/java-browser.jsp. If you want to open URL in a webbrowser like firefox or IE then use JDESKTOP Api. Hope this Help...

But my output is just JFrame(Even color did not get changed)

make a frame instance and add components to it
Something like this:

JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

Here's the link where I got this and will probably answer your next questions
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

make a frame instance and add components to it
Something like this:

JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

Here's the link where I got this and will probably answer your next questions
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

I think I add all components to JFrame:

Line 44:

this.add(new JScrollPane(contentsArea), BorderLayout.CENTER);

Line 32:

this.add(enterField, BorderLayout.NORTH);

Don't call setVisible until all the GUI has been layed out.

Don't call setVisible until all the GUI has been layed out.

Thank you so much!
Now it works!

Creating a web-browser in Java isn't the best idea.
JEditorPane was originally used in order to display simple web-pages such as HTML files created by the user itself.
Trying to browse the web in a JEditorPane will be very frustrating as you'll notice nearly no plugin or command will work. With some really hard work and knowledge of some basic browser functionality you might be able to get a somewhat functional browser.

Otherwise I wouldn't recommend it.

If you think you got what it takes,
or just simply want to display simple HTML files then
here is some help:

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

public class WebBrowser extends JFrame 
			implements ActionListener, KeyListener, HyperlinkListener
{
	private JPanel contentPane = new JPanel();
	private JEditorPane editorPane = new JEditorPane();
	private JTextField addressBar = new JTextField();
	
	public WebBrowser()
	{
		contentPane = (JPanel) this.getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.setBackground(new Color(220,220,220));
		contentPane.setBorder(BorderFactory.createLineBorder(
				         getBackground().brighter()));
		contentPane.setOpaque(true);
		contentPane.setVisible(true);
		
		editorPane.addHyperlinkListener(this);
		editorPane.setEditable(false);
		
		JScrollPane sp = new JScrollPane(editorPane,
				     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		addressBar.setPreferredSize(new Dimension(getWidth(), 30));
		addressBar.setForeground(new Color(0,74,127));
		addressBar.setFont(new Font("SansSerif", Font.BOLD, 12));
		addressBar.addKeyListener(this);
		addressBar.setOpaque(true);
		addressBar.setVisible(true);
		
		setTitle("Java WebBrowser");
		setLayout(new BorderLayout());
		setLocationRelativeTo(null);
		setResizable(true);
		setSize(800,600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		
		contentPane.add(addressBar, BorderLayout.NORTH);
		contentPane.add(sp, BorderLayout.CENTER);
		
		repaint();
		validate();
	}
	
	// ActionListener reserved for buttons
	public void actionPerformed(ActionEvent e)
	{
		
	}
	
	// HyperlinkListener
	public void hyperlinkUpdate(HyperlinkEvent e)
	{
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
		{
			try
			{
				editorPane.setPage(e.getURL());
			}
			catch (IOException ex)
			{
				ex.printStackTrace();
			}
		}
	}
	
	public void urlPage(String URL)
	{
		try
		{
			editorPane.setPage(URL);
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
	
	
	
	// KeyListener
	public void keyPressed(KeyEvent e)
	{
		if (e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			System.out.println("Pressed Enter");
			String s = addressBar.getText();
			if (!s.contains("http://"))
			{
				urlPage("http://"+s);
				addressBar.setText(s);
			}
		}
	}
	
	public void keyTyped(KeyEvent e)
	{
		
	}
	
	public void keyReleased(KeyEvent e)
	{
		
	}
	
	
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		try
		{
			UIManager.setLookAndFeel(
					"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
				
		new WebBrowser();
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.