Results 1 to 11 of 11
- 04-11-2010, 12:00 AM #1
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
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.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);
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?Carpe Diem
Each day's a gift and not a given right
-
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(...).
- 04-11-2010, 12:23 AM #3
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.use this to your advantage and add your drawing panel to the JFrame contentPane, BorderLayout.CENTER.
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
-
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
- 04-11-2010, 12:44 AM #5
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
-
- 04-11-2010, 12:55 AM #7
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.Carpe Diem
Each day's a gift and not a given right
-
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?
- 04-11-2010, 12:59 AM #9
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
-
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!
- 04-11-2010, 01:20 AM #11
Similar Threads
-
JFrame resizing problem
By Bluefox815 in forum AWT / SwingReplies: 7Last Post: 02-24-2011, 07:56 PM -
Problem resizing JPanel on window resize
By Nyet in forum AWT / SwingReplies: 4Last Post: 11-27-2009, 03:13 AM -
Triangles java problem (basic help)
By adz06 in forum New To JavaReplies: 5Last Post: 10-31-2009, 06:58 PM -
Resizing Images in JPanel??
By NoNickName in forum New To JavaReplies: 1Last Post: 04-09-2009, 10:19 PM -
[SOLVED] very basic java problem
By sales1 in forum New To JavaReplies: 13Last Post: 08-20-2008, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks