Results 1 to 8 of 8
  1. #1
    bantes is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default I need some help with JPanel

    hello. I have a JFrame, I use a container for this. Now, i want to add two JPanels to the container and then add the container to the JFrame. My problem is that I cannot set the "size" of the JPanels. I want the first Jpanel to take the 1/3 of the screen vertical, and the other JPanel to take the rest of the Jframe. How can I do that? panel.setSize and setPreferedSize does not work .
    Do you have any suggestions?, because i really need it.
    thank you

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Which LayoutManager do you use?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    bantes is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    when i try to use the setsize and setpreferedsize i use borderlayout manager. I think that GridLayout is more flexible, but when i try to use it i get an exception error. I think that I am looking for a nice tutorial or a good code example on GridLayout. Here is my GridLayout code, which does not do the work and throws exception.

    static JPanel p = new JPanel( ) ;
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 10;
    c.gridheight = 10;
    c.weightx = 30;
    c.weighty = 30;
    c.fill = GridBagConstraints.NONE;

    p.setLayout(new GridBagLayout());
    contentPane.add(p,c );

  4. #4
    bantes is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Here is the Exception
    Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

    thanks for your interest mr. phHein

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    In your code, the line "contentPane.add(p,c);" will cause the exception.
    The second argument to add() must be a string name.
    The layout manager will use this name to choose where to put the object in its layout.
    GridBagLayout does not use this name. For GridBagLayout, the call is:
    Java Code:
             gridbag.setConstraints(button, c);
             add(button);
    You might consider using JSplitPane: How to Use Split Panes. Then there's no need for a layout manager.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Use a vertical BoxLayout and set the preferred size of each component.

  8. #8
    bantes is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM
  2. how to set image to a jpanel
    By masa in forum AWT / Swing
    Replies: 1
    Last Post: 03-02-2009, 08:36 AM
  3. How to use Jpanel
    By Manfizy in forum NetBeans
    Replies: 0
    Last Post: 02-19-2009, 12:34 PM
  4. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 03:58 PM
  5. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 05:04 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
  •