Boa noite,
Estou com dois problemas. O primeiro é que eu tenho um JTextfield chamado txtLogin e quando digito algum texto nele e clico no botão salvar para gravar em um .xml dá erro:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "3jjj"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:514)....e por ai vai
Tenho 3 JTextfield configurados da mesma forma, mas apenas o txtLogin não está aceitando eu digitar texto nele, se por exemplo digito um número ele não dá erro.
O outro problema é que quando grava o arquivo xml não está gravando o valor certo da pontuacaoProfessor que vem do txtPontuacaoProfessor e além disso nem grava a informação da data do cadastro que vem do txtDataCadastro.
Fica assim:
Poderiam me ajudar por favor? Agradeço desde já. Segue o código:
import com.thoughtworks.xstream.XStream;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.Container;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import com.thoughtworks.xstream.XStream;
public class FormularioCadastro extends JFrame
{
Cadastros cadastro;
private GridLayout gridLayout;
private Container container;
private JTextField txtCodigoUsuario;
private JTextField txtLogin;
private JPasswordField passSenha;
private JTextField txtMechaPointsTotal;
private JTextField txtMedalhas;
private JTextField txtTokens;
private JTextField txtPontuacaoProfessor;
private JTextField txtDataCadastro;
private JLabel lblCodigoUsuario;
private JLabel lblLogin;
private JLabel lblSenha;
private JLabel lblMechaPointsTotal;
private JLabel lblMedalhas;
private JLabel lblTokens;
private JLabel lblPontuacaoProfessor;
private JLabel lblDataCadastro;
private JButton btnSalvar;
public FormularioCadastro()
{
super( "Formulário de cadastro" );
gridLayout = new GridLayout(9,2,10,10);
container = getContentPane();
setLayout( gridLayout );
lblCodigoUsuario = new JLabel("Código do Cliente: ");
add( lblCodigoUsuario );
txtCodigoUsuario = new JTextField( 7 );
add( txtCodigoUsuario );
lblLogin = new JLabel("Login: ");
add( lblLogin );
txtLogin = new JTextField( 20 );
add( txtLogin );
lblSenha = new JLabel("Senha: ");
add( lblSenha );
passSenha = new JPasswordField( 13 );
add( passSenha );
lblMechaPointsTotal = new JLabel("Total de MechaPoints: ");
add( lblMechaPointsTotal );
txtMechaPointsTotal = new JTextField( 6 );
add( txtMechaPointsTotal );
lblMedalhas = new JLabel("Medalhas: ");
add( lblMedalhas );
txtMedalhas = new JTextField( 6 );
add( txtMedalhas );
lblTokens = new JLabel("Tokens: ");
add( lblTokens );
txtTokens = new JTextField( 6 );
add( txtTokens );
lblPontuacaoProfessor = new JLabel("Pontuação como professor: ");
add( lblPontuacaoProfessor );
txtPontuacaoProfessor = new JTextField( 7 );
add( txtPontuacaoProfessor );
lblDataCadastro = new JLabel("Data do Cadastro: ");
add( lblDataCadastro );
txtDataCadastro = new JTextField( 8 );
add( txtDataCadastro );
btnSalvar = new JButton("Salvar");
add( btnSalvar );
btnSalvar.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
cadastro = new Cadastros();
cadastro.setCodigoUsuario(Integer.parseInt(txtCodigoUsuario.getText()));
cadastro.setLogin(txtLogin.getText());
cadastro.setSenha(passSenha.getText());
cadastro.setMechaPointsTotal(Integer.parseInt(txtMechaPointsTotal.getText()));
cadastro.setMedalhas(Integer.parseInt(txtMedalhas.getText()));
cadastro.setTokens(Integer.parseInt(txtLogin.getText()));
cadastro.setPontuacaoProfessor(Integer.parseInt(txtPontuacaoProfessor.getText()));
cadastro.setDataCadastro(txtDataCadastro.getText());
XStream xStream = new XStream();
xStream.alias("Cadastros", Cadastros.class);
File arquivo = new File("cadastros.xml");
FileOutputStream gravar;
JOptionPane.showMessageDialog( null, "Dados gravados com sucesso!" );
try {
gravar = new FileOutputStream(arquivo);
gravar.write(xStream.toXML(cadastro).getBytes());
gravar.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
);
} // fim do construtor FormularioCadastro
} // fim da classe TextFieldFrame
import javax.swing.JFrame;
public class TelaFormularioCadastro
{
public static void main( String args[] )
{
FormularioCadastro formularioCadastro = new FormularioCadastro();
formularioCadastro.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
formularioCadastro.setSize( 450, 300 ); // configura o tamanho do frame
formularioCadastro.setVisible( true ); // exibe o frame
} // fim de main
} // fim da classe TelaFormularioCadastro