Oppure

Loading
05/08/09 5:53
killer
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...


package test;

import java.awt.Cursor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import javax.swing.JOptionPane;

public class Rubrica extends javax.swing.JFrame {

    public int pos;//Variabile utilizzata per restutuire la posizione di un oggetto all'interno del vettore
    public Object val;//JSpinner
    public Vector vOrig = new Vector();
    public Contatti cont;
    public String nazionalita, sesso, interessi;
    public Object def;

    /** Creates new form Libreria */
    public Rubrica() {
        initComponents();
        this.leggi();
        this.abilitaDisabilitaComponents(0, false);
        pos = -1;
        nazionalita = "";
        sesso = "";
        interessi = "";
        this.val = 0;
        this.jLabelPosInv.setText("" + this.val);
        this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("")));
    }

    private void setComponntsZero() {
        this.jTextFieldNome.setText("");
        this.jTextFieldCognome.setText("");
        this.jTextFieldCittà.setText("");
        this.jTextFieldProv.setText("");
        this.jTextFieldIndirizzo.setText("");
        this.jTextFieldNumero.setText("");
        this.jTextFieldTel.setText("");
        this.jTextFieldCell.setText("");
        this.jTextFieldEMail.setText("");
    }

    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) {
        int contr = -1;
        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(""))) {
            contr = 1;
        } else {
            contr = -1;
        }
        return contr;
    }

    private int verificaPosizione(int valJSpinner) {
        int flag = 0;
        int index = 0;

        while ((index < this.vOrig.size() && (flag == 0))) {

            this.cont = (Contatti) this.vOrig.get(index);
            if (valJSpinner==this.cont.posizione)) {
                pos = index;
                flag = 1;

            }
            index++;
        }
        if (flag == 0) {
            pos = -1;

        }
        return pos;
    }

    private void get() {
        this.cont = new Contatti();

        this.cont.posizione = this.val;
        this.cont.nome = this.jTextFieldNome.getText();
        this.cont.cognome = this.jTextFieldCognome.getText();
        this.cont.citta = this.jTextFieldCittà.getText();
        this.cont.prov = this.jTextFieldProv.getText();
        this.cont.ind = this.jTextFieldIndirizzo.getText();
        this.cont.num = Integer.parseInt(this.jTextFieldNumero.getText());
        this.cont.tel = this.jTextFieldTel.getText();
        this.cont.cell = this.jTextFieldCell.getText();
        this.cont.eMail = this.jTextFieldEMail.getText();
        this.cont.nazionalita = this.nazionalita;
        this.cont.sesso = this.sesso;
        this.cont.interessi = this.interessi;
    }

    private void leggi() {
        try {
            FileInputStream file1 = new FileInputStream("Contatti_In_Rubrica.cnt");
            ObjectInputStream f1_contatti = new ObjectInputStream(file1);


            this.vOrig = (Vector) f1_contatti.readObject();
            file1.close();
        } catch (IOException e) {
        } catch (Exception e) {
        }
    }

    private int scrivi() {
        int scritto = -1;
        try {
            FileOutputStream file2 = new FileOutputStream("Contatti_In_Rubrica.cnt");
            ObjectOutputStream f2 = new ObjectOutputStream(file2);

            this.vOrig.addElement(this.cont);
            f2.writeObject(this.vOrig);
            scritto = 1;
            file2.close();
        } catch (Exception e) {
            scritto = -1;
        }
        return scritto;
    }

                            
    private void jButtonMemorizzaActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        String nm = this.jTextFieldNome.getText();
        String cgn = this.jTextFieldCognome.getText();
        String cit = this.jTextFieldCittà.getText();
        String prov = this.jTextFieldProv.getText();
        String ind = this.jTextFieldIndirizzo.getText();
        String num = this.jTextFieldIndirizzo.getText();
        String tel = this.jTextFieldTel.getText();
        String cell = this.jTextFieldCell.getText();
        String eMail = this.jTextFieldEMail.getText();
        String naz = this.nazionalita;
        String sx = this.sesso;
        String inter = this.interessi;
        int controllo = -1;
        controllo = this.controllo(nm, cgn, cit, prov, ind, num, tel, cell, eMail, naz, sx, inter);
        if (controllo == 1) {
            JOptionPane.showMessageDialog(rootPane, "Attenzione riempire tutti i campi..!!", "Msg From Rubrica System", JOptionPane.WARNING_MESSAGE);
        } else {
            this.get();
            int written = this.scrivi();
            if (written == 1) {
                JOptionPane.showMessageDialog(rootPane, "Contatto salvato", "Msg From Rubrica System", JOptionPane.INFORMATION_MESSAGE);
                this.abilitaDisabilitaComponents(0, false);
                this.jSpinner.setEnabled(true);
                this.jButtonVer.setEnabled(true);
                this.setComponntsZero();
            } else {
                JOptionPane.showMessageDialog(rootPane, "Contatto non salvato !!", "Msg From Rubrica System", JOptionPane.WARNING_MESSAGE);
            }
        }
    }                                                
    private void jMenuItemCercaActionPerformed(java.awt.event.ActionEvent evt) {                                               
        Thread t = new Thread(new ChiudiHome(this, "Cerca"));
        t.start();
    }                                              
    private void jMenuItemElencoActionPerformed(java.awt.event.ActionEvent evt) {                                                
        Thread t = new Thread(new ChiudiHome(this, "Elenco"));
        t.start();
    }                                               
    private void jMenuItemModificaActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        Thread t = new Thread(new ChiudiHome(this, "Modifica"));
        t.start();
    }                                                 
    private void jMenuItemEliminaActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        Thread t = new Thread(new ChiudiHome(this, "Elimina"));
        t.start();
    }                                                
    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {                                              
        System.exit(0);
    }                                             
    private void jMenuItemInfoSoftwareActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        InfoSoftware i = new InfoSoftware();
        i.setVisible(true);
    }                                                     
    private void jButtonCancelMouseMoved(java.awt.event.MouseEvent evt) {                                         
        this.setCursor(Cursor.HAND_CURSOR);
    }                                        
    private void jButtonMemorizzaMouseMoved(java.awt.event.MouseEvent evt) {                                            
        this.setCursor(Cursor.HAND_CURSOR);
    }                                           
    private void formMouseMoved(java.awt.event.MouseEvent evt) {                                
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }                               
    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             
    private void jMenuFileMouseMoved(java.awt.event.MouseEvent evt) {                                     
        this.setCursor(Cursor.HAND_CURSOR);
    }                                    
    private void jMenuItemExitMouseMoved(java.awt.event.MouseEvent evt) {                                         
        this.setCursor(Cursor.HAND_CURSOR);
    }                                        
    private void jMenuStrumentiMouseMoved(java.awt.event.MouseEvent evt) {                                          
        this.setCursor(Cursor.HAND_CURSOR);
    }                                         
    private void jMenuGestioneMouseMoved(java.awt.event.MouseEvent evt) {                                         
        this.setCursor(Cursor.HAND_CURSOR);
    }                                        
    private void jMenuItemCercaMouseMoved(java.awt.event.MouseEvent evt) {                                          
        this.setCursor(Cursor.HAND_CURSOR);
    }                                         
    private void jMenuItemElencoMouseMoved(java.awt.event.MouseEvent evt) {                                           
        this.setCursor(Cursor.HAND_CURSOR);
    }                                          
    private void jMenuItemModificaMouseMoved(java.awt.event.MouseEvent evt) {                                             
        this.setCursor(Cursor.HAND_CURSOR);
    }                                            
    private void jMenuItemEliminaMouseMoved(java.awt.event.MouseEvent evt) {                                            
        this.setCursor(Cursor.HAND_CURSOR);
    }                                           
    private void jMenuInfMouseMoved(java.awt.event.MouseEvent evt) {                                    
        this.setCursor(Cursor.HAND_CURSOR);
    }                                   
    private void jMenuItemInfoSoftwareMouseMoved(java.awt.event.MouseEvent evt) {                                                 
        this.setCursor(Cursor.HAND_CURSOR);
    }                                                
    private void jCheckBoxEuropeaMouseMoved(java.awt.event.MouseEvent evt) {                                            
        this.setCursor(Cursor.HAND_CURSOR);
    }                                           
    private void jCheckBoxEsteraMouseMoved(java.awt.event.MouseEvent evt) {                                           
        this.setCursor(Cursor.HAND_CURSOR);
    }                                          
    private void jCheckBoxMaschileMouseMoved(java.awt.event.MouseEvent evt) {                                             
        this.setCursor(Cursor.HAND_CURSOR);
    }                                            
    private void jCheckBoxFemminileMouseMoved(java.awt.event.MouseEvent evt) {                                              
        this.setCursor(Cursor.HAND_CURSOR);
    }                                             
    private void jCheckBoxStudenteMouseMoved(java.awt.event.MouseEvent evt) {                                             
        this.setCursor(Cursor.HAND_CURSOR);
    }                                            
    private void jCheckBoxLavoratoreMouseMoved(java.awt.event.MouseEvent evt) {                                               
        this.setCursor(Cursor.HAND_CURSOR);
    }                                              
    private void jPanelAggiungiContattoMouseMoved(java.awt.event.MouseEvent evt) {                                                  
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }                                                 
    private void jSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                      

        this.val = this.jSpinner.getValue();
        this.jLabelPosInv.setText("" + this.val);
    }                                     
    private void jCheckBoxEuropeaActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        nazionalita = "Europea";
    }                                                
    private void jCheckBoxEsteraActionPerformed(java.awt.event.ActionEvent evt) {                                                
        nazionalita = "Estera";
    }                                               
    private void jCheckBoxMaschileActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        sesso = "Maschile";
    }                                                 
    private void jCheckBoxFemminileActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        sesso = "Femminile";
    }                                                  
    private void jCheckBoxStudenteActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        interessi = "Studente";
    }                                                 
    private void jCheckBoxLavoratoreActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        interessi = "Lavoratore";
    }                                                   
    private void jComboBoxDefault1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        this.def = jComboBoxDefault1.getSelectedItem();
        if (this.def.equals("Nessuno")) {
            this.jTextFieldTel.setText("X");
            this.jTextFieldTel.setEnabled(false);
        } else {
            if (this.def.equals("")) {
                this.jTextFieldTel.setText("");
                this.jTextFieldTel.setEnabled(true);
            }
        }
    }                                                 
    private void jComboBoxDefault2ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        this.def = jComboBoxDefault2.getSelectedItem();
        if (this.def.equals("Nessuno")) {
            this.jTextFieldCell.setText("X");
            this.jTextFieldCell.setEnabled(false);
        } else {
            if (this.def.equals("")) {
                this.jTextFieldCell.setText("");
                this.jTextFieldCell.setEnabled(true);
            }
        }
    }                                                 
    private void jComboBoxDefault3ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        this.def = jComboBoxDefault3.getSelectedItem();
        if (this.def.equals("Nessuno")) {
            this.jTextFieldEMail.setText("X");
            this.jTextFieldEMail.setEnabled(false);
        } else {
            if (this.def.equals("")) {
                this.jTextFieldEMail.setText("");
                this.jTextFieldEMail.setEnabled(true);
            }
        }
    }                                                 

    private void jButtonVerActionPerformed(java.awt.event.ActionEvent evt) {                                           
        int x = Integer.parseInt(this.jLabelPosInv.getText());
        if (x < 0) {
            this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no.gif")));
            JOptionPane.showMessageDialog(rootPane, "Attenzione...non è possibile " + "\n" + "memorizzare una posizione negativa !!", "Message from rubrica system", JOptionPane.WARNING_MESSAGE);
        } else {
            if (x >= 0) {
                int esiste = -1;
                
                
                
                esiste = this.verificaPosizione(x);//Questo metodo non dovrebbe    funzionare correttamente
                if (esiste == -1) {
                    this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("/si.gif")));
                    JOptionPane.showMessageDialog(rootPane, "Posizione disponibile", "Message from rubrica system", JOptionPane.INFORMATION_MESSAGE);
                    this.jSpinner.setEnabled(false);
                    this.abilitaDisabilitaComponents(0, true);
                    this.jButtonVer.setEnabled(false);
                } else {

                    this.jLabelVerifica.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no.gif")));
                    JOptionPane.showMessageDialog(rootPane, "Posizione non  disponibile", "Message from rubrica system", JOptionPane.WARNING_MESSAGE);
                    this.abilitaDisabilitaComponents(0, false);
                }
            }
        }
    }                                          
    private void abilitaDisabilitaComponents(int num, boolean aFlag) {
        if (num == 0) {
            this.jTextFieldNome.setEnabled(aFlag);
            this.jTextFieldCognome.setEnabled(aFlag);
            this.jTextFieldCittà.setEnabled(aFlag);
            this.jTextFieldProv.setEnabled(aFlag);
            this.jTextFieldIndirizzo.setEnabled(aFlag);
            this.jTextFieldNumero.setEnabled(aFlag);
            this.jTextFieldTel.setEnabled(aFlag);
            this.jTextFieldCell.setEnabled(aFlag);
            this.jTextFieldEMail.setEnabled(aFlag);
            this.jButtonCancel.setEnabled(aFlag);
            this.jButtonMemorizza.setEnabled(aFlag);
            this.jComboBoxDefault1.setEnabled(aFlag);
            this.jComboBoxDefault2.setEnabled(aFlag);
            this.jComboBoxDefault3.setEnabled(aFlag);
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Rubrica().setVisible(true);
            }
        });
    }
}


package test;

import java.io.Serializable;


public class Contatti implements Serializable {

    Object posizione;
    String nome;
    String cognome;
    String citta;
    String prov;
    String ind;
    int num;
    String tel;
    String cell;
    String eMail;
    String nazionalita;
    String sesso;
    String interessi;

    public Contatti(){
         posizione=0;
         nome="";
         cognome="";
         citta="";
         prov="";
         ind="";
         num=0;
         tel="";
         cell="";
         eMail="";
         nazionalita="";
         sesso="";
         interessi="";
    }
}


edit by netarrow: ho aggiunto il tag code
Ultima modifica effettuata da netarrow 05/08/09 20:14
aaa