My way is to make a custom textfield extending Panel.
class MyTextField extends Panel
then you have a TextField as a member of the panel MyTextField. Whenever your component gains focus, you add the TextField to the panel, and when focus is lost you remove it. (use a lyout that makes the TextField fill the entire panel, like GridLayout(1,1)) In the paint method of the panel you paint the text. use FontMetrics to get information about the font, and calculate where to paint it acording to alignment.
public void paint(Graphics g)
{
g.setColor(..
g.fillRect(..
g.setColor(...
g.draw3dRect(..
g.setColor(...
g.drawString(
g.draw3dRect(...
This solution gives you a textfield that works exactly like a normal textfield when it is active (when you are writing text in it) However, when the input focus is moved to another component, the text is displayed acording to your rules of alignment and so on. Remember that you need to code a litle bit extra to support some of the features that exists in TextField. (Like echo char support, and graying when editable is set to false)
";-)
Ragnvald Barth
Software engineer