File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Canvas resizing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Canvas resizing" Watch "Canvas resizing" New topic

Canvas resizing


Joined: Feb 03, 2005
Posts: 26
I would like to know how to resize a canvas which has been added my applet. And also is it possible to add a canvas to a JPanel.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
how to resize a canvas which has been added my applet
Depends on the layout manager of its Container. Some layout managers like GridLayout and BorderLayout ignore some or all of the size information about the components you give them to layout. Other layout managers like FlowLayout and GridBagLayout respect the size requests of their child components. So how to work with this?
Nesting components is one approach. The center section of a BorderLayout will expand its child component to fill itself in both horizontal and vertical dimensions (see BorderLayout api). So you can add the component to another component and add the second component to the BorderLayout. The intermediate component will expand to fill the center section of the BorderLayout and allow the component whose size you want to control to be displayed at its preferred size (which you may set).
Another approach is to use a layout manager that will respect a components preferred size and call setPreferredSize on the component or, if the component is a class you can override getPreferredSize in the class and return a Dimension that reperesents the size you want for the component. See Lesson: Laying Out Components Within a Container.

is it possible to add a canvas to a JPanel
Yes but it is recommended that we not mix AWT and Swing components. You can use a JPanel in Swing just like you use a Canvas in the AWT. If painting you use protected void paintComponent(Graphics g) instead of public void paint(Graphics g). See Implementing a Custom Component.
I agree. Here's the link:
subject: Canvas resizing
It's not a secret anymore!