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

Sizing Swing object on a JApplet

Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
Hi Everyone,

Whenever I add the a Swing object to a JApplet, the Swing object will fill the entire window no matter what I try.

The code at the end of the e-mail boils down my difficulty into a simple example. Please see the code and comments in the init() method.

I also included the html after the java code so you can see what I'm experiencing for yourself.

Run the applet, uncomment //pane.add(label); at line 33, then run it again and you'll see what I'm talking about.

Thank You in Advance for Your Help,


import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.Font;

public class PaintApplet extends JApplet
private JLabel label = new JLabel("JLabel");
private Container pane;
public void init()
JPanel drawAreaJPanel = new TestCanvas();
pane = getContentPane();


// Attempts to control the size are ignored whenever label is displayed.
label.setBounds(10, 250, 20, 10);
label.setMinimumSize(new Dimension(20, 10));
label.setPreferredSize(new Dimension(20, 10));

// Label occupies entire screen, with or without null layout

// No effect, with or without null layout

public Dimension getPreferredSize()
return new Dimension(400, 300);

class TestCanvas extends JPanel
{public Dimension getPreferredSize()
return new Dimension(400, 300);

public void paint( Graphics g )
g.setFont(new Font(null, Font.PLAIN, 100));
g.drawString("Help!", 30, 100);

<meta name="generator" content="Modelworks IDE">
<h1>Test page for PaintApplet Class</h1><hr>
<applet code=PaintApplet.class width=300 height=300></applet>
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Some observations about your code:
1 - JApplet extends from Applet which extends Panel. The default layout manager for Panel is FlowLayout. So the content pane of a JApplet is (by default) FlowLayout. You can change it to another layout to suit your purposes.

2 - in Swing (any component with "J" prefix) we usually override the paintComponent method. Overriding paint, which is a Container method and is called to paint a container's components, will paint over all components. It's okay to do but it takes some care.

3 - calling super.paintComponent in the paintComponent method will tell Swing to paint the background which will prevent artifacts in your component. If you do decide to override paint in lieu of paintComponent you can call super.paint.

4 - the Graphics2D class allows for more fancy affects in drawing and the RenderingHint makes smooth (anti–aliased) lines.

[ May 29, 2004: Message edited by: Craig Wood ]
[ May 29, 2004: Message edited by: Craig Wood ]
I agree. Here's the link:
subject: Sizing Swing object on a JApplet
It's not a secret anymore!