9 Replies - 803 Views - Last Post: 22 April 2011 - 09:46 PM Rate Topic: -----

#1 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

error in HyperlinkListener in java...help

Posted 29 March 2011 - 12:09 AM

not able to navigate between web pages...that is on clicking any link on the website....it is not navigating to that link....but the website like www.yahoo.com is being opened by web browser...plz help......if this prblm not solved ....thn i will be no able to save history items nd bookmarks...plz helpppppppppppppppp


import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.Applet;

public class WeBrowsertry3 extends JFrame 
{

public JPanel panel1,panel2,window_panel,panel3,panel4;

public JLabel address_label;

public JTextField address_tf;

public JEditorPane window_pane;

public JScrollPane window_scroll;

public JButton b1,b2,b3,b4,b5,b6;

private Go go = new Go();
Go reload=new Go();
Menugo menugo1=new Menugo();
public JTabbedPane jtb1,jtb2;



public WeBrowsertry3() throws IOException
{
JMenuBar menuBar = new JMenuBar();	
JMenu menu1 = new JMenu("....");

JMenuItem item1,item2,item3;
item1=new JMenuItem("add tab");
item2=new JMenuItem(">>>>");
item3=new JMenuItem("<<<<");
menu1.add(item1);
menu1.addSeparator();
menu1.add(item2);
menu1.add(item3);

menuBar.add(menu1);


item1.addActionListener(menugo1); 


b1=new JButton("back");
b2=new JButton("forward");
//b2.setSize(30,40);
b3=new JButton("reload");
b4=new JButton("Go");
b5=new JButton("history");
b6=new JButton("Add Tab");
b6.addActionListener(new MyAction());
b4.addActionListener(go);
b3.addActionListener(go);
address_label = new JLabel(" address: ",SwingConstants.CENTER); // for address label
address_tf = new JTextField("http://www.yahoo.com",JTextField.LEFT);// for entering address


panel1=new JPanel(new BorderLayout());
panel2 = new JPanel(new BorderLayout());
panel3 = new JPanel(new BorderLayout());
window_panel = new JPanel(new BorderLayout());
panel4=new JPanel(new BorderLayout());



window_pane = new JEditorPane("http://www.yahoo.com"); // for editor pane
window_pane.setContentType("text/html"); // for content in the panel
window_pane.setEditable(false); // we can't update the text of a website
//window_pane.add(p1);

JSplitPane sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel1,panel2);
JSplitPane sp2=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel3,panel4);


panel1.add(b1,BorderLayout.WEST);
panel1.add(b2,BorderLayout.CENTER);
panel1.add(b3,BorderLayout.EAST);
panel2.add(address_label,BorderLayout.WEST); // address label on the left
panel2.add(address_tf,BorderLayout.CENTER);
panel2.add(b4,BorderLayout.EAST);
panel3.add(b5,BorderLayout.WEST);
panel4.add(b6,BorderLayout.WEST);
panel4.add(menuBar,BorderLayout.EAST);



window_scroll = new JScrollPane(window_pane);
window_panel.add(window_scroll);

Container pane = getContentPane();
pane.setLayout(new BorderLayout());	
pane.add(sp,BorderLayout.NORTH);
pane.add(sp2,BorderLayout.SOUTH);
//pane.add(panel3,BorderLayout.SOUTH);
pane.add(window_panel,BorderLayout.CENTER);
//pane.add(panel4,BorderLayout.SOUTH);


setTitle("My new web browser");
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public class Go implements ActionListener
{

public void actionPerformed(ActionEvent ae)
{

try
{

window_pane.setPage(address_tf.getText());

} 

catch (MalformedURLException e)
{ // new URL() failed
window_pane.setText("MalformedURLException: " + e);

} 

catch (IOException e) 
{ // openConnection() failed
window_pane.setText("IOException: " + e);
}

}

}





public class Menugo implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str2 = e.getActionCommand();
if(str2.equals("add tab"))
{



} 


}
}


public class MyAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
if(str.equals("Add Tab"))
{
String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
if(!st.equals(""))
{


}
}

}
}
public static void main(String args[]) throws IOException
{
WeBrowsertry3 wb = new WeBrowsertry3();
}

} 



Is This A Good Question/Topic? 0
  • +

Replies To: error in HyperlinkListener in java...help

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: error in HyperlinkListener in java...help

Posted 29 March 2011 - 03:02 AM

** Topic renamed for clarity **

Use a HyperlinkListener. You should be able to find the address clicked and set the JEditorPane to that new address.
Was This Post Helpful? 2
  • +
  • -

#3 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: error in HyperlinkListener in java...help

Posted 30 March 2011 - 06:09 AM

View PostDogstopper, on 29 March 2011 - 03:02 AM, said:

** Topic renamed for clarity **

Use a HyperlinkListener. You should be able to find the address clicked and set the JEditorPane to that new address.




thnku a lot sir ...for rplng............... :)
THROUGH THIS.......it will be feasible for me to....click on any ling of a website.??
LIKE IN THE SAME WAY.....we navigate in a web browser....plz reply sirrrrrrrr.....
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: error in HyperlinkListener in java...help

Posted 30 March 2011 - 02:56 PM

Yes, just get the URL location of the HyperlinkEvent (use getURL()) and then set the JEditorPane's location to that source (use setSource()). Make sure that you actually add the listener to the JEditorPane.

Give it a try!
Was This Post Helpful? 2
  • +
  • -

#5 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: error in HyperlinkListener in java...help

Posted 01 April 2011 - 08:44 AM

View PostDogstopper, on 30 March 2011 - 02:56 PM, said:

Yes, just get the URL location of the HyperlinkEvent (use getURL()) and then set the JEditorPane's location to that source (use setSource()). Make sure that you actually add the listener to the JEditorPane.

Give it a try!



thnks again sir,
i have done some changes......but the code is giving a CT error.....plz look at it..(sndng that snippet only...rest of the prgrm is same)
THE ERROR IS ";expected".... this error is coming at-->

JEditorPane window_pane = new JEditorPane("http://www.yahoo.com"); // for editor pane
		//window_pane.addHyperlinkListener(link);
		window_pane.setContentType("text/html"); // for content in the panel
		window_pane.setEditable(false); // we can't update the text of a website
		final JEditorPane finalpane =window_pane;
  window_ pane.addHyperlinkListener(new HyperlinkListener()// [u]error is coming in this line..[/u] [b]; expected[/b]
        {
            public void hyperlinkUpdate(HyperlinkEvent r)
            {
                try
                {
            	   if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            	 finalpane.setPage(r.getURL());
	   
                }catch(Exception e)
                {}
            }
        });





plz helpppppppppppppp :-)
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12148
  • View blog
  • Posts: 45,161
  • Joined: 27-December 08

Re: error in HyperlinkListener in java...help

Posted 01 April 2011 - 04:54 PM

What is the error?

Also, have a look at this link on asking questions the smart way.
Was This Post Helpful? 1
  • +
  • -

#7 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: error in HyperlinkListener in java...help

Posted 20 April 2011 - 07:09 PM

the code is giving a CT error.....plz look at it..(sndng that snippet only...rest of the prgrm is same)
THE ERROR IS ";expected".... this error is coming at-->


import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.Applet;

public class WeBrowsertry3 extends JFrame 
      {
	
	public JPanel panel1,panel2,window_panel,panel3,panel4;
	
	public JLabel address_label;

	public JTextField address_tf;

	public JEditorPane window_pane;

	public JScrollPane window_scroll;

	public JButton b1,b2,b3,b4,b5,b6;
	
	private Go go = new Go();
	Go reload=new Go();
	Menugo menugo1=new Menugo();
	public JTabbedPane jtb1,jtb2;
 
	
	
               public WeBrowsertry3() throws IOException
              {
		JMenuBar menuBar = new JMenuBar();						
    JMenu menu1 = new JMenu("....");
  
    JMenuItem item1,item2,item3;
    item1=new JMenuItem("add tab");
    item2=new JMenuItem(">>>>");
    item3=new JMenuItem("<<<<");
    menu1.add(item1);
   menu1.addSeparator();
   menu1.add(item2);
   menu1.add(item3);

    menuBar.add(menu1);

  
   item1.addActionListener(menugo1);   
	

		b1=new JButton("back");
		b2=new JButton("forward");
			//b2.setSize(30,40);
		b3=new JButton("reload");
		b4=new JButton("Go");
		b5=new JButton("history");
		b6=new JButton("Add Tab");
		b6.addActionListener(new MyAction());
		b4.addActionListener(go);
		b3.addActionListener(go);
		address_label = new JLabel(" address: ",SwingConstants.CENTER); // for address label
		address_tf = new JTextField("http://www.yahoo.com",JTextField.LEFT);// for entering address

			
		panel1=new JPanel(new BorderLayout());
		panel2 = new JPanel(new BorderLayout());
		panel3 = new JPanel(new BorderLayout());
		window_panel = new JPanel(new BorderLayout());
		panel4=new JPanel(new BorderLayout());
		
		

		window_pane = new JEditorPane("http://www.yahoo.com"); // for editor pane
		window_pane.setContentType("text/html"); // for content in the panel
		window_pane.setEditable(false); // we can't update the text of a website
		
   //error in coming here ;expected.....plz help
		 window_ pane.addHyperlinkListener(new HyperlinkListener(){
	
  	                           public void hyperlinkUpdate(HyperlinkEvent evt) 
 			{
    
			  if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
		                        {
  			 	   		
      					  pane.setPage(evt.getURL()); 
					
  			      }
       
  		                }
		});

//hyperlink code ending


				
		JSplitPane sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel1,panel2);
		JSplitPane sp2=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel3,panel4);
		
	  	
		panel1.add(b1,BorderLayout.WEST);
		panel1.add(b2,BorderLayout.CENTER);
		panel1.add(b3,BorderLayout.EAST);
		panel2.add(address_label,BorderLayout.WEST); // address label on the left
		panel2.add(address_tf,BorderLayout.CENTER);
		panel2.add(b4,BorderLayout.EAST);
		panel3.add(b5,BorderLayout.WEST);
		panel4.add(b6,BorderLayout.WEST);
		panel4.add(menuBar,BorderLayout.EAST);
		
		        
		
		window_scroll = new JScrollPane(window_pane);
		window_panel.add(window_scroll);

		Container pane = getContentPane();
				pane.setLayout(new BorderLayout());	
		pane.add(sp,BorderLayout.NORTH);
		pane.add(sp2,BorderLayout.SOUTH);
		//pane.add(panel3,BorderLayout.SOUTH);
		pane.add(window_panel,BorderLayout.CENTER);
		//pane.add(panel4,BorderLayout.SOUTH);
		
		
		setTitle("My new web browser");
		setSize(800,600);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	     }



           public class Go implements ActionListener
             {

		public void actionPerformed(ActionEvent ae)
                 {

			try
                          {

				window_pane.setPage(address_tf.getText());

			  } 

                        catch (MalformedURLException e)
                          {     // new URL() failed
				window_pane.setText("MalformedURLException: " + e);

			  }  

                        catch (IOException e) 
                          {               // openConnection() failed
				window_pane.setText("IOException: " + e);
			  }

	         }

	     }





public class Menugo implements ActionListener
{
   public void actionPerformed(ActionEvent e)
     {
	String str2 = e.getActionCommand();
      	  if(str2.equals("add tab"))
	  {
        	      
                        
	
                  }    
	
	
     }
}
 

public class MyAction implements ActionListener
  {
       public void actionPerformed(ActionEvent e)
	{
      	  String str = e.getActionCommand();
      	  if(str.equals("Add Tab"))
	  {
        	      String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
       	      if(!st.equals(""))
		{
		  
		
        		}
    	  }
     
               }
  }
	public static void main(String args[]) throws IOException
	 {
		WeBrowsertry3 wb = new WeBrowsertry3();
	 }

}


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12148
  • View blog
  • Posts: 45,161
  • Joined: 27-December 08

Re: error in HyperlinkListener in java...help

Posted 20 April 2011 - 07:17 PM

Which line?! We aren't mind readers! Post the error exactly as it appears on your compiler! We need to know what errors you are encountering, as well as where they are occurring!
Was This Post Helpful? 1
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: error in HyperlinkListener in java...help

Posted 20 April 2011 - 07:22 PM

...Topics merged...

Mac, the code does have a comment on the one where the error is...and he did tell us that the compiler is saying that it needs a semicolon there. I can confirm this.

OP, your problem is here:
window_ pane.addHyperlinkListener(new HyperlinkListener() 



You need to remove the space after the "_" character. Like this:
window_pane.addHyperlinkListener(new HyperlinkListener() 



And that should fix your problem.
Was This Post Helpful? 3
  • +
  • -

#10 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: error in HyperlinkListener in java...help

Posted 22 April 2011 - 09:46 PM

View PostDogstopper, on 20 April 2011 - 07:22 PM, said:

...Topics merged...

Mac, the code does have a comment on the one where the error is...and he did tell us that the compiler is saying that it needs a semicolon there. I can confirm this.

OP, your problem is here:
window_ pane.addHyperlinkListener(new HyperlinkListener() 



You need to remove the space after the "_" character. Like this:
window_pane.addHyperlinkListener(new HyperlinkListener() 





THNAKU U SO MUCH SIR.... so silly....ws jst laughing lloking at that error,,,,lol

And that should fix your problem.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1