How can I show the content entered in a JTextfield in a JOptionPane (In a form)?

0

This is the code of my form, the part of the JOptionPane is where I have the problem. // LDVives = Place where you live

private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {                                          
    txtNombre.getText();
    txtEdad.getText();
    txtLDVives.getText();
}                                         

private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    JOptionPane.showMessageDialog(null, "Tus datos son\nNombre: "+txtNombre+"\nEdad: "+txtEdad+"\nLugar donde Vives: "+txtLDVives);
}
    
asked by Nicolás Ramos Díaz 18.12.2018 в 23:39
source

1 answer

0

The problem is in this section:

private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    JOptionPane.showMessageDialog(null, "Tus datos son\nNombre: "+txtNombre+"\nEdad: "+txtEdad+"\nLugar donde Vives: "+txtLDVives);
}

What you are trying to print are the variables, for example txtNombre that I imagine is JTextField and you should print your representation in chain, instead of the chain representation of its content.

The graphic components already implement the Java Bean specification and therefore have methods called accesors , which specifically for your case are Setter and Getter.

setName sets the value, while getName reads the value of a variable called name, and that convention works so widely used.

For this reason it replaces the calls to the fields by a call to its text property, that is: txtNombre.getText() to get the text within the JTextField called txtNombre and that should be called internally text .

Your code would look like:

JOptionPane.showMessageDialog(null, "Tus datos son\nNombre: "+txtNombre.getText()+"\nEdad: "+txtEdad.getText()+"\nLugar donde Vives: "+txtLDVives.getText());
    
answered by 19.12.2018 в 00:54