• Post Reply Bookmark Topic Watch Topic
  • New Topic

what am i doing wrong urgent

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am attempting to extend a plaindocument in order to pass an instance of it as a parameter to
JTextField's setDocument(). I need a JTextField that can only accept 30 characters.
public class CustomDocument extends PlainDocument
{
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
char[] source = str.toCharArray();
if(source.length > 30)
{
char[] result = new char[30];
for (int i = 0; i < 30; i++)
{
result[i] = source[i];
}
super.insertString(offs, new String(result, 0, 30), a);
}
else
super.insertString(offs, new String(source, 0, source.length), a);
}
}
fields[7] = new JTextField();
fields[7].setBounds(115, 474, 220, 25);
CustomDocument c = new CustomDocument();
fields[7].setDocument(c);

Well the problem that im having is that in the method insertString the str arguement that is being passed to the method is always blank, but the offs argument is correct. Did i forget something???
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
Not sure why yours doesn't work. But the code below should work for you.

Regards,
Manfred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!