Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2008
    Messaggi
    49

    Jtextarea...passsargli i dati di una classe !

    Ciao, stavo giocando con java e swing e volevo passare i valori letti da un file excel ad una jtextarea cliccando su un bottone, ma non va...non mi scrive nulla. Se invece li faccio stampare sulla console allora funziona. Il codice è questo:
    che sbaglio ?
    Alex

    codice:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            BirdGui leggi = new BirdGui();
            leggi.getValori();
            
            
        }
        
       public void getValori(){
            Cell nome, cognome, nascita, anni, peso;
            String nomeC = "";
            String cognomeC = "";
            String nascitaC = null;
            int anniC = 0;
            Double pesoC = 0.0;//in kg
            LabelCell lc;
            LabelCell dc;
            NumberCell nc;
             
            try {
                //Apro il file di excel da leggere
                Workbook workbook = Workbook.getWorkbook(new File("/home/alex/Scrivania/utenti.xls"));
                //Seleziono il foglio sul quale voglio operare (il primo foglio ha indice 0)
                Sheet sheet = workbook.getSheet(0);
                 
                //Leggo tutte le righe
                int riga = 1;//indice riga, parto da 1 per saltare l'intestazione dei campi
                int numeroRighe = sheet.getRows();//calcolo quante righe ci sono nel foglio
                 
                //Attento che c'è una riga in più per l'intestazione!
                for(int i = 1; i < numeroRighe; i++){
                   // System.out.println(i);
                    nome = sheet.getCell(0, riga);
                    lc = (LabelCell)nome;
                    nomeC = lc.getString();
                     
                    cognome = sheet.getCell(1, riga);
                    lc = (LabelCell)cognome;
                    cognomeC = lc.getString();
                     
                    nascita = sheet.getCell(2, riga);
                    dc = (LabelCell)nascita;
                    nascitaC = dc.getString();
                     
                    anni = sheet.getCell(3, riga);
                    nc = (NumberCell)anni;
                    anniC = (int)nc.getValue();
                     
                    peso = sheet.getCell(4, riga);
                    nc = (NumberCell)peso;
                    pesoC = nc.getValue();
                    String valore = ("Riga: " + riga + " nome: " + nomeC + " Cognome: " + cognomeC + " Nascita: " + nascitaC + " età: " + anniC + " Peso: " + pesoC);
                    jTextArea1.append(valore);
                    //passo alla riga successiva
                    riga++;
                }
                
                 
                //Chiudo excel e libero la memoria
                workbook.close();
               
                 
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    Difficile aiutarti visto che non compare in quel codice l'aggiunta della jtext area al pannello e poi al frame.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jul 2008
    Messaggi
    49
    Beh non lo avevo postato perchè il codice del Jframe, il Jpanel e la jTextArea erano stati aggiunti da netbeans in automatico mentre creavo nella scheda "design"...cmq il codice completo è questo qui sotto...ho creato un altro metodo (setAggiungi) in modo da poter utilizzare jTextArea con il metodo getValori.....nel debug sembra funzionare..ma poi non mi scrive nulla all'interno della text

    codice:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package bird;
    
    import java.io.File;
    import java.io.IOException;
    import jxl.Cell;
    import jxl.LabelCell;
    import jxl.NumberCell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    
    /**
     *
     * @author alex
     */
    public class BirdGui extends javax.swing.JFrame {
        /**
         * Creates new form BirdGui
         */
       
        
        public void setAggiungi (String str){
               String  dato = str;
                jTextArea1.append(dato);
        }
        public BirdGui() {
            initComponents();
        
        }
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("Leggi");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(20, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap(24, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(32, 32, 32)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(25, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            BirdGui leggi = new ThunderBirdGui();
            leggi.getValori();
          //  System.out.println(dato);
          //  jTextArea1.append(dato);
            
        }                                        
        
       public void getValori(){
            Cell nome, cognome, nascita, anni, peso;
            String nomeC = "";
            String cognomeC = "";
            String nascitaC = null;
            int anniC = 0;
            Double pesoC = 0.0;//in kg
            LabelCell lc;
            LabelCell dc;
            NumberCell nc;
             
            try {
                //Apro il file di excel da leggere
                Workbook workbook = Workbook.getWorkbook(new File("/home/alex/Scrivania/utenti.xls"));
                //Seleziono il foglio sul quale voglio operare (il primo foglio ha indice 0)
                Sheet sheet = workbook.getSheet(0);
                 
                //Leggo tutte le righe
                int riga = 1;//indice riga, parto da 1 per saltare l'intestazione dei campi
                int numeroRighe = sheet.getRows();//calcolo quante righe ci sono nel foglio
                 
                //Attento che c'è una riga in più per l'intestazione!
                for(int i = 1; i < numeroRighe; i++){
                    //System.out.println(i);
                    nome = sheet.getCell(0, riga);
                    lc = (LabelCell)nome;
                    nomeC = lc.getString();
                     
                    cognome = sheet.getCell(1, riga);
                    lc = (LabelCell)cognome;
                    cognomeC = lc.getString();
                     
                    nascita = sheet.getCell(2, riga);
                    dc = (LabelCell)nascita;
                    nascitaC = dc.getString();
                     
                    anni = sheet.getCell(3, riga);
                    nc = (NumberCell)anni;
                    anniC = (int)nc.getValue();
                     
                    peso = sheet.getCell(4, riga);
                    nc = (NumberCell)peso;
                    pesoC = nc.getValue();
                    String valore = ("Riga: " + riga + " nome: " + nomeC + " Cognome: " + cognomeC + " Nascita: " + nascitaC + " età: " + anniC + " Peso: " + pesoC);
                    setAggiungi(valore);
                    //passo alla riga successiva
                    riga++;
                }
                
                 
                //Chiudo excel e libero la memoria
                workbook.close();
               
                 
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * http://download.oracle.com/javase/tu...feel/plaf.html
             */
            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(BirdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(BirdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(BirdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(BirdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
            /*
                     * Create and display the form
             */
            BirdGui leggi = new BirdGui();
            java.awt.EventQueue.invokeLater(new Runnable() {
                
                @Override
                public void run() {
                    new BirdGui().setVisible(true);
                    
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        // End of variables declaration                   
    
    }
       // TODO Auto-generated method stub

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    butta via tutto e comincia da qui.
    The Swing Tutorial

    Tu non stai imparando java. Lo stai uccidendo.
    EDIT:
    comincerei ancora più a monte nello studio... c'è qualcosa di macchinoso nella lettura riga per riga (a che ti serve una variabile in più che hai già per effettuare lo scorrimento delle righe?)
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    Utente di HTML.it
    Registrato dal
    Jul 2008
    Messaggi
    49


    volevo solo provare...pensavo fosse semplice passargli i dati !

  6. #6
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    Il problema è che non si può imparare a sviluppare senza sapere cosa si deve utilizzare e cosa si sta facendo. ^^

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.