Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2011
    Messaggi
    7

    Problema con list iterator

    Ciao a tutti...ho una form che visualizza l'elenco degli utenti di un sito...ho inserito due pulsanti per spostarmi avanti e indietro tramite un iterator. Il problema è che se per esempio premo avanti poi per tornare all'utente precedente devo premere il pulsante indietro 2 volte...Vi posto il codice per capire meglio:

    codice:
    package org.apache.esme.form;
    
    import java.util.List;
    import java.util.ListIterator;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import org.apache.esme.api.RestMetodi;
    import org.apache.esme.model.AllUsers;
    
    /**
     *
     * @author gabriele
     */
    public class FrmUsers extends javax.swing.JFrame {
        
        JFrame frmIndex;
        RestMetodi pRestMetodi;
        ListIterator it;
        List users;
        
        private void acquisisci() {
            try {
                users = pRestMetodi.getUsers();
                } catch (Exception ex) {
                    Logger.getLogger(FrmUsers.class.getName()).log(Level.SEVERE, null, ex);
                }
                it = users.listIterator();
                AllUsers us = (AllUsers)it.next();
                pnlUsers1.showUsers(us);
           
        }
    
        /** Creates new form FrmUsers */
        public FrmUsers() {
            initComponents();
            frmIndex = new JFrame();
            pRestMetodi = new RestMetodi();
        }
        
        public FrmUsers(JFrame p_frmIndex) {
            initComponents();
            pRestMetodi = new RestMetodi();
            acquisisci();
            frmIndex = p_frmIndex;
        }
    
        /** 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() {
    
            BtnIndietro = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            pnlUsers1 = new org.apache.esme.form.PnlUsers();
            BtnNext = new javax.swing.JButton();
            BtnPrev = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            BtnIndietro.setText("Indietro");
            BtnIndietro.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    BtnIndietroActionPerformed(evt);
                }
            });
    
            jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24));
            jLabel1.setText("ELENCO UTENTI ESME");
    
            BtnNext.setText(">");
            BtnNext.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    BtnNextActionPerformed(evt);
                }
            });
    
            BtnPrev.setText("<");
            BtnPrev.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    BtnPrevActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(123, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(115, 115, 115))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(75, Short.MAX_VALUE)
                    .addComponent(pnlUsers1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(61, 61, 61))
                .addGroup(layout.createSequentialGroup()
                    .addGap(48, 48, 48)
                    .addComponent(BtnPrev, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addGap(304, 304, 304)
                    .addComponent(BtnNext, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addGap(46, 46, 46))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(204, Short.MAX_VALUE)
                    .addComponent(BtnIndietro, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(199, 199, 199))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addComponent(pnlUsers1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnPrev)
                        .addComponent(BtnNext))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                    .addComponent(BtnIndietro)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void BtnIndietroActionPerformed(java.awt.event.ActionEvent evt) {                                            
            this.setVisible(false);
            frmIndex.setVisible(true);
            frmIndex.setEnabled(true);   
        }                                           
    
        private void BtnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
             if (it.hasNext()) {
               AllUsers us = (AllUsers)it.next();
               pnlUsers1.showUsers(us);    
            }
            else {
               JOptionPane.showMessageDialog(null, "Non esiste un elemento successivo", "Elemento non esistente", JOptionPane.ERROR_MESSAGE);
            }
        }                                       
    
        private void BtnPrevActionPerformed(java.awt.event.ActionEvent evt) {                                        
            if (it.hasPrevious()) {
               AllUsers us = (AllUsers)it.previous();
               pnlUsers1.showUsers(us);    
            }
            else {
               JOptionPane.showMessageDialog(null, "Non esiste un elemento precedente", "Elemento non esistente", JOptionPane.ERROR_MESSAGE);
            }
        }                                       
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
           
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new FrmUsers().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton BtnIndietro;
        private javax.swing.JButton BtnNext;
        private javax.swing.JButton BtnPrev;
        private javax.swing.JLabel jLabel1;
        private org.apache.esme.form.PnlUsers pnlUsers1;
        // End of variables declaration

  2. #2
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328
    Dalla documentazione del metodo next() di ListIterator:

    Returns the next element in the list. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)
    Questo perchè il metodo next() ritorna il prossimo oggetto nella lista, e fa avanzare il cursore... ergo, se dopo un next() chiamo una previous(), ottengo lo stesso oggetto.

    In qualche modo dovrai tenerne conto, magari con l'uso di una variabile booleana che indica qual è stata l'ultima azione:

    Se l'ultima azione che hai fatto è un "next", per ottenere il precedente dovrai fare 2 previous.
    Se l'ultima azione che hai fatto è un "previous" per ottenere il successivo dovrai fare 2 next.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2011
    Messaggi
    7
    Mi era sfuggito...sinceramente credevo di aver fatto un errore...
    Come faccio a memorizzare l'ultima azione che ho fatto?

    Non sono molto pratico di Java


    Grazie

  4. #4
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328
    Originariamente inviato da Ravo86
    Mi era sfuggito...sinceramente credevo di aver fatto un errore...
    Come faccio a memorizzare l'ultima azione che ho fatto?

    Non sono molto pratico di Java


    Grazie
    L'ho detto... basta usare una variabile. Puoi usare un'intero, ma è sufficiente una variabile booleana.
    Inizialmente è impostata a "true" (è come se avessi fatto una next, ma è il caso base iniziale, non ha alcun effetto). Se effettui una "next" (e, all'inizio, puoi solo fare una next), imposti la variabile a "true". Se effettui una previous, imposti la variabile a "false".

    Prima di fare la next() o la previous(), controlli la variabile... e agisci di conseguenza.

    Se e solo se l'ultima azione che hai fatto è un "next" (ovvero, la variabile è "true"), per ottenere il precedente dovrai fare 2 previous, altrimenti ne fai uno.
    Se e solo se l'ultima azione che hai fatto è un "previous" (ovvero, la variabile è "false") per ottenere il successivo dovrai fare 2 next, altrimenti ne fai uno.

    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  5. #5
    Utente di HTML.it
    Registrato dal
    Sep 2011
    Messaggi
    7
    Grazie mille Lele sono riuscito, lascio il codice se a qualcuno potesse servire:

    codice:
    package org.apache.esme.form;
    
    import java.util.List;
    import java.util.ListIterator;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import org.apache.esme.api.RestMetodi;
    import org.apache.esme.model.AllUsers;
    
    /**
     *
     * @author gabriele
     */
    public class FrmUsers extends javax.swing.JFrame {
        
        JFrame frmIndex;
        RestMetodi pRestMetodi;
        ListIterator it;
        List users;
        boolean lastAction = true;
        
        
        private void acquisisci() {
            try {
                users = pRestMetodi.getUsers();
                } catch (Exception ex) {
                    Logger.getLogger(FrmUsers.class.getName()).log(Level.SEVERE, null, ex);
                }
                it = users.listIterator();
                AllUsers us = (AllUsers)it.next();
                pnlUsers1.showUsers(us);
           
        }
    
        /** Creates new form FrmUsers */
        public FrmUsers() {
            initComponents();
            frmIndex = new JFrame();
            pRestMetodi = new RestMetodi();
            acquisisci();
        }
        
        public FrmUsers(JFrame p_frmIndex) {
            initComponents();
            pRestMetodi = new RestMetodi();
            acquisisci();
            frmIndex = p_frmIndex;
        }
    
        /** 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() {
    
            BtnIndietro = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            pnlUsers1 = new org.apache.esme.form.PnlUsers();
            BtnNext = new javax.swing.JButton();
            BtnPrev = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            BtnIndietro.setText("Indietro");
            BtnIndietro.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    BtnIndietroActionPerformed(evt);
                }
            });
    
            jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24));
            jLabel1.setText("ELENCO UTENTI ESME");
    
            BtnNext.setText(">");
            BtnNext.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    BtnNextActionPerformed(evt);
                }
            });
    
            BtnPrev.setText("<");
            BtnPrev.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    BtnPrevActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(123, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(115, 115, 115))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(75, Short.MAX_VALUE)
                    .addComponent(pnlUsers1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(61, 61, 61))
                .addGroup(layout.createSequentialGroup()
                    .addGap(48, 48, 48)
                    .addComponent(BtnPrev, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addGap(304, 304, 304)
                    .addComponent(BtnNext, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addGap(46, 46, 46))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(204, Short.MAX_VALUE)
                    .addComponent(BtnIndietro, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(199, 199, 199))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addComponent(pnlUsers1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnPrev)
                        .addComponent(BtnNext))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                    .addComponent(BtnIndietro)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void BtnIndietroActionPerformed(java.awt.event.ActionEvent evt) {                                            
            this.setVisible(false);
            frmIndex.setVisible(true);
            frmIndex.setEnabled(true);   
        }                                           
    
        private void BtnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
            if (lastAction == false) { 
               if (it.hasNext()) {
                  AllUsers us = (AllUsers)it.next();
                  us = (AllUsers)it.next();
                  lastAction = true;
                  pnlUsers1.showUsers(us);    
               }
               else {
                  JOptionPane.showMessageDialog(null, "Non esiste un elemento successivo", "Elemento non esistente", JOptionPane.ERROR_MESSAGE);
               }
            }
            else {
                if (it.hasNext()) {
                  AllUsers us = (AllUsers)it.next();
                  lastAction = true;
                  pnlUsers1.showUsers(us);    
               }
               else {
                  JOptionPane.showMessageDialog(null, "Non esiste un elemento successivo", "Elemento non esistente", JOptionPane.ERROR_MESSAGE);
               }
            }
        }                                       
    
        private void BtnPrevActionPerformed(java.awt.event.ActionEvent evt) {                                        
            if (lastAction == true) {
               if (it.hasPrevious()) {
                   AllUsers us = (AllUsers)it.previous();
                   us = (AllUsers)it.previous();
                   lastAction = false;
                   pnlUsers1.showUsers(us);    
                  }
               else {
                  JOptionPane.showMessageDialog(null, "Non esiste un elemento precedente", "Elemento non esistente", JOptionPane.ERROR_MESSAGE);
               }
            }
            else {
                if (it.hasPrevious()) {
                   AllUsers us = (AllUsers)it.previous();
                   lastAction = false;
                   pnlUsers1.showUsers(us);    
                  }
               else {
                  JOptionPane.showMessageDialog(null, "Non esiste un elemento precedente", "Elemento non esistente", JOptionPane.ERROR_MESSAGE);
               }
            }
        }                                       
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
           
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new FrmUsers().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton BtnIndietro;
        private javax.swing.JButton BtnNext;
        private javax.swing.JButton BtnPrev;
        private javax.swing.JLabel jLabel1;
        private org.apache.esme.form.PnlUsers pnlUsers1;
        // End of variables declaration

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.