Canvas in a ScrollPane

How can you make a canvas nonresizable? The canvas automatically resizes when the frame is resized.

The default layout manager for java.awt.Frame is BorderLayout. BorderLayout will expand the components of its associated container along the regions to which they are attached. For example, a component added to the west or east sides will expand along the vertical axis, but not alter its width. A component added to the north or south sides will expand along the horizontal axis, but not alter its height. A component added to thecenter will expand along both the vertical and horizontal axes. When you added your canvas to the frame, if you did not specify the regionwhere it was to be placed, then it was placed in the center by default.

To eliminate all resizing behavior, you must choose a different layout manager for your container: in this case, a Frame. Which layoutmanager you choose will depend on exactly where you want the canvas placed. The following example uses FlowLayout to prevent a canvas from being resized after it is placed in a Frame. The canvas remains centered horizontally, but winds up being stuck on the top end of the Frame. If you do not desire this behavior, you will likely have to place the Canvas in a Panel that uses a differentlayout manager, and then place the Panel in a Frame.

import java.awt.*;import java.awt.event.*;public final class FixedCanvas {  public static void main(String[] args) {    Canvas canvas;    Frame frame;    WindowListener exitListener;    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {	Window window = e.getWindow();	window.setVisible(false);	window.dispose();	System.exit(0);      }    };    canvas = new Canvas();    canvas.setSize(100, 100);    canvas.setBackground(Color.white);    frame = new Frame();    frame.setBackground(;    frame.setLayout(new FlowLayout(FlowLayout.CENTER));    frame.add(canvas);    frame.addWindowListener(exitListener);    frame.pack();    frame.setVisible(true);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS