plaindocument gettext() not working...

T

tiewknvc9

Hi,

I have created a jtextfield that uses a defaultModel of a
PlainDocument.

I do this so that I can limit the input in the textfield to 3 digits.

The problem that seems to have arisen is that when I call
myTextField.getText(), nothing is returned unless specifically set with
setText();

This is especially annoying since I am trying to get user input in the
field.

Does anyone know why this is happening? What can I do to fix this?

Thanks
 
T

tiewknvc9

ok.

when I call
myVar.getText(), I get nothing returned unless I use setText() to set
the text of the textfield....

Thanks for the help!

this is the jtextfield class...
----------------------------------------------------

public class TextField3Nums extends JTextField implements
FocusListener{

public static String m_strVal;

public TextField3Nums(String str) {
super(str);
m_strVal = str;

this.addFocusListener(this);
}

public void focusGained(FocusEvent e) {}; // not needed


public void focusLost(FocusEvent e) {
// do checking
if (this.getText().length() == 0){
//set default value on exit from field
this.setText(m_strVal);
}
}

protected Document createDefaultModel() {
return new PlainDoc3Nums();
}

static class PlainDoc3Nums extends PlainDocument {

public void insertString(int iOffset, String str, AttributeSet
ats)
throws BadLocationException {

char[] insertChars = str.toCharArray();

boolean valid = true;
boolean fit = true;
if (insertChars.length + getLength() <= 4) {
for (int i = 0; i < insertChars.length; i++) {
if (insertChars == '.'){
valid = true;
break;
}
if (!Character.isDigit(insertChars)) {
valid = false;
break;
}
//limit to 3 numbers only, . not to be included
if (Character.isDigit(insertChars)){
if (insertChars.length + getLength() == 4){
if (!this.getText(0,3).contains(".")){

valid = false;
break;
}
}
}
}
}
else{
fit = false;
}

if (fit && valid){
super.insertString(iOffset, str, ats);
}else if (!fit){
//do nothing
}

if (getLength() == 0){
super.insertString(iOffset, m_strVal, ats);
}
}

//protected void
removeUpdate(AbstractDocument.DefaultDocumentEvent chng){
//super.removeUpdate(chng);

/*if (getLength() == 1){
try{
insertString(1, m_strVal, null);
}catch(BadLocationException ble){

}
}*/
//}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top