MaskFormatter

1 resposta
rafadgomes

To tendo um problema com a minha máscara. A mascara limita os caracteres a numeros e somente 4 + um ponto no meio. Mas estou com alguns problemas.

1- Quero que quando a pessoa digite apenas 3 numeros o 1º do textfield fique como 0. Por exemplo:

A pessoa digita 973… queria que ficasse assim 09.73 e nao 97.3

2- Quero que se a pessoa nao digitar pelo menos 3 numeros, apareca uma mensagem de erro avisando-a

Alguem pode me ajudar?! Muito obrigado! Ai em baixo esta o meu maskformatter.

private MaskFormatter criaMascara() {
MaskFormatter mascara = null;

try {
    mascara = new MaskFormatter("##.##");
    

} catch (Exception e) {
    JOptionPane.showMessageDialog(null,"Digite suas notas com os quatro numeros. Exemplo: 10.00");
}

return mascara;
}

1 Resposta

fsjr

Ai meu, bem tosquinho o exemplo, cria e compila ai que deve funcionar. É só uma idéia, implementa uma coisa mais profissional agora :slight_smile:

Abraços

package mascara;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;

import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

import org.apache.xml.utils.Trie;

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.text.NumberFormat;

public class ExemploMascara extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JPanel jPanel = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;
	
	

	private JFormattedTextField JFormattedTextField = null;

	private JFormattedTextField JFormattedTextField1 = null;

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Mascara");
			jLabel1 = new JLabel();
			jLabel1.setText("Mascara");
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(2);
			jPanel = new JPanel();
			jPanel.setLayout(gridLayout);
			jPanel.add(jLabel1, null);
			jPanel.add(getJFormattedTextField1(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJFormattedTextField(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes JFormattedTextField	
	 * 	
	 * @return javax.swing.JFormattedTextField	
	 */
	private JFormattedTextField getJFormattedTextField() {
		if (JFormattedTextField == null) {
			JFormattedTextField = new JFormattedTextField(criaMascara());
			
			JFormattedTextField.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					
					/**
					 * Método quando sai de foco JFormattedTextField
					 */
					if( JFormattedTextField.getText().trim().length() <= 3 ) {
						JOptionPane.showMessageDialog(null, "Digite pelo menos 3 números!");						
					}
					else  if( JFormattedTextField.getText().trim().length() == 4 ) {
						String[] texto = JFormattedTextField.getText().split("\\.");						
						JFormattedTextField.setText("0"+texto[0]+texto[1]);
					}
					
					
				}
			});
			
			
		}
		return JFormattedTextField;
	}

	/**
	 * This method initializes JFormattedTextField1	
	 * 	
	 * @return javax.swing.JFormattedTextField	
	 */
	private JFormattedTextField getJFormattedTextField1() {
		if (JFormattedTextField1 == null) {
			JFormattedTextField1 = new JFormattedTextField(criaMascara());			
			JFormattedTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					
					
					
					/**
					 * Método quando sai de foco JFormattedTextField1
					 */
					if( JFormattedTextField1.getText().trim().length() <= 3 ) {
						JOptionPane.showMessageDialog(null, "Digite pelo menos 3 números!");						
					}
					else  if( JFormattedTextField1.getText().trim().length() == 4 ) {
						String[] texto = JFormattedTextField1.getText().split("\\.");						
						JFormattedTextField1.setText("0"+texto[0]+texto[1]);
					}
					
					
				}
			});
			
		}
		return JFormattedTextField1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ExemploMascara thisClass = new ExemploMascara();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public ExemploMascara() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(309, 92);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("Exemplo");
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(jLabel, BorderLayout.NORTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	private MaskFormatter criaMascara() {
		MaskFormatter formatter = null;
		try { 
			 formatter = new MaskFormatter("##.##");			  
			 //formatter.setPlaceholderCharacter('.');
		} catch (Exception e) {			 
		}
		return formatter;
	} 

}
Criado 2 de novembro de 2006
Ultima resposta 3 de nov. de 2006
Respostas 1
Participantes 2