Tame Truncated Text

You may notice that the first or last character in a Label’s text can get partially chopped off by some implementations of the Java Virtual Machine. Older versions of browsers seem particularly poor in this respect. Oddly, the left side of a right justified Label is often the victim. I suspect the weakness is in the accuracy of the FontMetrics which are supposed to provide the width of each character, but this doesn’t help when you just want to get a good looking applet or application developed. In the following class, each Label’s text is padded with spaces to avoid truncation.

 import java.awt.*;public class PaddedLabel extends Label {// Constructor, left justification    public PaddedLabel(String text) {        this(text, LEFT);    }// Constructor, specified justification    public PaddedLabel(String text, int justification) {        super(" " + text.trim() + " ", justification);    }// Accessor, returns text without padding    public String getText() {        return super.getText().trim();    }// Accessor, sets text with padding    public void setText(String text) {        super.setText(" " + text.trim() + " ");    }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: