Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - JSpinner
Forum - Java - JSpinner

Avatar
killer (Normal User)
Expert


Messaggi: 217
Iscritto: 19/07/2009

Segnala al moderatore
Postato alle 7:53
Mercoledì, 05/08/2009
ciao a tutti...ho utilizzato lo JSpinner con l'evento associato 'stateChanged' per poter ricavare il valore  ogni volta che  avviene un cambiamento....bene....ho dichiarato una variabile globale nella classe Object val; e successivamente ho utilizzato il metodo   val=JSpinner.getValue(); che mi restituisce un 'oggetto' e non un intero... adesso pero devo controllare che tale valore  non sia presente nel Vector v; ho provato con  la ricerca standard confrontando un oggetto con un altro oggetto e addirittura settando una jLabel.setText(""+val),facendo la get  confrontando un intero con un oggetto che rikiamo dalla classe contatti e non funziona.poi l'oggetto cont di classe Contatti lo memorizzo nel vettore  facendo  v.addElementAt(this.cont);  e il vettore copiandolo su File  nel blocco try catch  facendo : file.writeObject(this.v);  benissimo  nel costruttore ho inserito il metodo di lettura che legge dal file e mi memorizza nel vettore l'oggetto....quindi lavorerò  con il vettore.....solo che quando eseguo una ricerca per trovare il valore che mi restituisce lo spinner non lo fa....alcune volte mi dice posizione disponibile altre volte posizione non disponibile...adesso vi posto il codice non vorrei ci siano degli errori particolari...


Codice sorgente - presumibilmente Java

  1. package test;
  2.  
  3. import java.awt.Cursor;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.util.Vector;
  10. import javax.swing.JOptionPane;
  11.  
  12. public class Rubrica extends javax.swing.JFrame {
  13.  
  14.     public int pos;//Variabile utilizzata per restutuire la posizione di un oggetto all'interno del vettore
  15.     public Object val;//JSpinner
  16.     public Vector vOrig = new Vector();
  17.     public Contatti cont;
  18.     public String nazionalita, sesso, interessi;
  19.     public Object def;
  20.  
  21.     /** Creates new form Libreria */
  22.     public Rubrica() {
  23.         initComponents();
  24.         this.leggi();
  25.         this.abilitaDisabilitaComponents(0, false);
  26.         pos = -1;
  27.         nazionalita = "";
  28.         sesso = "";
  29.         interessi = "";
  30.         this.val = 0;
  31.         this.jLabelPosInv.setText("" + this.val);
  32.         this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("")));
  33.     }
  34.  
  35.     private void setComponntsZero() {
  36.         this.jTextFieldNome.setText("");
  37.         this.jTextFieldCognome.setText("");
  38.         this.jTextFieldCittà.setText("");
  39.         this.jTextFieldProv.setText("");
  40.         this.jTextFieldIndirizzo.setText("");
  41.         this.jTextFieldNumero.setText("");
  42.         this.jTextFieldTel.setText("");
  43.         this.jTextFieldCell.setText("");
  44.         this.jTextFieldEMail.setText("");
  45.     }
  46.  
  47.     private int controllo(String nome, String cognome, String citta, String prov, String ind, String num, String tel, String cell, String Email, String nazion, String sesso, String interessi) {
  48.         int contr = -1;
  49.         if ((nome.equals("")) || (cognome.equals("")) || (citta.equals("")) || (prov.equals("")) || (ind.equals("")) || (num.equals("")) || (tel.equals("")) || (cell.equals("")) || (Email.equals("")) || (nazion.equals("")) || (sesso.equals("")) || (interessi.equals(""))) {
  50.             contr = 1;
  51.         } else {
  52.             contr = -1;
  53.         }
  54.         return contr;
  55.     }
  56.  
  57.     private int verificaPosizione(int valJSpinner) {
  58.         int flag = 0;
  59.         int index = 0;
  60.  
  61.         while ((index < this.vOrig.size() && (flag == 0))) {
  62.  
  63.             this.cont = (Contatti) this.vOrig.get(index);
  64.             if (valJSpinner==this.cont.posizione)) {
  65.                 pos = index;
  66.                 flag = 1;
  67.  
  68.             }
  69.             index++;
  70.         }
  71.         if (flag == 0) {
  72.             pos = -1;
  73.  
  74.         }
  75.         return pos;
  76.     }
  77.  
  78.     private void get() {
  79.         this.cont = new Contatti();
  80.  
  81.         this.cont.posizione = this.val;
  82.         this.cont.nome = this.jTextFieldNome.getText();
  83.         this.cont.cognome = this.jTextFieldCognome.getText();
  84.         this.cont.citta = this.jTextFieldCittà.getText();
  85.         this.cont.prov = this.jTextFieldProv.getText();
  86.         this.cont.ind = this.jTextFieldIndirizzo.getText();
  87.         this.cont.num = Integer.parseInt(this.jTextFieldNumero.getText());
  88.         this.cont.tel = this.jTextFieldTel.getText();
  89.         this.cont.cell = this.jTextFieldCell.getText();
  90.         this.cont.eMail = this.jTextFieldEMail.getText();
  91.         this.cont.nazionalita = this.nazionalita;
  92.         this.cont.sesso = this.sesso;
  93.         this.cont.interessi = this.interessi;
  94.     }
  95.  
  96.     private void leggi() {
  97.         try {
  98.             FileInputStream file1 = new FileInputStream("Contatti_In_Rubrica.cnt");
  99.             ObjectInputStream f1_contatti = new ObjectInputStream(file1);
  100.  
  101.  
  102.             this.vOrig = (Vector) f1_contatti.readObject();
  103.             file1.close();
  104.         } catch (IOException e) {
  105.         } catch (Exception e) {
  106.         }
  107.     }
  108.  
  109.     private int scrivi() {
  110.         int scritto = -1;
  111.         try {
  112.             FileOutputStream file2 = new FileOutputStream("Contatti_In_Rubrica.cnt");
  113.             ObjectOutputStream f2 = new ObjectOutputStream(file2);
  114.  
  115.             this.vOrig.addElement(this.cont);
  116.             f2.writeObject(this.vOrig);
  117.             scritto = 1;
  118.             file2.close();
  119.         } catch (Exception e) {
  120.             scritto = -1;
  121.         }
  122.         return scritto;
  123.     }
  124.  
  125.                            
  126.     private void jButtonMemorizzaActionPerformed(java.awt.event.ActionEvent evt) {                                                
  127.         String nm = this.jTextFieldNome.getText();
  128.         String cgn = this.jTextFieldCognome.getText();
  129.         String cit = this.jTextFieldCittà.getText();
  130.         String prov = this.jTextFieldProv.getText();
  131.         String ind = this.jTextFieldIndirizzo.getText();
  132.         String num = this.jTextFieldIndirizzo.getText();
  133.         String tel = this.jTextFieldTel.getText();
  134.         String cell = this.jTextFieldCell.getText();
  135.         String eMail = this.jTextFieldEMail.getText();
  136.         String naz = this.nazionalita;
  137.         String sx = this.sesso;
  138.         String inter = this.interessi;
  139.         int controllo = -1;
  140.         controllo = this.controllo(nm, cgn, cit, prov, ind, num, tel, cell, eMail, naz, sx, inter);
  141.         if (controllo == 1) {
  142.             JOptionPane.showMessageDialog(rootPane, "Attenzione riempire tutti i campi..!!", "Msg From Rubrica System", JOptionPane.WARNING_MESSAGE);
  143.         } else {
  144.             this.get();
  145.             int written = this.scrivi();
  146.             if (written == 1) {
  147.                 JOptionPane.showMessageDialog(rootPane, "Contatto salvato", "Msg From Rubrica System", JOptionPane.INFORMATION_MESSAGE);
  148.                 this.abilitaDisabilitaComponents(0, false);
  149.                 this.jSpinner.setEnabled(true);
  150.                 this.jButtonVer.setEnabled(true);
  151.                 this.setComponntsZero();
  152.             } else {
  153.                 JOptionPane.showMessageDialog(rootPane, "Contatto non salvato !!", "Msg From Rubrica System", JOptionPane.WARNING_MESSAGE);
  154.             }
  155.         }
  156.     }                                                
  157.     private void jMenuItemCercaActionPerformed(java.awt.event.ActionEvent evt) {                                              
  158.         Thread t = new Thread(new ChiudiHome(this, "Cerca"));
  159.         t.start();
  160.     }                                              
  161.     private void jMenuItemElencoActionPerformed(java.awt.event.ActionEvent evt) {                                                
  162.         Thread t = new Thread(new ChiudiHome(this, "Elenco"));
  163.         t.start();
  164.     }                                              
  165.     private void jMenuItemModificaActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  166.         Thread t = new Thread(new ChiudiHome(this, "Modifica"));
  167.         t.start();
  168.     }                                                
  169.     private void jMenuItemEliminaActionPerformed(java.awt.event.ActionEvent evt) {                                                
  170.         Thread t = new Thread(new ChiudiHome(this, "Elimina"));
  171.         t.start();
  172.     }                                                
  173.     private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {                                              
  174.         System.exit(0);
  175.     }                                            
  176.     private void jMenuItemInfoSoftwareActionPerformed(java.awt.event.ActionEvent evt) {                                                      
  177.         InfoSoftware i = new InfoSoftware();
  178.         i.setVisible(true);
  179.     }                                                    
  180.     private void jButtonCancelMouseMoved(java.awt.event.MouseEvent evt) {                                        
  181.         this.setCursor(Cursor.HAND_CURSOR);
  182.     }                                        
  183.     private void jButtonMemorizzaMouseMoved(java.awt.event.MouseEvent evt) {                                            
  184.         this.setCursor(Cursor.HAND_CURSOR);
  185.     }                                          
  186.     private void formMouseMoved(java.awt.event.MouseEvent evt) {                                
  187.         this.setCursor(Cursor.DEFAULT_CURSOR);
  188.     }                              
  189.     private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {                                              
  190.         // TODO add your handling code here:
  191.     }                                            
  192.     private void jMenuFileMouseMoved(java.awt.event.MouseEvent evt) {                                    
  193.         this.setCursor(Cursor.HAND_CURSOR);
  194.     }                                    
  195.     private void jMenuItemExitMouseMoved(java.awt.event.MouseEvent evt) {                                        
  196.         this.setCursor(Cursor.HAND_CURSOR);
  197.     }                                        
  198.     private void jMenuStrumentiMouseMoved(java.awt.event.MouseEvent evt) {                                          
  199.         this.setCursor(Cursor.HAND_CURSOR);
  200.     }                                        
  201.     private void jMenuGestioneMouseMoved(java.awt.event.MouseEvent evt) {                                        
  202.         this.setCursor(Cursor.HAND_CURSOR);
  203.     }                                        
  204.     private void jMenuItemCercaMouseMoved(java.awt.event.MouseEvent evt) {                                          
  205.         this.setCursor(Cursor.HAND_CURSOR);
  206.     }                                        
  207.     private void jMenuItemElencoMouseMoved(java.awt.event.MouseEvent evt) {                                          
  208.         this.setCursor(Cursor.HAND_CURSOR);
  209.     }                                          
  210.     private void jMenuItemModificaMouseMoved(java.awt.event.MouseEvent evt) {                                            
  211.         this.setCursor(Cursor.HAND_CURSOR);
  212.     }                                            
  213.     private void jMenuItemEliminaMouseMoved(java.awt.event.MouseEvent evt) {                                            
  214.         this.setCursor(Cursor.HAND_CURSOR);
  215.     }                                          
  216.     private void jMenuInfMouseMoved(java.awt.event.MouseEvent evt) {                                    
  217.         this.setCursor(Cursor.HAND_CURSOR);
  218.     }                                  
  219.     private void jMenuItemInfoSoftwareMouseMoved(java.awt.event.MouseEvent evt) {                                                
  220.         this.setCursor(Cursor.HAND_CURSOR);
  221.     }                                                
  222.     private void jCheckBoxEuropeaMouseMoved(java.awt.event.MouseEvent evt) {                                            
  223.         this.setCursor(Cursor.HAND_CURSOR);
  224.     }                                          
  225.     private void jCheckBoxEsteraMouseMoved(java.awt.event.MouseEvent evt) {                                          
  226.         this.setCursor(Cursor.HAND_CURSOR);
  227.     }                                          
  228.     private void jCheckBoxMaschileMouseMoved(java.awt.event.MouseEvent evt) {                                            
  229.         this.setCursor(Cursor.HAND_CURSOR);
  230.     }                                            
  231.     private void jCheckBoxFemminileMouseMoved(java.awt.event.MouseEvent evt) {                                              
  232.         this.setCursor(Cursor.HAND_CURSOR);
  233.     }                                            
  234.     private void jCheckBoxStudenteMouseMoved(java.awt.event.MouseEvent evt) {                                            
  235.         this.setCursor(Cursor.HAND_CURSOR);
  236.     }                                            
  237.     private void jCheckBoxLavoratoreMouseMoved(java.awt.event.MouseEvent evt) {                                              
  238.         this.setCursor(Cursor.HAND_CURSOR);
  239.     }                                              
  240.     private void jPanelAggiungiContattoMouseMoved(java.awt.event.MouseEvent evt) {                                                  
  241.         this.setCursor(Cursor.DEFAULT_CURSOR);
  242.     }                                                
  243.     private void jSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                      
  244.  
  245.         this.val = this.jSpinner.getValue();
  246.         this.jLabelPosInv.setText("" + this.val);
  247.     }                                    
  248.     private void jCheckBoxEuropeaActionPerformed(java.awt.event.ActionEvent evt) {                                                
  249.         nazionalita = "Europea";
  250.     }                                                
  251.     private void jCheckBoxEsteraActionPerformed(java.awt.event.ActionEvent evt) {                                                
  252.         nazionalita = "Estera";
  253.     }                                              
  254.     private void jCheckBoxMaschileActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  255.         sesso = "Maschile";
  256.     }                                                
  257.     private void jCheckBoxFemminileActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  258.         sesso = "Femminile";
  259.     }                                                  
  260.     private void jCheckBoxStudenteActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  261.         interessi = "Studente";
  262.     }                                                
  263.     private void jCheckBoxLavoratoreActionPerformed(java.awt.event.ActionEvent evt) {                                                    
  264.         interessi = "Lavoratore";
  265.     }                                                  
  266.     private void jComboBoxDefault1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  267.         this.def = jComboBoxDefault1.getSelectedItem();
  268.         if (this.def.equals("Nessuno")) {
  269.             this.jTextFieldTel.setText("X");
  270.             this.jTextFieldTel.setEnabled(false);
  271.         } else {
  272.             if (this.def.equals("")) {
  273.                 this.jTextFieldTel.setText("");
  274.                 this.jTextFieldTel.setEnabled(true);
  275.             }
  276.         }
  277.     }                                                
  278.     private void jComboBoxDefault2ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  279.         this.def = jComboBoxDefault2.getSelectedItem();
  280.         if (this.def.equals("Nessuno")) {
  281.             this.jTextFieldCell.setText("X");
  282.             this.jTextFieldCell.setEnabled(false);
  283.         } else {
  284.             if (this.def.equals("")) {
  285.                 this.jTextFieldCell.setText("");
  286.                 this.jTextFieldCell.setEnabled(true);
  287.             }
  288.         }
  289.     }                                                
  290.     private void jComboBoxDefault3ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  291.         this.def = jComboBoxDefault3.getSelectedItem();
  292.         if (this.def.equals("Nessuno")) {
  293.             this.jTextFieldEMail.setText("X");
  294.             this.jTextFieldEMail.setEnabled(false);
  295.         } else {
  296.             if (this.def.equals("")) {
  297.                 this.jTextFieldEMail.setText("");
  298.                 this.jTextFieldEMail.setEnabled(true);
  299.             }
  300.         }
  301.     }                                                
  302.  
  303.     private void jButtonVerActionPerformed(java.awt.event.ActionEvent evt) {                                          
  304.         int x = Integer.parseInt(this.jLabelPosInv.getText());
  305.         if (x < 0) {
  306.             this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no.gif")));
  307.             JOptionPane.showMessageDialog(rootPane, "Attenzione...non è possibile " + "\n" + "memorizzare una posizione negativa !!", "Message from rubrica system", JOptionPane.WARNING_MESSAGE);
  308.         } else {
  309.             if (x >= 0) {
  310.                 int esiste = -1;
  311.                
  312.                
  313.                
  314.                 esiste = this.verificaPosizione(x);//Questo metodo non dovrebbe    funzionare correttamente
  315.                 if (esiste == -1) {
  316.                     this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("/si.gif")));
  317.                     JOptionPane.showMessageDialog(rootPane, "Posizione disponibile", "Message from rubrica system", JOptionPane.INFORMATION_MESSAGE);
  318.                     this.jSpinner.setEnabled(false);
  319.                     this.abilitaDisabilitaComponents(0, true);
  320.                     this.jButtonVer.setEnabled(false);
  321.                 } else {
  322.  
  323.                     this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no.gif")));
  324.                     JOptionPane.showMessageDialog(rootPane, "Posizione non  disponibile", "Message from rubrica system", JOptionPane.WARNING_MESSAGE);
  325.                     this.abilitaDisabilitaComponents(0, false);
  326.                 }
  327.             }
  328.         }
  329.     }                                          
  330.     private void abilitaDisabilitaComponents(int num, boolean aFlag) {
  331.         if (num == 0) {
  332.             this.jTextFieldNome.setEnabled(aFlag);
  333.             this.jTextFieldCognome.setEnabled(aFlag);
  334.             this.jTextFieldCittà.setEnabled(aFlag);
  335.             this.jTextFieldProv.setEnabled(aFlag);
  336.             this.jTextFieldIndirizzo.setEnabled(aFlag);
  337.             this.jTextFieldNumero.setEnabled(aFlag);
  338.             this.jTextFieldTel.setEnabled(aFlag);
  339.             this.jTextFieldCell.setEnabled(aFlag);
  340.             this.jTextFieldEMail.setEnabled(aFlag);
  341.             this.jButtonCancel.setEnabled(aFlag);
  342.             this.jButtonMemorizza.setEnabled(aFlag);
  343.             this.jComboBoxDefault1.setEnabled(aFlag);
  344.             this.jComboBoxDefault2.setEnabled(aFlag);
  345.             this.jComboBoxDefault3.setEnabled(aFlag);
  346.         }
  347.     }
  348.  
  349.     public static void main(String args[]) {
  350.         java.awt.EventQueue.invokeLater(new Runnable() {
  351.             public void run() {
  352.                 new Rubrica().setVisible(true);
  353.             }
  354.         });
  355.     }
  356. }
  357.  
  358.  
  359. package test;
  360.  
  361. import java.io.Serializable;
  362.  
  363.  
  364. public class Contatti implements Serializable {
  365.  
  366.     Object posizione;
  367.     String nome;
  368.     String cognome;
  369.     String citta;
  370.     String prov;
  371.     String ind;
  372.     int num;
  373.     String tel;
  374.     String cell;
  375.     String eMail;
  376.     String nazionalita;
  377.     String sesso;
  378.     String interessi;
  379.  
  380.     public Contatti(){
  381.          posizione=0;
  382.          nome="";
  383.          cognome="";
  384.          citta="";
  385.          prov="";
  386.          ind="";
  387.          num=0;
  388.          tel="";
  389.          cell="";
  390.          eMail="";
  391.          nazionalita="";
  392.          sesso="";
  393.          interessi="";
  394.     }
  395. }



edit by netarrow: ho aggiunto il tag code

Ultima modifica effettuata da netarrow il 05/08/2009 alle 22:14
PM Quote