Hi there.I have a quick question.I wrote this program to look like my own browser.It views the code of the url givven and it was suppose to be a browser too.The button explanations are as following:

CODE=Shows the code of the given url
VIEW=When you push it it was supposed to open the page in the window.(This is the part i need help in.)

Please help me.

Some other explanations ----> Pencere means window.


The code is given below:

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.Date;
import java.util.Calendar;

class pencere extends JFrame{
	JTextArea ta;
	JScrollPane sp;
	Container cont;
	Toolkit kit;
	Dimension dim;
	int sw,sh;
	ust pan;
	
	public pencere(){
		setTitle("URL Viewer 1.0");
			cont = this.getContentPane();
			kit = this.getToolkit();
			dim = kit.getScreenSize();
			sw =dim.width;sh=dim.height;
			this.setSize(sw-100,sh-100);
			this.setLocation(50,50);
			ta=new JTextArea();
			ta.setEditable(false);
			ta.setBackground(new Color(0xde,0xde,0xde));
			ta.setTabSize(3);
			sp=new JScrollPane(ta);
			pan=new ust(this);
			
			cont.add(pan,BorderLayout.NORTH);
			cont.add(sp,BorderLayout.CENTER);
			
		}
	}
class ust extends JPanel{
	JTextField tf;
	JButton b1,b2,b3;
	pencere pen;
	Runtime r;
	Process p= null;
	Border bor;
	URL url1;
	URLConnection uconn;
	String ipstr;
	
	public ust(pencere vpen){
		this.pen = vpen;
		this.setLayout(new FlowLayout());
		tf=new JTextField(30);
		tf.setText("http://");
		bor= BorderFactory.createEtchedBorder();
		this.setBorder(bor);
		b1=new JButton("CODE");
		b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				ust.this.code();
				}
				});
		r= Runtime.getRuntime();
		
		b2=new JButton("VIEW");
		b2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
			String web[] ={"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE",tf.getText()};
			try{
				
				p = r.exec(web);
			}
			catch(Exception e){
			JOptionPane.showMessageDialog(pen,
			   		    		"Invalid URL",
		    		    		"Warning",
		    		    		JOptionPane.WARNING_MESSAGE);
			}
				}
			});
			
		b3=new JButton("Info");
		b3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				ust.this.info();
				}
			});
		
		add(tf);add(b1);add(b2);add(b3);
		
		}
	
	public void code(){
	try{
		url1=new URL(tf.getText());;
		uconn =url1.openConnection();
		pen.ta.setText("");
		BufferedReader in=new BufferedReader(new InputStreamReader(url1.openStream()));
		String source;
		int i=0;
		while((source=in.readLine())!=null){
			pen.ta.append(i++ +"\t| ");
		pen.ta.append(source+"\n");
		}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(pen,
			   		    		"Invalid URL",
		    		    		"Warning",
		    		    		JOptionPane.WARNING_MESSAGE);
			}
	}
	public void info(){
		final JDialog infosu=new JDialog(new Frame(),"Bilgiler");
		JLabel defprot=new JLabel("DefaultPort:"+url1.getDefaultPort());
		JButton kap=new JButton("Kapat");
		kap.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
		    infosu.setVisible(false);	    	
			}
			});
		JLabel file=new JLabel("File:"+url1.getFile());
		JLabel host=new JLabel("Host:"+url1.getHost());
		JLabel path=new JLabel("Path:"+url1.getPath());
		JLabel protocol=new JLabel("Protocol:"+url1.getProtocol());
		JLabel ref=new JLabel("Reference:"+url1.getRef());
		JLabel all=new JLabel("All:"+url1.toString());
		Date dat=new Date(uconn.getLastModified());
		JLabel lm=new JLabel("LM:"+dat.toString());
		Date exp=new Date(uconn.getExpiration());
		JLabel expire=new JLabel("Expire:"+exp.toString());
		try{
		ipstr=InetAddress.getByName(url1.getHost()).getHostAddress();
		}catch(Exception e){}
		JLabel ip=new JLabel("IP no:"+ipstr);
		
		
		infosu.getContentPane().setLayout(new GridLayout(11,1));
		Container dcont=infosu.getContentPane();
		dcont.add(kap);
		dcont.add(defprot);
		dcont.add(file);
		dcont.add(host);
		dcont.add(path);
		dcont.add(protocol);
		dcont.add(ref);
		dcont.add(all);
		dcont.add(lm);
		dcont.add(expire);
		dcont.add(ip);
		infosu.setSize(250,200);
		infosu.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
		infosu.setVisible(true);
	}	
	

	}





public class URLviewer {
	
	public static void main(String args[]){
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		pencere pen=new pencere();
		pen.setVisible(true);
		pen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		}
	}

Recommended Answers

All 2 Replies

Hi everyone,

You are using a JTextArea so going to a webpage via that JComponent is out the window so switch your JTextArea to a JTextPane. In the JTextPane api the have a setURL method. Use that method to go to a particular webpage

Finally i have become a guru. I have attained java nirvana

Richard West

firstly i wanna say "long live gurus" .secondly can you please change the code and post it cause i couldn't change the darn thing kept giving me errors

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.