Results 1 to 3 of 3
  1. #1
    jackfletcher is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Swing Web Browser problems

    Hi guys,

    I'm trying to create a simple web browser using Swing and AWT.
    So far I only have the basic structure written and plan to add more but I have encountered a pitfall in that when my code is run I am presented with two windows.
    One is sized properly and has no title, the other has the correct title of 'My Web Browser' but is not sized and neither display the components which I have added eg. the JEditorPane to display the html page.

    My code is below I would appreciate any points in the right direction!
    Thanks.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.swing.*;
    import javax.swing.event.HyperlinkListener;
    
    public class BrowserFrame {
    	
    	// Components:
    	private JFrame frame;
        private JPanel panelTop;	    
        private JEditorPane editor;
        private JScrollPane scroll;
    	private JTextField addressBar;
    	private JButton goButton;
    	private URL url;
    	
    	// Constructor -----
    		public BrowserFrame()
    		{
    			// Set the frame title
    			super("My Web Browser");
    			
    			// Create the components
    			createComponents();
    			
    			// Set the frame size
    			frame.setSize(800,600);
    			
    			// Set frame as visible
    			frame.setVisible(true);
    			
    			// Set default close operation
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			
    			// Add top panel to top of the frame
    			frame.add(BorderLayout.NORTH, panelTop);
    			
    			// Add address bar and go button to the top panel
    			panelTop.add(addressBar);
    			panelTop.add(goButton);
    			
    			// Add scroll pane to frame centre
    			frame.add(BorderLayout.CENTER, scroll);
    			
    			// Add more components below: -------
    			
    			
    			
    		
    		} // close BrowserFrame constructor
    		
    		
    		// Create all components
    		private void createComponents()
    		{
    			createFrame();
    			createTopPanel();
    			setURL();
    			createJEditorPane();
    			createJScrollPane();
    			createAddressBar();
    			createGoButton();
    		}
    		
    		// Create the JFrame
    		private void createFrame()
    		{
    			frame = new JFrame();
    		}
    		
    		// Create the top panel to hold text field and button
    		private void createTopPanel()
    		{
    			panelTop = new JPanel();
    		}
    		
    		// Set the URL
    		private void setURL()
    		{
    			try
    	    	{
    	    		url = new URL("http://www.google.com");
    	    	}
    	    	catch(MalformedURLException mue)
    	    	{
    	    		JOptionPane.showMessageDialog(null, mue);
    	    	}
    		}
    		
    		// Create the JEditorPane
    		private void createJEditorPane()
    		{
    			try
    	    	{
    	    		editor = new JEditorPane(url);
    	    		
    	    		// Set the editor pane to false
    	    		editor.setEditable(false);
    	    	}
    	    	catch(IOException ioe)
    	    	{
    	    		JOptionPane.showMessageDialog(null,ioe);
    	    	}
    		}
    		
    		// Create the JScrollPane
    		private void createJScrollPane()
    		{
    			scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		}
    		
    		// Create the address bar
    		private void createAddressBar()
    		{
    			addressBar = new JTextField();
    		}
    		
    		// Create the go button
    		private void createGoButton()
    		{
    			goButton = new JButton();
    		}
    		
    		public static void main(String[] args)
    		{
    			new BrowserFrame();
    		}
    		
    		    
    } // End of class BrowserFrame
    Last edited by jackfletcher; 12-12-2012 at 03:45 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Swing Web Browser problems

    Quote Originally Posted by jackfletcher View Post
    ... when my code is run I am presented with two windows.
    One is sized properly and has no title, the other has the correct title of 'My Web Browser' but is not sized and neither display the components which I have added eg. the JEditorPane to display the html page.

    My code is below ...
    Java Code:
    public class BrowserFrame {
    :
    :
    		public BrowserFrame()
    		{
    			// Set the frame title
    			super("My Web Browser");
    			:
    		}
    } // End of class BrowserFrame
    That code doesn't compile, so I don't see how you can be presented with any windows at all.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Swing Web Browser problems

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Embeded Browser Issue:Swing with JDIC
    By srinu_pamidi in forum AWT / Swing
    Replies: 0
    Last Post: 09-14-2012, 05:59 PM
  2. Problems with playing games and such in a browser
    By ThatBlackGuy in forum Java Applets
    Replies: 8
    Last Post: 04-26-2012, 01:25 AM
  3. Urgent - Browser Integration In Swing!
    By tavy88 in forum Advanced Java
    Replies: 7
    Last Post: 04-13-2011, 08:20 PM
  4. Replies: 15
    Last Post: 03-25-2010, 03:34 PM
  5. Swing in browser
    By trust in forum AWT / Swing
    Replies: 6
    Last Post: 07-10-2009, 01:28 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
  •