2

I am implementing browser kind of project and I am getting an exception.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

public class WebPageDisplay extends JPanel{

    public WebPageDisplay() {  

         super(new BorderLayout());  
         try{
       JPanel webBrowserPanel = new JPanel(new BorderLayout());   
       // webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));   
        final JWebBrowser webBrowser = new JWebBrowser();   
        webBrowser.setBarsVisible(false);   
        webBrowser.setStatusBarVisible(true);   
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);   
        add(webBrowserPanel, BorderLayout.CENTER);
                    webBrowser.navigate("www.google.com");
        JScrollPane scrollPane = new JScrollPane();   
        Dimension preferredSize = scrollPane.getPreferredSize();   
        preferredSize.height += 20;   
        scrollPane.setPreferredSize(preferredSize);   
//      add(scrollPane, BorderLayout.NORTH);   
        add(scrollPane,new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER, 
            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));   

         }catch(Exception e){
             e.printStackTrace();

        }
      }

    public static void main(String[] args){
        NativeInterface.open();

         SwingUtilities.invokeLater(new Runnable() {

              public void run() {

                 WebPageDisplay webDisplay = new WebPageDisplay();

              }

            });

    }
}

Here is the exception.

java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:409)
    at java.awt.Container.addImpl(Container.java:1074)
    at java.awt.Container.add(Container.java:927)
    at WebPageDisplay.<init>(PBXPageDisplay.java:35)
    at WebPageDisplay$1.run(PBXPageDisplay.java:51)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:677)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:638)
    at java.awt.EventQueue$1.run(EventQueue.java:636)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:647)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Kara
  • 6,115
  • 16
  • 50
  • 57
developer
  • 9,116
  • 29
  • 91
  • 150

1 Answers1

7

You can't use GridBagConstraints if you're not using a GridBagLayout. The compiler is telling you that this makes no sense, and I quite agree.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @developer: The suggestion again is -- don't use GridBagConstraints since they make no sense when the layout being used is not GridBagLayout. I'm not sure what about this confuses you. Is this all your code above or is any part of it borrowed, since that's the only way I can understand your confusion. – Hovercraft Full Of Eels Sep 23 '12 at 05:57
  • @developer: so you now understand my recommendation? Why are you trying to use GridBagConstraints here any way? – Hovercraft Full Of Eels Sep 23 '12 at 06:00
  • i have create a window(in this i will show a webpage) which will again be placed in another window. – developer Sep 23 '12 at 06:03
  • Your comment does not make sense to me. Please clarify. – Hovercraft Full Of Eels Sep 23 '12 at 06:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17013/discussion-between-developer-and-hovercraft-full-of-eels) – developer Sep 23 '12 at 06:04