Results 1 to 11 of 11
  1. #1
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default Basic Java GUI problem, JPanel not resizing.

    The past days I've been making a paint program to get familiar with graphics in java, I have a functioning program and now I want to be able to save the image.

    To accomplish this I've decided add a panel to the frame which I could save, instead of getting the graphics from the whole Frame due to getting the MenuBar and ToolBar as well then.

    My frame is generated with the following code

    Java Code:
    // Creating and adding stuff to JMenuBar and JToolBar
    ............
    
    JPanel malePanel = new JPanel();
    	
    	Container cp = frame.getContentPane();
    	cp.add(toolBar, BorderLayout.NORTH);
    	cp.add(malePanel, BorderLayout.CENTER);
    	frame.setJMenuBar(jmb);
        malePanel.add(drawShapes); // The shapes are drawn to JPanel instead of frame.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    However the JPanel does not cover the full image beneath the JToolBar, I've attached an image showing what it looks like. It shows pretty clearly where the JPanel is limited to.

    At first I believed it was because I defined the JPanel as final in order to be able to use it in my save function, but removing this and /* */ the save function didn't help.

    Adding a GridLayout obviously didn't work as it makes my toolbar half the frame and Panel half the frame. :(

    I want the JPanel to cover all of the area beneath the JToolBar, how can I accomplish this?
    Attached Thumbnails Attached Thumbnails Basic Java GUI problem, JPanel not resizing.-boundaries.jpg  
    Carpe Diem
    Each day's a gift and not a given right

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You should read up on how to use Swing/AWT layout managers, in particular the BorderLayout. Since a JFrame's contentPane (the container that holds things added to a JFrame) uses a default layout BorderLayout manager, use this to your advantage and add your drawing panel to the JFrame contentPane, BorderLayout.CENTER.

    Whatever you do, avoid the temptation to set a component's layout to null. With a few exceptions, this is usually a bad idea.

    Also, for my money, I'd try to save the drawn image as well, an image via ImageIO.write(...).

  3. #3
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    I've finished the saveImage function, but when I didn't use a Panel to draw on it saved everything including the menubar and the toolbar, I can save the panel as well, just need to resize it :)

    use this to your advantage and add your drawing panel to the JFrame contentPane, BorderLayout.CENTER.
    Isn't this exactly what I am doing? I've also tried adding the panel to south or "After_last_line". Neither seem to be working.

    Or am I missinterprenting what you are saying? :)

    The drawing panel of my code is "malePanel", native language instead of english, sorry if it is unclear :)
    Carpe Diem
    Each day's a gift and not a given right

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Cemi View Post
    I've finished the saveImage function, but when I didn't use a Panel to draw on it saved everything including the menubar and the toolbar, I can save the panel as well, just need to resize it :)

    Isn't this exactly what I am doing? I've also tried adding the panel to south or "After_last_line". Neither seem to be working.

    Or am I missinterprenting what you are saying? :)

    The drawing panel of my code is "malePanel", native language instead of english, sorry if it is unclear :)
    No, I see that you are correct, you are adding the drawing panel into the contentPanel at the BorderLayout.CENTER position. That suggests to me that the drawing panel (malePanel -- what language is this?) is in fact taking up the whole of the JFrame except for the tool bar and the top strip, but that for some reason, the drawing region is limited. It's hard to say without more code.

    Much luck

  5. #5
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    It's Norwegian :) male means to paint.

    I found the error I believe, after posting some of the code here I believe I spotted it ;) Going to test now.
    Last edited by Cemi; 04-11-2010 at 01:27 AM.
    Carpe Diem
    Each day's a gift and not a given right

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Cemi View Post
    It's Norwegian :) male means to paint.

    I found the error I believe, after posting some of the code here I believe I spotted it ;) Going to test now.
    OK, good luck. Please let us know what the culprit is.

  7. #7
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    This is how it turns out if I remove the line

    drawShapes.setPreferredSize(new Dimension(500, 500));

    Even if I do frame.setSize(500,500); and frame.setResizeable(false);.

    Neither does it help to write malePanel.setPreferredSize(500,500); But then again, then the panel would be limited in size again.
    Attached Thumbnails Attached Thumbnails Basic Java GUI problem, JPanel not resizing.-boundaries.jpg  
    Carpe Diem
    Each day's a gift and not a given right

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your malePanel, is it a single JPanel or does it happen to hold another JPanel or JComponent, the inner one that is actually drawn on?

  9. #9
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    It's just one single JPanel, it only contains the DrawShapes which is a the function which enables drawing on it.
    Carpe Diem
    Each day's a gift and not a given right

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yes, it contains draw shapes, but understand that the default layout for a JPanel is the FlowLayout. So drawshapes will be added into a flowlayout and won't fill malePanel.

    You can change malePanel's layout to BorderLayout and add drawShapes BorderLayout.CENTER
    Java Code:
        malePanel.setLayout(new BorderLayout());
        malePanel.add(drawShapes, BorderLayout.CENTER);

    or you could simply add drawShapes itself to the contentPane, BorderLayout.CENTER and get rid of malePanel which doesn't seem to be doing anything but messing things up.

    Much luck!

  11. #11
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    If I add it directly to contentPane as I did before my save function doesn't work :)

    Changing the layout did however work, thanks alot again! :D
    Carpe Diem
    Each day's a gift and not a given right

Similar Threads

  1. JFrame resizing problem
    By Bluefox815 in forum AWT / Swing
    Replies: 7
    Last Post: 02-24-2011, 07:56 PM
  2. Problem resizing JPanel on window resize
    By Nyet in forum AWT / Swing
    Replies: 4
    Last Post: 11-27-2009, 03:13 AM
  3. Triangles java problem (basic help)
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 06:58 PM
  4. Resizing Images in JPanel??
    By NoNickName in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 10:19 PM
  5. [SOLVED] very basic java problem
    By sales1 in forum New To Java
    Replies: 13
    Last Post: 08-20-2008, 08:33 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
  •