set standard/default size of swing JPanel
Hi,
A JPanel grows bigger as more components are added. That means, that by default, when nothing is contained in it, it is not visible. The "setSize" method does nothing unless a component is contained. How can i set a default size, when no component is contained? Is there something like JPanel.setDefaultSizeAndDontShrink()? :) Thanks |
Quote:
Code:
import java.awt.*; |
Explanation: I placed the JPanel on the Container (A JFrame) and added a border (so you can visualize the size of the JPanel on the Container) and used the setPreferredSize(Dimension d) to a given size. You might want to play with the uncommented methods (setMin and MaxSize).
By default, JPanels use the FlowLayout Manager and JFrames use the BorderLayout Manager. I set the JFrame layout to FlowLayout, otherwise your JPanel would be put in the center of the BorderLayout and take all space available instead of using the prefferedSize. Try commenting out the line: setLayout(new FlowLayout()); and you will see what happens... Check the API about Layout Managers and ask if you get stuck :) |
Thanks a lot,
i still need work to do, but i think i am on the right track :) |
No problems nocturna_gr :). I don't know if you are using an IDE or a simple text editor, but I highly recommend you to use Eclipse. It will help you a lot to find methods that you are not aware of and it definitely saves a lot of time instead of checking the API all the time, for example:
Code:
JFrame frame = new JFrame(); Just a thought though ;) Good luck! |
All times are GMT -5. The time now is 01:58 PM. |