Change the text of a textarea

I have a class that creates a graphical interface that includes a textarea and other elements. To create the GUI I used NetBeans so there are pieces of code that are not editable. My problem is that I can not change the text in the textarea. I've tried using setText() and append() but the textarea does not change. Where is the problem?

  public class Cifra extends javax.swing.JFrame {

     /** Creates new form Cifra */
     public Cifra() {
        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() {

        comboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        chiaveField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        antMexCif = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        antMex = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        logoutButton = new javax.swing.JButton();
        inviaPropostaButton = new javax.swing.JButton();
        indietroButton = new javax.swing.JButton();
        cifra = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cifra");

        comboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Scegli cifratura...", "Cesare", "Pseudocasuale", "Chiave" }));
        comboBox.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
              comboBoxActionPerformed(evt);
           }
        });

        jLabel1.setText("Inserisci una chiave:");

        chiaveField.setColumns(20);

        antMexCif.setEditable(false);
        antMexCif.setColumns(20);
        antMexCif.setRows(5);
        jScrollPane1.setViewportView(antMexCif);

        antMex.setEditable(false);
        antMex.setColumns(20);
        antMex.setRows(5);
        jScrollPane2.setViewportView(antMex);

        jLabel2.setText("Anteprima messaggio originale:");

        jLabel3.setText("Anteprima messaggio cifrato:");

        logoutButton.setText("Logout");
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
              logoutButtonActionPerformed(evt);
           }
        });

        inviaPropostaButton.setText("Invia proposta");

        indietroButton.setText("Indietro");
        indietroButton.setToolTipText("");
        indietroButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
              indietroButtonActionPerformed(evt);
           }
        });

        cifra.setText("Cifra");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(layout.createSequentialGroup()
              .addGap(28, 28, 28)
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                       .addGroup(layout.createSequentialGroup()
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                             .addComponent(jLabel2)
                             .addComponent(logoutButton))
                          .addGap(168, 168, 168))
                       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                          .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                       .addGroup(layout.createSequentialGroup()
                          .addComponent(indietroButton)
                          .addGap(40, 40, 40)
                          .addComponent(cifra)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                          .addComponent(inviaPropostaButton))
                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                             .addComponent(jLabel3)
                             .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addGroup(layout.createSequentialGroup()
                             .addComponent(jLabel1)
                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                             .addComponent(chiaveField, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))))))
              .addGap(31, 69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(layout.createSequentialGroup()
              .addContainerGap()
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                 .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addComponent(jLabel1)
                 .addComponent(chiaveField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addGroup(layout.createSequentialGroup()
                    .addGap(9, 9, 9)
                    .addComponent(jLabel2))
                 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel3)))
              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addGroup(layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                       .addComponent(inviaPropostaButton)
                       .addComponent(indietroButton)
                       .addComponent(cifra)))
                 .addGroup(layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(logoutButton)))
              .addContainerGap(16, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(741, 389));
        setLocationRelativeTo(null);
     }// </editor-fold>                                                                  

     /**
      * @param args the command line arguments
      */
     public static void start() {
        /* 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/tutorial/uiswing/lookandfeel/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(Cifra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
           java.util.logging.Logger.getLogger(Cifra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
           java.util.logging.Logger.getLogger(Cifra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
           java.util.logging.Logger.getLogger(Cifra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
          //</editor-fold>
           antMex = new javax.swing.JTextArea(DBManager.messaggio.getTesto() + newline);

           antMex.append("hello world" + newline);
           antMex.setText("hello world");

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
              new Cifra().setVisible(true);
           }
        });
     }

     private final static String newline = "\n";
     // Variables declaration - do not modify                     
     public static javax.swing.JTextArea antMex;
     private javax.swing.JTextArea antMexCif;
     private javax.swing.JTextField chiaveField;
     public static javax.swing.JButton cifra;
     private javax.swing.JComboBox comboBox;
     private javax.swing.JButton indietroButton;
     private javax.swing.JButton inviaPropostaButton;
     private javax.swing.JLabel jLabel1;
     private javax.swing.JLabel jLabel2;
     private javax.swing.JLabel jLabel3;
     private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JScrollPane jScrollPane2;
     private javax.swing.JButton logoutButton;
     // End of variables declaration                   
  }

Thanks!

Answers


The crux of your issue is that you will want to understand the difference between an object and a variable.

You're creating a new JTextArea object and putting it into the antMex variable, and then setting the text of this object:

// ******* here *******
antMex = new javax.swing.JTextArea(DBManager.messaggio.getTesto() + newline);

antMex.append("hello world" + newline);
antMex.setText("hello world");

Understand that this will have absolutely no effect on the original JTextArea object that was created, assigned to antMex, and added to your GUI before you created and assigned the new object.

Solution: don't create a new object but rather continue to call methods on the original reference.

// commenting out the line that creates the new JTextArea
// antMex = new javax.swing.JTextArea(DBManager.messaggio.getTesto() + newline);

antMex.append("hello world" + newline);
antMex.setText("hello world");

Better solution: avoid using GUI code generators until you have a little more Swing experience under your belt. You will learn so much by going through the Swing and general Java tutorials, believe me.


Edit Rod Algonquin makes a good point that my recommendations are being made before your object has been created. Instead consider giving Cifra public methods like:

public void setAntMexText(String text) {
  antMex.setText(text);
}

public void appendAntMexText(String text) {
  antMex.append(text);
}

and calling them like so:

  java.awt.EventQueue.invokeLater(new Runnable() {
     public void run() {
        Cifra cifra = new Cifra();
        cifra.setVisible(true);
        cifra.setAntMexText("Hello World!" + NEW_LINE); // NEW_LINE should be all caps
        cifra.appendAntMexText("Next Line");
     }
  });

       antMex.append("hello world" + newline);
       antMex.setText("hello world");

That code should be in the constructor of your class, after the initComponents() statement.

That is you can't reference the variable until the variable has been created.


Need Your Help

Migrate AbstractLobType to Hibernate 4 and Spring 3.2

spring hibernate jira

I am currently using Hibernate 3 and Spring 3.0. I want to migrate to Hibernate 4 and Spring 3.2. I am using a subclass of AbstractLobType to implement a UserType that maps from an InputStream to the

Countdown Timer which shows on the UI with AsyncTask

android android-asynctask countdown

I'm trying to implement a Countdown Timer which shows on the UI,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.