Search code examples
javaswingawtjtextareakeylistener

KeyPressedEvent to SetText for JTextArea


I attempted to make a JTextArea in my Java.Swing application using NetBeans so that when I press the Enter Button. The JTextArea will display "BUTTON PRESSED".

Here is what I have:

private void keyPressedEvent(java.awt.event.KeyEvent evt)
{
    if(evt.getKeyCode() == KeyEvent.VK_ENTER)
    {
         System.out.println("HERE"); //For sanity check.
         txtArea.setText("BUTTON PRESSED");
         System.out.println("HERE AGAIN"); //For sanity check
    }
}

I noticed that the print statements are executing but the txtArea is not setting the text.

I did some research and believe that evt.consume() should be called somewhere, but I am unsure of how that works.

EDIT: I tried putting evt.consume() in the if-block and it still did not set the text.

Adding the key listener like this:

private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            KeyPressedEvent(evt);
        }
    });
    jScrollPane1.setViewportView(jTextArea1);

    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(59, 59, 59)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(175, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(40, 40, 40)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(164, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

Solution

  • Works for me. How are you adding the keylistener?

    test program screenshot

    public class TextAreaTest extends KeyAdapter {
    
      private JFrame frame = new JFrame("Text Area Test");
      private JTextArea area = new JTextArea();
    
      public static void main(String[] args) {
        TextAreaTest test = new TextAreaTest();
        test.go();
      }
    
      private void go() {    
        area.addKeyListener(this);
        frame.getContentPane().add(area);
        frame.setVisible(true);
      }
    
      @Override
      public void keyPressed(KeyEvent evt) {
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
             System.out.println("HERE"); //For sanity check.
             area.setText("BUTTON PRESSED");
             System.out.println("HERE AGAIN"); //For sanity check
        }
      }
    
    }