Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175

    [NB + JAVA] persistenza stato pulsanti

    Non sò se il titolo è quello + adatto, cmq i miei esperimenti con Java continuano. Allora sto scorrendo le righe di questo DB, nell'interfaccia java ho 4 pulsanti di "navigazione": primo, precedente, successivo, ultimo.

    Se clicco sul bottone "ultimo" vado a visualizzare l'ultimo record della tabella di DB, quindi sarebbe necessario che i tasti successivo e ultimo siano inattivi - ultimo.setEnabled(false) e successivo.setEnabled(false) -.

    Il tutto funziona e i tasti riesco a disabilitarli opportunamente quando mi serve, ma basta che ci passi la freccia del mouse o ci arrivi col tasto tab che i tasti tornano cliccabili come prima.
    Di cosa si tratta?

    Questo è parte del codice:
    codice:
    public void setModalita(int modo) {
            modalita=modo;
            switch (modo)
            {
                case AVVIO: 
                    primo.setEnabled(false);
                    precedente.setEnabled(false);
                    ultimo.setEnabled(false);
                    successivo.setEnabled(false);
                    break;
                [...]
            }
        }
    
    [...]
    
    // HANDLER BOTTONE ULTIMO
    private void ultimoActionPerformed(java.awt.event.ActionEvent evt) {                                       
                try {
                    if (!rs.isLast()) {
                        rs.last(); 
                        edit.setText(rs.getString("Descrizione"));
                        setModalita(FINE);  //E' LA VOID DICHIARATA PRIMA CHE DECIDE QUALI TASTI ATTIVARE E QUALI NO
                    } 
                } catch (Exception e) {
                    System.out.println("Errore: "+e.getMessage());
                }
        }

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175
    nessuno che sà rispondermi

  3. #3
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328
    Credo che la parte di codice che hai postato non sia sufficiente a capire dov'è l'errore.
    Sicuramente hai delle istruzioni che "resettano" lo stato dell'applicazione, altrimenti un pulsante reso inattivo non può tornare da solo attivo, passanodci sopra il mouse o arrivandoci con il TAB.


    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

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175
    posto tutto il programma:
    codice:
    package reprise;
    import java.sql.*;
    import javax.swing.JTable;
    
    public class mask extends javax.swing.JFrame {
        
        /** Creates new form mask */
        public mask() {
            initComponents();
        }
        final static public int AVVIO=0;
        final static public int INIZIO=1;
        final static public int MEDIO=2;
        final static public int FINE=3;
        private int modalita;
        
        public void setModalita(int modo) {
            modalita=modo;
            switch (modo)
            {
                case AVVIO: 
                    primo.setEnabled(false);
                    precedente.setEnabled(false);
                    ultimo.setEnabled(false);
                    successivo.setEnabled(false);
                    break;
                case INIZIO: 
                    primo.setEnabled(false);
                    precedente.setEnabled(false);
                    ultimo.setEnabled(true);
                    successivo.setEnabled(true);
                    break;
                case MEDIO:
                    primo.setEnabled(true);
                    precedente.setEnabled(true);
                    ultimo.setEnabled(true);
                    successivo.setEnabled(true);
                    break;
                case FINE:
                    primo.setEnabled(true);
                    precedente.setEnabled(true);
                    ultimo.setEnabled(false);
                    successivo.setEnabled(false);
                    break;
            }
        }
        
        Connection con;
        Statement st;
        String query, conString;
        ResultSet rs;
        
        INIZIO CODICE GENERATO DA NETBEANS
        
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
        private void initComponents() {
            connetti = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jButton2 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            edit = new javax.swing.JTextField();
            primo = new javax.swing.JButton();
            precedente = new javax.swing.JButton();
            successivo = new javax.swing.JButton();
            ultimo = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            connetti.setText("INVIA");
            connetti.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    connettiMouseClicked(evt);
                }
            });
    
            jLabel1.setText("Clicca Invia per eseguire la query!");
    
            jButton2.setText("ESCI");
            jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton2MouseClicked(evt);
                }
            });
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            primo.setText("|<");
            primo.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    primoActionPerformed(evt);
                }
            });
    
            precedente.setText("<");
            precedente.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    precedenteActionPerformed(evt);
                }
            });
    
            successivo.setText(">");
            successivo.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    successivoActionPerformed(evt);
                }
            });
    
            ultimo.setText(">|");
            ultimo.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ultimoActionPerformed(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(connetti, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .add(edit, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .add(layout.createSequentialGroup()
                            .add(primo)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(precedente)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(successivo)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(ultimo)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(17, 17, 17)
                    .add(connetti)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jLabel1)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(edit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(primo)
                        .add(precedente)
                        .add(ultimo)
                        .add(successivo)
                        .add(jButton2))
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            pack();
        }// </editor-fold>
        
        FINE CODICE GENERATO DA NETBEANS
        
                            
    
        private void primoActionPerformed(java.awt.event.ActionEvent evt) {                                      
                try {
                    if (!rs.isFirst()) {
                        rs.first(); 
                        edit.setText(rs.getString("Descrizione"));
                        setModalita(INIZIO);
                        
                    } 
                } catch (Exception e) {
                    System.out.println("Errore: "+e.getMessage());
                }           
        }                                     
    
        private void ultimoActionPerformed(java.awt.event.ActionEvent evt) {                                       
                try {
                    if (!rs.isLast()) {
                        rs.last(); 
                        edit.setText(rs.getString("Descrizione"));
                        setModalita(FINE);
                    } 
                } catch (Exception e) {
                    System.out.println("Errore: "+e.getMessage());
                }
        }                                      
    
        private void precedenteActionPerformed(java.awt.event.ActionEvent evt) {                                           
                try {
                    if (!rs.isFirst()) {
                        rs.previous(); 
                        edit.setText(rs.getString("Descrizione"));
                        setModalita(MEDIO);
                    } else if (rs.isFirst()) {
                       setModalita(INIZIO); 
                    }
                } catch (Exception e) {
                    System.out.println("Errore: "+e.getMessage());
                }
        }                                          
    
        private void successivoActionPerformed(java.awt.event.ActionEvent evt) {                                           
                try {
                    if (!rs.isLast()) {
                        rs.next(); 
                        edit.setText(rs.getString("Descrizione"));
                        setModalita(MEDIO);
                    } else if (rs.isLast()) {
                       setModalita(FINE); 
                    }
                } catch (Exception e) {
                    System.out.println("Errore: "+e.getMessage());
                }           
        }                                          
    
        
        
        private void connettiMouseClicked(java.awt.event.MouseEvent evt) {                                      
            initComponents();
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                conString="jdbc:odbc:localhost";
                con=DriverManager.getConnection(conString, "mariano", "120384");
                st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    	    query="select * from articolo order by codart ASC";
                rs = st.executeQuery(query);
                rs.first();
                edit.setText(rs.getString("Descrizione"));
                setModalita(INIZIO);
            } catch (Exception e) {
                System.out.println("Errore: "+e.getMessage());
            }
        }                                     
    
        
        private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
            System.exit(0);
        }                                     
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new mask().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify                     
        private javax.swing.JButton connetti;
        private javax.swing.JTextField edit;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JButton precedente;
        private javax.swing.JButton primo;
        private javax.swing.JButton successivo;
        private javax.swing.JButton ultimo;
        // 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.