Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Sizing Swing object on a JApplet

Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic