Tame Truncated Text

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() + " ");    }}
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist