Campo JFotmattedTextField

Cl?dio Rocha post seu codigo inteiro fica melhor para analisar sua situacao…

[quote=Cl?dio Rocha]
Mudei o código conforme a seguir para o JRadioButton fosse selecionado, transferisse o foco para o JFormattedTextField txtNrRecFed, mas ainda não está funcionando. Alguém tem uma sugestão?

[code]private void optCNPJActionPerformed(java.awt.event.ActionEvent evt) {
if(optCNPJ.isSelected()==true)
txtNrRecFed.requestFocus();
try{
mask = new MaskFormatter("##.###.###/####-##");
txtNrRecFed = new JFormattedTextField(mask);
}catch(ParseException e){}

// TODO add your handling code here:[/code][/quote]

Cl?dio Rocha post seu codigo inteiro, fica melhor para analisar onde esta o errro

A seguir o código em questão:

[code]
import java.awt.event.ActionEvent;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class TelaEmpresa extends javax.swing.JFrame{

private MaskFormatter mask = null;

/** Creates new form TelaEmpesa */
public TelaEmpresa() {
    initComponents();

}

@SuppressWarnings(“unchecked”)

//Generate Code

private void txtAutuadoActionPerformed(java.awt.event.ActionEvent evt) {
optCNPJ.requestFocus();
// TODO add your handling code here:
}

private void optCNPJActionPerformed(java.awt.event.ActionEvent evt) {
if(optCNPJ.isSelected()==true)
txtNrRecFed.requestFocus();
try{
mask = new MaskFormatter("##.###.###/####-##");
txtNrRecFed = new JFormattedTextField(mask);
}catch(ParseException e){}

// TODO add your handling code here:

}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TelaEmpresa().setVisible(true);
}
});
}

// Variables declaration - do not modify                     
private javax.swing.JButton cmdGravar;
private javax.swing.JButton cmdLimpar;
private javax.swing.JButton cmdSair;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JRadioButton optCNPJ;
private javax.swing.JRadioButton optCPF;
private javax.swing.JRadioButton optEmbarc;
private javax.swing.ButtonGroup optGrupoCatAut;
private javax.swing.ButtonGroup optGrupoRecFed;
private javax.swing.JRadioButton optTransCarga;
private javax.swing.JRadioButton optTransPass;
private javax.swing.JTextField txtAutuado;
private javax.swing.JFormattedTextField txtNrRecFed;
// End of variables declaration                   

}[/code]

Ainda não funciona. Alguém tem uma sugestão.

Gente eu acho que o caminho da praia é por aqui ó:

[code]public class TelaEmpresa extends JFrame {
private static final String MSK_CNPJ = “##.###.###/####-##”;
private static final String MSK_CPF = “###.###.###.##”;

private JFormattedTextField txtCodigo = new JFormattedTextField();

private JRadioButton optCNPJ = new JRadioButton("Cnpj");
private JRadioButton optCPF = new JRadioButton("Cpf");

public TelaEmpresa() {
	this.inicializar();
}

private void inicializar() {
	
	this.txtCodigo.setColumns(25);

	JPanel pnlCodigo = new JPanel(new FlowLayout(FlowLayout.LEFT));
	JPanel pnlTipos = new JPanel(new FlowLayout(FlowLayout.LEFT));

	pnlCodigo.add(new JLabel("Código:"));
	pnlCodigo.add(this.txtCodigo);

	ButtonGroup grupoTipos = new ButtonGroup();

	grupoTipos.add(this.optCNPJ);
	grupoTipos.add(this.optCPF);

	pnlTipos.add(this.optCNPJ);
	pnlTipos.add(this.optCPF);
	
	// ---------------------------->> INICIO DA PARTE QUE INTERESSA <<--------------------------
	
	this.optCNPJ.setSelected(true);
	
	this.txtCodigo.setFormatterFactory(this.obterFormatacao(MSK_CNPJ));

	this.optCNPJ.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			TelaEmpresa.this.aplicarFormatacao(e);
		}

	});
	
	this.optCPF.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			TelaEmpresa.this.aplicarFormatacao(e);
		}

	});
	
	// ---------------------------->> FIM DA PARTE QUE INTERESSA <<--------------------------		

	super.getContentPane().add(pnlCodigo, BorderLayout.CENTER);
	super.getContentPane().add(pnlTipos, BorderLayout.SOUTH);

	super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	super.setSize(400, 90);

	super.setLocationRelativeTo(null);
}

// ---------------------------->> INICIO DA PARTE QUE MUITO INTERESSA <<--------------------------

private void aplicarFormatacao(ActionEvent event) {
	String  pic = null;
	
	if (optCNPJ.isSelected() == true) {
		pic = MSK_CNPJ;
	} else {
		pic = MSK_CPF;
	}
	
	this.txtCodigo.setFormatterFactory(this.obterFormatacao(pic));
	
	this.txtCodigo.setValue(null);
	this.txtCodigo.requestFocus();
}

private DefaultFormatterFactory obterFormatacao(String pic) {
	DefaultFormatterFactory factory = null;
	
	try {
		MaskFormatter formatter = new MaskFormatter(pic);
		factory = new DefaultFormatterFactory(formatter);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	
	return factory;
}

// ---------------------------->> FIM DA PARTE QUE MUITO INTERESSA <<--------------------------

public static void main(String[] args) {
	TelaEmpresa empresaView = new TelaEmpresa();
	
	empresaView.setVisible(true);
}

}[/code]

Executei na minha máquina e funcionou, eu juro rsrsrsrsr.

flws

Gente eu acho que o caminho da praia é por aqui ó:

[code]public class TelaEmpresa extends JFrame {
private static final String MSK_CNPJ = “##.###.###/####-##”;
private static final String MSK_CPF = “###.###.###.##”;

private JFormattedTextField txtCodigo = new JFormattedTextField();

private JRadioButton optCNPJ = new JRadioButton("Cnpj");
private JRadioButton optCPF = new JRadioButton("Cpf");

public TelaEmpresa() {
	this.inicializar();
}

private void inicializar() {
	
	this.txtCodigo.setColumns(25);

	JPanel pnlCodigo = new JPanel(new FlowLayout(FlowLayout.LEFT));
	JPanel pnlTipos = new JPanel(new FlowLayout(FlowLayout.LEFT));

	pnlCodigo.add(new JLabel("Código:"));
	pnlCodigo.add(this.txtCodigo);

	ButtonGroup grupoTipos = new ButtonGroup();

	grupoTipos.add(this.optCNPJ);
	grupoTipos.add(this.optCPF);

	pnlTipos.add(this.optCNPJ);
	pnlTipos.add(this.optCPF);
	
	// ---------------------------->> INICIO DA PARTE QUE INTERESSA <<--------------------------
	
	this.optCNPJ.setSelected(true);
	
	this.txtCodigo.setFormatterFactory(this.obterFormatacao(MSK_CNPJ));

	this.optCNPJ.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			TelaEmpresa.this.aplicarFormatacao(e);
		}

	});
	
	this.optCPF.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			TelaEmpresa.this.aplicarFormatacao(e);
		}

	});
	
	// ---------------------------->> FIM DA PARTE QUE INTERESSA <<--------------------------		

	super.getContentPane().add(pnlCodigo, BorderLayout.CENTER);
	super.getContentPane().add(pnlTipos, BorderLayout.SOUTH);

	super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	super.setSize(400, 90);

	super.setLocationRelativeTo(null);
}

// ---------------------------->> INICIO DA PARTE QUE MUITO INTERESSA <<--------------------------

private void aplicarFormatacao(ActionEvent event) {
	String  pic = null;
	
	if (optCNPJ.isSelected() == true) {
		pic = MSK_CNPJ;
	} else {
		pic = MSK_CPF;
	}
	
	this.txtCodigo.setFormatterFactory(this.obterFormatacao(pic));
	
	this.txtCodigo.setValue(null);
	this.txtCodigo.requestFocus();
}

private DefaultFormatterFactory obterFormatacao(String pic) {
	DefaultFormatterFactory factory = null;
	
	try {
		MaskFormatter formatter = new MaskFormatter(pic);
		factory = new DefaultFormatterFactory(formatter);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	
	return factory;
}

// ---------------------------->> FIM DA PARTE QUE MUITO INTERESSA <<--------------------------

public static void main(String[] args) {
	TelaEmpresa empresaView = new TelaEmpresa();
	
	empresaView.setVisible(true);
}

}[/code]

Executei na minha máquina e funcionou, eu juro rsrsrsrsr.

flws

Vou testar mas desde já muito obrigado. Muito engraçada sua postagem…

Funcionou perfeitamente. Muitissímo Obrigado.