This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sizing Swing object on a JApplet

Lou Pelagalli
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!