Salve,
ho questa semplicissima applicazione:
consiste in un JPanel che contiene una jTextArea (contenuta a sua volta in un JScrollPane) e due jButton con etichetta "Chiudi" e "Vai", il primo chiude l'applicazione mentre su "Vai" ho definito un evento actionPerformed, per cui alla pressione di esso eseguo queste semplici istruzioni:
mentre scriviFile(); è un metodo che provvede a scrivere 1000 volte (ovviamente è per una prova) un file.codice:int index = 0; jTextArea1.setText(""); while (index<1000) { index++; jTextArea1.append("Passo "+index+"\n"); scriviFile(); }
Il problema è che il contenuto di jTextArea1 NON viene aggiornato ad ogni iterazione del ciclo, ma dopo che è stato scritto 1000 volte il file, ossia dopo l'ultima iterazione :master:
Posto per maggior chiarezza l'intero codice
Grazie![]()
codice:package tools; import java.io.*; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.String; public class ProvaLog extends javax.swing.JFrame { public ProvaLog() { initComponents(); } // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton2 = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Prova Finestra di Log"); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Prova Log", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jTextArea1.setColumns(30); jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 13)); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jButton2.setText("Vai"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 38, Short.MAX_VALUE) .add(jButton2) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(jButton2) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jButton1.setText("Chiudi"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(jButton1)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(jButton1) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed vai(); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed System.exit(0); }//GEN-LAST:event_jButton1ActionPerformed public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ProvaLog().setVisible(true); } }); } private void vai() { int index = 0; jTextArea1.setText(""); while (index<1000) { index++; jTextArea1.append("Passo "+index+"\n"); scriviFile(); } } private void scriviFile() { File descrizioni; String testo; int err; String filename_input = ""; filename_input = "C://prova_log___.txt"; descrizioni = new File(filename_input); if (!descrizioni.exists()) { try { descrizioni.createNewFile(); } catch (IOException e) { System.out.println("Unable to create "+filename_input+": "+e.getMessage()); } } testo = "Questo è un file di testo utilizzato per una prova."; try { scriviFile(descrizioni, testo); } catch (FileNotFoundException ex2) { System.out.println(ex2.getMessage()); } catch (IOException ex2) { System.out.println(ex2.getMessage()); } } void scriviFile(File unFile, String testo) throws FileNotFoundException, IOException { if (unFile == null) { throw new IllegalArgumentException("File should not be null."); } if (!unFile.exists()) { throw new FileNotFoundException("File does not exist: " + unFile); } if (!unFile.isFile()) { throw new IllegalArgumentException("Should not be a directory: " + unFile); } if (!unFile.canWrite()) { throw new IllegalArgumentException("File cannot be written: " + unFile); } Writer output = null; try { output = new BufferedWriter(new FileWriter(unFile)); output.write(testo); } finally { if (output != null) { output.close(); } } System.out.println("File creato..."); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration//GEN-END:variables }

Rispondi quotando
