Right align text in a textfield

Hi !How do i right align text in a text filed. i need to do it in such a way that it works with java 1.1.thanks in advancesamit
[176 byte] By [samitr] at [2007-9-26 3:21:06]
# 1

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

Ragnvald at 2007-6-29 11:37:38 > top of Java-index,Desktop,Core GUI APIs...