java - Passing values to object variables -
i using netbeans in order create jframe asks first name, last name, id number, gender, , level of education. new oop , specially java, please, bear me , excuse poor choice of words.
i first created public class
package registros; public class estudiantes { string nombre; string apellido; string sexo; string ci; string nived; }
then created jframe, idea when hit button "insertar"(jbutton3 (in jbutton3actionperformed)) should obtain values of different jtextareas, radiobuttons , checkboxes, set values of different object variables , place object in vector. need button, don't know how "fill in 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 { (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 , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new newjframe().setvisible(true); } }); }
i have omitted code of grid
in action listener jbutton
, use getters , setters retrieve , store information in swing components necessary. example, jtextfield
named textfieldname
, can text saying string name = textfieldname.gettext();
or can set text inside text field saying textfieldname.settext("this custom string.");
.
you can check out methods available swing components in api: jcombobox, jtextfield, etc.
also, presume because edited code, you're missing closing bracket on jbutton3actionperformed
method.
to save information model class, estudiantes
, should use getters , setters similarly. 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 getters , setters can set , retrieve strings values stored in swing components. example:
estudiantes information = new estudiantes(); information.setnombre("12345"); system.out.println(information.getnombre()); output: 12345
you should familiarize java naming conventions. note have corrected estudiantes
uppercamelcase convention class names.
Comments
Post a Comment