简体   繁体   中英

Passing values to object variables

i am using Netbeans in order to create a jframe that asks for first name, last name, id number, gender, and level of education. I am new to OOP and specially Java, so please, bear with me and excuse my poor choice of words.

i first created a public class

package registros;
   public class estudiantes {
     String nombre;
     String apellido;
     String sexo;
     String ci;
     String nived;
    }

Then i created the jframe, the idea is that when i hit the button "Insertar"(jbutton3 (in jButton3ActionPerformed)) it should obtain the values of the different jtextareas, radiobuttons and checkboxes, set the values of the different object variables and finally place the object in a vector. What i need help is with that button, i don't know how to "fill in the object"(???)

package registros;

import java.util.*;

public class NewJFrame extends javax.swing.JFrame {
estudiantes es;
Vector v = new Vector (5,1);
String sexoValue;

public NewJFrame() {
    this.es = new estudiantes();
    initComponents();
 private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jPanel1 = new javax.swing.JPanel();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    jCheckBox1 = new javax.swing.JCheckBox();
    jCheckBox2 = new javax.swing.JCheckBox();
    jCheckBox3 = new javax.swing.JCheckBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Registro Estudiantes");

    jLabel1.setText("Nombre");

    jLabel2.setText("Apellido");

    jLabel3.setText("C.I.");

    jLabel5.setText("Nivel de Instruccion");

    jButton1.setText("Actualizar");

    jButton2.setText("Buscar");

    jButton3.setText("Insertar");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    jButton4.setText("Eliminar");

    jButton5.setText("Salir");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });

    buttonGroup1.add(jRadioButton1);
    jRadioButton1.setText("Masculino");

    buttonGroup1.add(jRadioButton2);
    jRadioButton2.setText("Femenino");
jRadioButton1.getAccessibleContext().setAccessibleName("btnMasc");
    jRadioButton2.getAccessibleContext().setAccessibleName("btnFem");

    jTextField1.setName(""); // NOI18N

    jLabel4.setText("Sexo");

    jCheckBox1.setText("Primaria");

    jCheckBox2.setText("Secundaria");

    jCheckBox3.setText("Universidad");
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.exit(0);     
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt){                                         
        String nombreValue = jTextField1.getText();
        String apellidoValue = jTextField2.getText();
        String ciValue = jTextField3.getText();
        if (jRadioButton1.isSelected()){
            sexoValue ="Masculino";
        }
else
            sexoValue = "Femenino"; 

public static void main(String args[]) {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

I have omitted the code of the grid

In the action listener for your JButton , simply use the getters and setters to retrieve and store information in the Swing components as necessary. For example, for a JTextField named textFieldName , you can get the text by saying String name = textFieldName.getText(); or you can set the text inside the text field by saying textFieldName.setText("This is my custom string."); .

You can check out the methods available for the swing components in the API: JComboBox , JTextField , etc.

Also, I presume it is because you edited your code, but you're missing a closing bracket on your jButton3ActionPerformed method.

To finally save the information back to your model class, estudiantes , you should use getters and setters similarly. For example:

public class Estudiantes {
    private String nombre;
    private String apellido;
    private String sexo;
    private String ci;
    private String nived;
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public String getApellido() {
        return apellido;
    }
    public void setApellido(String apellido) {
        this.apellido = apellido;
    }
    public String getSexo() {
        return sexo;
    }
    public void setSexo(String sexo) {
        this.sexo = sexo;
    }
    public String getCi() {
        return ci;
    }
    public void setCi(String ci) {
        this.ci = ci;
    }
    public String getNived() {
        return nived;
    }
    public void setNived(String nived) {
        this.nived = nived;
    }
}

With the getters and setters you can easily set and retrieve the strings to the values stored in your Swing components. For example:

Estudiantes information = new Estudiantes();
information.setNombre("12345");
System.out.println(information.getNombre());

Output: 12345

You should also familiarize yourself with Java naming conventions . Note that I have corrected your estudiantes to UpperCamelCase as is the convention for class names.

What you need is to create a getter and setter method in your estudiantes class so you can set the values of that class fields when you instantiate it inside jButton3ActionPerformed .

example:

create a getter and setter

 public class estudiantes {
     String nombre;
     String apellido;
     String sexo;
     String ci;
     String nived;

     //set method
     public void setnombre(String nombre)
     {
       this.nombre = nombre;
     }

     //get method
     public String setnombre()
     {
       return this.nombre;
     }

     //do everything
    }

Inside the jButton3ActionPerformed you can then set values to everything after you instantiate the class

//above is your code
estudiantes es = new estudiantes();
es.setnombre(nombreValue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM