Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Why Alignment of the opened URL is not proper?

  1. #1
    Ashi is offline Member
    Join Date
    Jan 2012
    Location
    Pune ,Maharashtra
    Posts
    9
    Rep Power
    0

    Default Why Alignment of the opened URL is not proper?

    Hello,

    I have tried this code for making Browser. But After Executing the code the alignment of URL is not proper what I can do?


    package urlpkg;



    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;
    import javax.swing.JFrame;

    public class Entry extends JFrame implements HyperlinkListener
    {

    // These are the buttons for iterating through the page list.
    private JButton backButton, forwardButton;



    // Page location text field.
    private JTextField locationTextField;

    // Editor pane for displaying pages.
    private JEditorPane displayEditorPane;

    // Browser's list of pages that have been visited.
    private ArrayList pageList = new ArrayList();

    // Constructor for Web Browser.
    public Entry()
    {
    setTitle("Wyzo");
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(9500,1000);

    // Handle closing events.
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    actionExit();
    }
    });


    /*


    */


    // Set up file menu.
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenu viewMenu = new JMenu("View");
    JMenu historyMenu = new JMenu("History");
    JMenu bookmarksMenu = new JMenu("Bookmarks");
    JMenu toolsMenu = new JMenu("Tools");
    JMenu helpsMenu = new JMenu("Helps");




    fileMenu.setMnemonic(KeyEvent.VK_F);
    editMenu.setMnemonic(KeyEvent.VK_E);
    viewMenu.setMnemonic(KeyEvent.VK_V);
    historyMenu.setMnemonic(KeyEvent.VK_H);
    bookmarksMenu.setMnemonic(KeyEvent.VK_B);
    toolsMenu.setMnemonic(KeyEvent.VK_T);
    helpsMenu.setMnemonic(KeyEvent.VK_H);

    JMenuItem New = new JMenuItem("New",KeyEvent.VK_N);
    JMenuItem Open = new JMenuItem("Open",KeyEvent.VK_O);
    JMenuItem Save = new JMenuItem("Save",KeyEvent.VK_S);
    JMenuItem SaveAs = new JMenuItem("Save As",KeyEvent.VK_A);
    JMenuItem Print = new JMenuItem("Print",KeyEvent.VK_P);
    JMenuItem Exitm = new JMenuItem("Exit",KeyEvent.VK_X);

    JMenuItem cut= new JMenuItem("Cut",KeyEvent.VK_N);
    JMenuItem copy= new JMenuItem("Copy",KeyEvent.VK_N);
    JMenuItem paste= new JMenuItem("Paste",KeyEvent.VK_N);
    JMenuItem delete= new JMenuItem("Delete",KeyEvent.VK_N);





    Exitm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionExit();
    }
    });


    fileMenu.add(New);
    fileMenu.add(Open);
    fileMenu.add(Save);
    fileMenu.add(SaveAs);
    fileMenu.add(Print);
    fileMenu.add(Exitm);

    editMenu.add(cut);
    editMenu.add(copy);
    editMenu.add(paste);
    editMenu.add(delete);

    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(viewMenu);
    menuBar.add(historyMenu);
    setJMenuBar(menuBar);



    // Set up button panel.
    JPanel buttonPanel = new JPanel();
    backButton = new JButton("< Back");

    backButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionBack();
    }
    });
    backButton.setEnabled(false);
    buttonPanel.add(backButton);

    forwardButton = new JButton("Forward >");
    forwardButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionForward();
    }
    });
    forwardButton.setEnabled(false);
    buttonPanel.add(forwardButton);


    ImageIcon ic4=new ImageIcon("d:\\r1.gif");
    JLabel li4=new JLabel(ic4);
    buttonPanel.add(li4);

    ImageIcon ic3=new ImageIcon("d:\\p1.gif");
    JLabel li3=new JLabel(ic3);
    buttonPanel.add(li3);

    ImageIcon ic2=new ImageIcon("d:\\h1.gif");
    JLabel li2=new JLabel(ic2);
    buttonPanel.add(li2);

    ImageIcon ic1=new ImageIcon("d:\\s1.gif");
    JLabel li1=new JLabel(ic1);
    buttonPanel.add(li1);

    ImageIcon ic=new ImageIcon("d:\\d1.gif");
    JLabel li=new JLabel(ic);
    buttonPanel.add(li);
    locationTextField = new JTextField(35);
    locationTextField.addKeyListener(new KeyAdapter() {
    public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    actionGo();
    }
    }
    });
    buttonPanel.add(locationTextField);
    JButton goButton = new JButton("GO");
    goButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionGo();
    }
    });
    buttonPanel.add(goButton);

    // Set up page display.
    displayEditorPane = new JEditorPane();
    displayEditorPane.setContentType("text/html");
    displayEditorPane.setEditable(false);
    displayEditorPane.addHyperlinkListener(this);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(new JScrollPane(displayEditorPane),
    BorderLayout.CENTER);
    }

    // Exit this program.
    private void actionExit() {
    System.exit(0);
    }

    // Go back to the page viewed before the current page.
    private void actionBack() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
    showPage(
    new URL((String) pageList.get(pageIndex - 1)), false);
    }
    catch (Exception e) {}
    }

    // Go forward to the page viewed after the current page.
    private void actionForward() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
    showPage(
    new URL((String) pageList.get(pageIndex + 1)), false);
    }
    catch (Exception e) {}
    }

    // Load and show the page specified in the location text field.
    private void actionGo() {
    URL verifiedUrl = verifyUrl(locationTextField.getText());
    if (verifiedUrl != null) {
    showPage(verifiedUrl, true);
    } else {
    showError("Invalid URL");
    }
    }

    // Show dialog box with error message.
    private void showError(String errorMessage) {
    JOptionPane.showMessageDialog(this, errorMessage,"Error", JOptionPane.ERROR_MESSAGE);
    }

    // Verify URL format.
    private URL verifyUrl(String url) {
    // Only allow HTTP URLs.
    if (!url.toLowerCase().startsWith("http://"))
    return null;

    // Verify format of URL.
    URL verifiedUrl = null;
    try {
    verifiedUrl = new URL(url);
    } catch (Exception e) {
    return null;
    }

    return verifiedUrl;
    }

    /* Show the specified page and add it to
    the page list if specified. */
    private void showPage(URL pageUrl, boolean addToList)
    {
    // Show hour glass cursor while crawling is under way.
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_C URSOR));

    try {
    // Get URL of page currently being displayed.
    URL currentUrl = displayEditorPane.getPage();

    // Load and display specified page.
    displayEditorPane.setPage(pageUrl);

    URL newUrl = displayEditorPane.getPage();

    if (addToList) {
    int listSize = pageList.size();
    if (listSize > 0) {
    int pageIndex = pageList.indexOf(currentUrl.toString());
    if (pageIndex < listSize - 1) {
    for (int i = listSize - 1; i > pageIndex; i--) {
    pageList.remove(i);
    }
    }
    }
    pageList.add(newUrl.toString());
    }

    locationTextField.setText(newUrl.toString());

    updateButtons();
    }
    catch (Exception e)
    {
    showError("Unable to load page");
    }
    finally
    {
    // Return to default cursor.
    setCursor(Cursor.getDefaultCursor());
    }
    }
    private void updateButtons() {
    if (pageList.size() < 2)
    {
    backButton.setEnabled(false);
    forwardButton.setEnabled(false);
    }
    else
    {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    backButton.setEnabled(pageIndex > 0);
    forwardButton.setEnabled(
    pageIndex < (pageList.size() - 1));
    }
    }

    public void hyperlinkUpdate(HyperlinkEvent event) {
    HyperlinkEvent.EventType eventType = event.getEventType();
    if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
    if (event instanceof HTMLFrameHyperlinkEvent) {
    HTMLFrameHyperlinkEvent linkEvent =(HTMLFrameHyperlinkEvent) event;
    HTMLDocument document =(HTMLDocument) displayEditorPane.getDocument();
    document.processHTMLFrameHyperlinkEvent(linkEvent) ;
    }
    else
    {
    showPage(event.getURL(), true);
    }
    }
    }


    public static void main(String[] args) {
    new Entry();

    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Why Alignment of the opened URL is not proper?

    the alignment of URL is not proper
    Please explain what "alignment of URL" means.
    Are you taking about a String that contains a URL being displayed in a GUI component?
    The URL class is used by many other classes to find resources.

    Please Edit your post and wrap your code with code tags:
    [code]
    <YOUR CODE HERE>
    [/code]
    to save its formatting

  3. #3
    Ashi is offline Member
    Join Date
    Jan 2012
    Location
    Pune ,Maharashtra
    Posts
    9
    Rep Power
    0

    Post Re: why alignment of URL is not proper?

    Hi,


    when i an going to execute this code,the which gets opened seems something different because the alignment of the component within that link is not proper.
    e.g., When I am going to open Google , I observed that the place of some Components is changed.


    Java Code:
    package urlpkg;
    
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;
    import javax.swing.JFrame;
    
    public class Entry extends JFrame implements HyperlinkListener
    	{
    	
    	// These are the buttons for iterating through the page list.
    	private JButton backButton, forwardButton;
    	
    	    
    	
    	// Page location text field.
    	private JTextField locationTextField;
    
    	// Editor pane for displaying pages.
    	private JEditorPane displayEditorPane;
    
    	// Browser's list of pages that have been visited.
    	private ArrayList pageList = new ArrayList();
    
    	// Constructor for Web Browser.
         public Entry()
         {
    		setTitle("Wyzo");
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		setSize(9500,1000);
    	   
    	    // Handle closing events.
    	      addWindowListener(new WindowAdapter() {
    	    	  public void windowClosing(WindowEvent e) {
    	    		  actionExit();
    	    	  }
    	      });
    	      
    	      
    	    /* 
    	      
    	     
    	       */ 
    	  	
    	  	
    	  		// Set up file menu.
    	      JMenuBar menuBar = new JMenuBar();
    	      JMenu fileMenu = new JMenu("File");
    	      JMenu editMenu = new JMenu("Edit");
    	      JMenu viewMenu = new JMenu("View");
    	      JMenu historyMenu = new JMenu("History");
    	      JMenu bookmarksMenu = new JMenu("Bookmarks");
    	      JMenu toolsMenu = new JMenu("Tools");
    	      JMenu helpsMenu = new JMenu("Helps");
    	     
    	      
    	     
    	      
    	      fileMenu.setMnemonic(KeyEvent.VK_F);
    	      editMenu.setMnemonic(KeyEvent.VK_E);
    	      viewMenu.setMnemonic(KeyEvent.VK_V);
    	      historyMenu.setMnemonic(KeyEvent.VK_H);
    	      bookmarksMenu.setMnemonic(KeyEvent.VK_B);
    	      toolsMenu.setMnemonic(KeyEvent.VK_T);
    	      helpsMenu.setMnemonic(KeyEvent.VK_H);
    	
    	      JMenuItem New = new JMenuItem("New",KeyEvent.VK_N);
    	      JMenuItem Open = new JMenuItem("Open",KeyEvent.VK_O);
    	      JMenuItem Save = new JMenuItem("Save",KeyEvent.VK_S);
    	      JMenuItem SaveAs = new JMenuItem("Save As",KeyEvent.VK_A);
    	      JMenuItem Print = new JMenuItem("Print",KeyEvent.VK_P);
    	      JMenuItem Exitm = new JMenuItem("Exit",KeyEvent.VK_X);
    	      
    	      JMenuItem cut= new JMenuItem("Cut",KeyEvent.VK_N);
    	      JMenuItem copy= new JMenuItem("Copy",KeyEvent.VK_N);
    	      JMenuItem paste= new JMenuItem("Paste",KeyEvent.VK_N);
    	      JMenuItem delete= new JMenuItem("Delete",KeyEvent.VK_N);
    	      
    	      
    	     
    	      
    	      
    	      Exitm.addActionListener(new ActionListener() {
    	    	  public void actionPerformed(ActionEvent e) {
    	    		  actionExit();
    	    	  }
    	      });
    	     
    		  
    	      fileMenu.add(New);
    	      fileMenu.add(Open);
    	      fileMenu.add(Save);
    	      fileMenu.add(SaveAs);
    	      fileMenu.add(Print);
    	      fileMenu.add(Exitm);
    	      
    	      editMenu.add(cut);
    	      editMenu.add(copy);
    	      editMenu.add(paste);
    	      editMenu.add(delete);
    	      
    	      menuBar.add(fileMenu);
    	      menuBar.add(editMenu);
    	      menuBar.add(viewMenu);
    	      menuBar.add(historyMenu);
    	      setJMenuBar(menuBar);
    	     
    	      
    	      
    	      // Set up button panel.
    	      JPanel buttonPanel = new JPanel();
    	      backButton = new JButton("< Back");
    	      
    	      backButton.addActionListener(new ActionListener() {
    	    	  public void actionPerformed(ActionEvent e) {
    	    		  actionBack();
    	    	  }
    	      });
    	      backButton.setEnabled(false);
    	      buttonPanel.add(backButton);
    	      
    	      forwardButton = new JButton("Forward >");
    	      forwardButton.addActionListener(new ActionListener() {
    	    	  public void actionPerformed(ActionEvent e) {
    	    		  actionForward();
    	    	  }
    	      });
    	      forwardButton.setEnabled(false);
    		buttonPanel.add(forwardButton);
    		
    		 
    	      ImageIcon ic4=new ImageIcon("d:\\r1.gif");
    	      JLabel li4=new JLabel(ic4);
    	      buttonPanel.add(li4);
    	      
    	      ImageIcon ic3=new ImageIcon("d:\\p1.gif");
    	      JLabel li3=new JLabel(ic3);
    	      buttonPanel.add(li3);
    	      
    	      ImageIcon ic2=new ImageIcon("d:\\h1.gif");
    	      JLabel li2=new JLabel(ic2);
    	      buttonPanel.add(li2);
    	     
    	      ImageIcon ic1=new ImageIcon("d:\\s1.gif");
    	      JLabel li1=new JLabel(ic1);
    	      buttonPanel.add(li1);
    	      
    	      ImageIcon ic=new ImageIcon("d:\\d1.gif");
    	      JLabel li=new JLabel(ic);
    	      buttonPanel.add(li);
    		locationTextField = new JTextField(35);
    		locationTextField.addKeyListener(new KeyAdapter() {
    			public void keyReleased(KeyEvent e) {
    				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    					actionGo();
    				}
    			}
    		});
    		buttonPanel.add(locationTextField);
    		JButton goButton = new JButton("GO");
    		goButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				actionGo();
    			}
    		});
    		buttonPanel.add(goButton);
    
    		// Set up page display.
    		displayEditorPane = new JEditorPane();
    		displayEditorPane.setContentType("text/html");
    		displayEditorPane.setEditable(false);
    		displayEditorPane.addHyperlinkListener(this);
    
    		getContentPane().setLayout(new BorderLayout());
    		getContentPane().add(buttonPanel, BorderLayout.NORTH);
    		getContentPane().add(new JScrollPane(displayEditorPane),
    				BorderLayout.CENTER);
         }	
    
    	// Exit this program.
    	private void actionExit() {
    			System.exit(0);
    		}
    
    	// Go back to the page viewed before the current page.
    	private void actionBack() {
    		URL currentUrl = displayEditorPane.getPage();
    		int pageIndex = pageList.indexOf(currentUrl.toString());
    		try {
    			showPage(
    					new URL((String) pageList.get(pageIndex - 1)), false);
    		}
    		catch (Exception e) {}
    		}
    
    	// Go forward to the page viewed after the current page.
    	private void actionForward() {
    		URL currentUrl = displayEditorPane.getPage();
    		int pageIndex = pageList.indexOf(currentUrl.toString());
    		try {
    			showPage(
    					new URL((String) pageList.get(pageIndex + 1)), false);
    			}
    		catch (Exception e) {}
    		}
    
    	// Load and show the page specified in the location text field.
    	private void actionGo() {
    		URL verifiedUrl = verifyUrl(locationTextField.getText());
    		if (verifiedUrl != null) {
    			showPage(verifiedUrl, true);
    		} else {
    			showError("Invalid URL");
    		}
    		}
    
    	// Show dialog box with error message.
    	private void showError(String errorMessage) {
    	JOptionPane.showMessageDialog(this, errorMessage,"Error", JOptionPane.ERROR_MESSAGE);
    	}
    
    	// Verify URL format.
    	private URL verifyUrl(String url) {
    	// Only allow HTTP URLs.
    	if (!url.toLowerCase().startsWith("http://"))
    	return null;
    
    	// Verify format of URL.
    	URL verifiedUrl = null;
    	try {
    	verifiedUrl = new URL(url);
    	} catch (Exception e) {
    	return null;
    	}
    
    	return verifiedUrl;
    	}
    
    	/* Show the specified page and add it to
    	the page list if specified. */
    	private void showPage(URL pageUrl, boolean addToList)
    		{
    		// Show hour glass cursor while crawling is under way.
    		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    
    		try {
    			// Get URL of page currently being displayed.
    			URL currentUrl = displayEditorPane.getPage();
    
    			// Load and display specified page.
    			displayEditorPane.setPage(pageUrl);
    	
    			URL newUrl = displayEditorPane.getPage();
    
    			if (addToList) {
    				int listSize = pageList.size();
    				if (listSize > 0) {
    					int pageIndex =	pageList.indexOf(currentUrl.toString());
    					if (pageIndex < listSize - 1) {
    						for (int i = listSize - 1; i > pageIndex; i--) {
    							pageList.remove(i);
    						}
    					}
    				}
    				pageList.add(newUrl.toString());
    			}
    
    			locationTextField.setText(newUrl.toString());
    
    			updateButtons();
    		}
    		catch (Exception e)
    		{
    			showError("Unable to load page");
    		}
    		finally
    		{
    			// Return to default cursor.
    			setCursor(Cursor.getDefaultCursor());
    		}
    		}
    		private void updateButtons() {
    			if (pageList.size() < 2)
    			{
    				backButton.setEnabled(false);
    				forwardButton.setEnabled(false);
    			} 
    			else
    			{
    				URL currentUrl = displayEditorPane.getPage();
    				int pageIndex = pageList.indexOf(currentUrl.toString());
    				backButton.setEnabled(pageIndex > 0);
    				forwardButton.setEnabled(
    						pageIndex < (pageList.size() - 1));
    			}
    		}
    
    	public void hyperlinkUpdate(HyperlinkEvent event) {
    	HyperlinkEvent.EventType eventType = event.getEventType();
    	if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
    	if (event instanceof HTMLFrameHyperlinkEvent) {
    	HTMLFrameHyperlinkEvent linkEvent =(HTMLFrameHyperlinkEvent) event;
    	HTMLDocument document =(HTMLDocument) displayEditorPane.getDocument();
    	document.processHTMLFrameHyperlinkEvent(linkEvent);
    	} 
    	else 
    	{
    	showPage(event.getURL(), true);
    	}
    	}
    	}
    
    
    	public static void main(String[] args) {
    		new Entry();
    		
    	}
    
    }
    Last edited by Norm; 01-09-2012 at 12:52 PM. Reason: added code tags

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why Alignment of the opened URL is not proper?

    code like
    this
    is no
    fun
    to read
    .
    read
    what Norm
    posted
    and
    try to
    edit
    your post
    .
    DarrylBurke likes this.

  5. #5
    Ashi is offline Member
    Join Date
    Jan 2012
    Location
    Pune ,Maharashtra
    Posts
    9
    Rep Power
    0

    Default Re: Why Alignment of the opened URL is not proper?

    I am sending screenshots of the output for the code written previously for Browser.preview.bmp

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Why Alignment of the opened URL is not proper?

    the alignment of the component within that link is not proper.
    Please explain what is not proper.

Similar Threads

  1. To many opened threads
    By jotremar in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 07-08-2010, 12:47 PM
  2. writing to an opened file..
    By eponcedeleon in forum Advanced Java
    Replies: 1
    Last Post: 02-23-2010, 09:01 PM
  3. how to check the window has already opened
    By javastuden in forum AWT / Swing
    Replies: 3
    Last Post: 01-29-2010, 07:26 AM
  4. Reading an already opened file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-25-2008, 06:50 PM
  5. How to know the ports opened by JVM
    By sandeepspatil in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 01:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •