Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [Java] Chiudere JDialog

    Salve a tutti!
    Ho un problema col mio programma.
    Tramite una JDialog inserisco dei dati e grazie ad un bottone li salvo su di un file txt. Io vorrei che questo bottone mi chiudesse anche la mia JDialog ma non riesco a trovare come si fa.
    Chi mi aiuta? Grazie

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: [Java] Chiudere JDialog

    Originariamente inviato da elnelly
    Tramite una JDialog inserisco dei dati e grazie ad un bottone li salvo su di un file txt. Io vorrei che questo bottone mi chiudesse anche la mia JDialog ma non riesco a trovare come si fa.
    Chi mi aiuta? Grazie
    setVisible(false) o dispose(). Quest'ultimo rilascia anche le risorse "native" usate dalla dialog.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    dispose() e setVisible() riesco ad utilizzarli in un'altra dialog ma non nella stessa.
    Mi spiego meglio.
    Io ho questa dialog di nome AggiuntaDipendente, essa contiene delle textfield nelle quali l'utente inserisce nome, cognome ecc... del dipendente. Poi tramite un pulsante OK i dati vengono trascritti su file txt e mi si apre una dialog Dipendenti, che mostra l'elenco dei dipendenti. Il mio problema è che AggiuntaDipendente non scompare e io volevo aggiungere alle funzioni del pulsante OK una funzione che la faccia sparire.
    Come faccio a usare dispose() nella stessa dialog che voglio chiudere?

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da elnelly
    Il mio problema è che AggiuntaDipendente non scompare
    Senza vedere il codice che hai scritto ... sarebbe più facile tirare 3 numeri a caso e vincere un terno secco al lotto.

    Originariamente inviato da elnelly
    Come faccio a usare dispose() nella stessa dialog che voglio chiudere?
    Invocandolo sul reference al JDialog ... che chiaramente devi avere da qualche parte (es. perché la tua classe è-un JDialog o perché hai un campo di istanza JDialog).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Questo è il pulsante in Dipendenti che mi manda ad AggiuntaDipendente
    codice:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            JDialog AggiuntaDipendente = new AggiuntaDipendente(null, false);
            AggiuntaDipendente.setVisible(true);
    }

    Questo è il codice della JDialog AggiuntaDipendente...
    In rosso dove voglio fare in modo che AggiuntaDipendente si chiuda.
    Devo invocare dispose() sul reference al JDialog, ma non ho la più pallida idea di come fare
    codice:
    /*
    
    package myfarm;
    import java.io.*;
    import javax.swing.JDialog;
    
    public class AggiuntaDipendente extends javax.swing.JDialog {
    
        /** Creates new form AggiuntaDipendente */
        public AggiuntaDipendente(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            Nome_TextField = new javax.swing.JTextField();
            Cognome_TextField = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jLabel4 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(myfarm.MyFarmApp.class).getContext().getResourceMap(AggiuntaDipendente.class);
            setBackground(resourceMap.getColor("Form.background")); // NOI18N
            setName("Form"); // NOI18N
    
            jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
            jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
            jLabel1.setName("jLabel1"); // NOI18N
    
            jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
            jLabel2.setName("jLabel2"); // NOI18N
    
            jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
            jLabel3.setName("jLabel3"); // NOI18N
    
            Nome_TextField.setText(resourceMap.getString("Nome_TextField.text")); // NOI18N
            Nome_TextField.setName("Nome_TextField"); // NOI18N
            Nome_TextField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    Nome_TextFieldActionPerformed(evt);
                }
            });
    
            Cognome_TextField.setText(resourceMap.getString("Cognome_TextField.text")); // NOI18N
            Cognome_TextField.setName("Cognome_TextField"); // NOI18N
    
            jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
            jButton1.setName("jButton1"); // NOI18N
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel4.setForeground(resourceMap.getColor("jLabel4.foreground")); // NOI18N
            jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
            jLabel4.setName("jLabel4"); // NOI18N
    
            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(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jLabel2)
                                        .add(jLabel3))
                                    .add(40, 40, 40)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, Cognome_TextField)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, Nome_TextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)))
                                .add(jButton1))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 258, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jLabel1))
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel2)
                        .add(Nome_TextField, 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(Cognome_TextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel3))
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jButton1))
                    .addContainerGap(129, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void Nome_TextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            QUI C'E' IL CODICE CHE AGGIUNGE IL DIPENDENTE AL FILE TXT
            JDialog Dipendenti = new Dipendenti(null, false); 
            Dipendenti.setVisible(true); //apro Dialog Dipendenti
            QUI VOGLIO FARE IN MODO CHE AGGIUNTADIPENDENTE SI CHIUDA!
            
        }                                        
    
        /**
         * @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(AggiuntaDipendente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(AggiuntaDipendente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(AggiuntaDipendente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(AggiuntaDipendente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the dialog */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    AggiuntaDipendente dialog = new AggiuntaDipendente(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    
                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JTextField Cognome_TextField;
        private javax.swing.JTextField Nome_TextField;
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        // End of variables declaration                   
    }

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da elnelly
    codice:
    public class AggiuntaDipendente extends javax.swing.JDialog {
    
        /* ..... */
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // .....
            dispose();                // <--- Esattamente come dicevo
        }                                        
    
        /* ..... */
    }
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  7. #7
    Noo... bastava solo questo!! Ahah funziona!! Grazie Mille andbin!!! Grazie!!!!

  8. #8
    Ops mi sono accorto che adesso ho un altro problema.
    Da Dipendenti apro la dialog AggiuntaDipendente, con il pulsante OK AggiuntaDipendenti si chiude e mi si apre una nuova schermata Dipendenti. Mi ritrovo con due schermate Dipendenti. Come faccio a far chiudere la schermata Dipendenti vecchia?

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.