Ajuda para testar codigo abaixo

Amigos, peguei este codigo neste mesmo forum, ele faz um campo TextField ficar com a entrada em numeros com decimais, eu tentei mas não consegui simular um simples programa implementando ele.

Alguem poderia me ajudar a criar um simples TextField com este codigo abaixo?

Dario

ps

codigo extraido de

http://www.guj.com.br/posts/list/64872.java#515893

[code]
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import javax.swing.text.SimpleAttributeSet;

/**

  • Component JMoneyField

  • @author Dyorgio da Silva Nascimento
    */
    public class JMoneyField extends JFormattedTextField {

    private static final long serialVersionUID = -5712106034509737967L;
    private static final SimpleAttributeSet nullAttribute = new SimpleAttributeSet();

    /**

    • Creates a new instance of JMoneyField
      */
      public JMoneyField() {
      this.setHorizontalAlignment( JTextField.CENTER );
      this.setDocument(new MoneyFieldDocument());
      this.addFocusListener(new MoneyFieldFocusListener());
      this.setText(“0,00”);
      this.addCaretListener(new CaretListener(){
      public void caretUpdate(CaretEvent e) {
      if (e.getDot() != getText().length() ) {
      getCaret().setDot(getText().length());
      }
      }
      });
      }

    private final class MoneyFieldFocusListener extends FocusAdapter{
    public void focusGained(FocusEvent e) {
    selectAll();
    }
    }

    private final class MoneyFieldDocument extends PlainDocument {

     /**
      * 
      */
     private static final long serialVersionUID = -3802846632709128803L;
    
     public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
     	String original = getText(0,getLength());
     	
     	// Permite apenas digitar até 16 caracteres (9.999.999.999,99)
     	if (original.length()<16) {
     		StringBuffer mascarado = new StringBuffer();
     		if (a != nullAttribute) {
     			//limpa o campo
     			remove(-1,getLength());
     			
     			mascarado.append((original+str).replaceAll("[^0-9]",""));
      			for (int i = 0; i < mascarado.length(); i++){
      				if (!Character.isDigit(mascarado.charAt(i))){
      					mascarado.deleteCharAt(i);
      				}
      			}
      			Long number = new Long(mascarado.toString());
      			
      			mascarado.replace(0, mascarado.length(), number.toString());
      			
      			if ( mascarado.length() < 3 )
      			{
          			if ( mascarado.length() == 1 ) {
          				mascarado.insert(0,"0");
          				mascarado.insert(0,",");
          				mascarado.insert(0,"0");
          			}else if ( mascarado.length() == 2 ) {
          				mascarado.insert(0,",");
          				mascarado.insert(0,"0");
          			}
      			}else{
      				mascarado.insert(mascarado.length()-2,",");
      			}
      			
      			if ( mascarado.length() > 6 ) {
      				mascarado.insert(mascarado.length()-6, '.');
      				if (mascarado.length() > 10 ) {
      					mascarado.insert(mascarado.length()-10, '.');
      					if (mascarado.length() > 14 ) {
          					mascarado.insert(mascarado.length()-14, '.');
      					}
      				}
      			}
      			super.insertString(0, mascarado.toString(), a);
     		}else{
     			if (original.length() == 0){
     				super.insertString(0, "0,00", a);
     			}
     		}
     	}
     }
     
     @Override
     public void remove(int offs, int len) throws BadLocationException {
     	if ( len == getLength() ) {
     		super.remove(0, len);
     		if (offs != -1){
     			insertString(0, "",nullAttribute);
     		}
     	}else{
     		String original = getText(0, getLength()).substring(0, offs) + getText(0, getLength()).substring(offs+len);
     		super.remove(0, getLength());
     		insertString(0,original,null);
     	}
     }
    

    }
    }[/code]

Mas o código já existe, que mais você precisa? O que quer fazer? Use JMoneyField em suas classes ao invés de JTextField.

Eu preciso apenas de um exemplo de como rodar este codigo, tipo como compilo e testo ele?

Dario

Já criou alguma aplicação usando JTextField? Faça o mesmo usando o JMoneyField, pois, ele é um JTextField, uma especialização.

estou começando a entender agora, ele é um novo objeto migrado do TextField

como eu implemento ele na paleta do netbeans?

dario

O JMoneyField é um JTextField com características a mais, ele extends JTextField.
Na paleta do Netbeans? Sei lá… posso dizer (e não me leve a mal) que você tá mal acostumado… coloque essa classe em seu projeto e passe a utilizar ela, na mão. Ou então coloca um JTextField via paleta e vê sem tem como alterar para JMoneyField, alterar a classe dele. Não tenho resposta pra isso, só vendo.