Wednesday, September 16, 2009

Right align/justify in textarea, java

1. Font has to be first of all changed to monospace. Using something like:
txtAreaResult.setFont(new Font("monospaced",Font.PLAIN,12));

2. When we append character to the stringbuffer, we use this method
private String rightAlign(String input, int pad) {

int length = pad - input.length();

if (length < 0)
return input.substring(0,input.length());

StringBuffer buffer = new StringBuffer(pad);
for(int i=0; i<length; i++)
buffer.append(" ");

return new String(buffer.append(input));
}

3. we can use append like this:
StringBuffer results = new StringBuffer();
results.append(rightAlign("Months", 6));

No comments: