Results 1 to 2 of 2
  1. #1
    ToXSiK is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default GridBagLayout and JFrames/JPanels!

    Hey everyone, first post here, so hello! =)

    Anyways, I am making a level editor for my game.

    On a JFrame I am trying to organize 5 components (A JPanel component called Map that just overrides paintComponent() method to draw the map), 2 JButton's, a JComboBox, and a JLabel. I organized the buttons and the JComboBox into its own JPanel via GridBagLayout and then added it to my JFrame (800x600, btw) by using
    Java Code:
    frame.add(panel1, BorderLayout.SOUTH);
    and the graphics panel containing the JPanel object called Map that overrides paintComponent() in it's own JPanel using GridBagLayout by doing

    Java Code:
    frame.add(panel2, BorderLayout.NORTH);
    I have figured out GridBagLayout and have set the size of all the elements on the screen and organized them correctly using setPrefferedSize() and the GridBagConstraints.

    So, basically, I add the Graphics object to the top of the frame and the button panel object to the bottom of the frame, and everything works!

    Except, when I call repaint on the Map object, map is suddently resized and overlaps the buttons on the botton! (BorderLayout.SOUTH)

    :(

    Any suggestions? Sorry, putting code to demonstrate this would be complicated.

    Thanks for the help, also this may belong in 2d, sorry if it does!

    ToXSiK

    PS, Images may help! Here you go:

    Before (This is how I want it to look even after calling repaint)



    After (When calling repaint to update the screen, this happens :( )

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

    Default

    I have figured out GridBagLayout and have set the size of all the elements on the screen and organized them correctly using setPrefferedSize()
    There is no need to call setPreferredSize(). All components have a preferred size and the layout manager uses that information to lay out the components.

    The exception is your custom component. Since you are doing custom painting, you need to implement the getPreferredSize() method to return the proper size. Or you can invoke setPreferredSize() on this component (although overriding the getPreferredSize() method is the better solution).

    Sorry, putting code to demonstrate this would be complicated.
    Well, the problem is with your code. So you need to isolate the problem by creating a Short, Self Contained, Correct Example. We can't guess what you might be doing wrong.

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  2. JPanels and JFrames
    By Dean29126 in forum New To Java
    Replies: 11
    Last Post: 01-16-2011, 03:39 PM
  3. JFrames and JPanels with ActionListeners
    By atomant in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 07:26 PM
  4. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  5. Adding JPanels to JFrames based on x-y co-ordinates
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 07-24-2008, 10:41 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
  •