user5330423
user5330423

Reputation:

Adding an action listener to JButton

I'm a bit at a novice in Java, and I ran into an error trying to add an active listener to the button in my GUI. It's a fairly simple GUI, if not a bit messy. Right now, there's only a label, button, and text area. What I'm trying to do evantually, is on a button click have it take the input from the text area, and change the label to respond to it. It's a very simple idea...but for a newcomer it has been very vexing. I'm mainly self taught, and whenever I try adding an active listener I get extensive issues.

I assume all of my attempts at adding an active listener are too failed to be of use, so I took out my attempts. This code is simply the GUI. Nothing really happens besides it initiating the code. My question is: How would I add an active listener and a response code?

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class IgnisTest extends JFrame {
   JPanel pnlButton = new JPanel();     
   JButton say = new JButton("Say");
   JLabel output = new JLabel("This is a test");    
   JTextField input = new JTextField();


   public IgnisTest() {       
       pnlButton.setBackground(Color.black);
       say.setPreferredSize( new Dimension( 100, 25) );
       input.setPreferredSize( new Dimension(100, 25) );
       pnlButton.add(say);
       pnlButton.add(input);

       JPanel pnlWrapper = new JPanel(new GridBagLayout()); 
       pnlWrapper.setBackground(Color.black);
       GridBagConstraints constraints = new GridBagConstraints(); 
       pnlWrapper.add(pnlButton, constraints); 

       JPanel pnlLeft = new JPanel();
       pnlLeft.setBackground(Color.black); 
       add(pnlWrapper, BorderLayout.SOUTH); 
       add(pnlLeft, BorderLayout.CENTER); 
       pnlLeft.add(output);

       output.setForeground(Color.white);
       setSize(400, 600);
       setTitle("Ignis");
       setLocationRelativeTo(null);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
  }

  public static void main(String[] args) {
       new IgnisTest();
  }    
}

Upvotes: 1

Views: 3849

Answers (2)

shan1024
shan1024

Reputation: 1399

Easiest way to do this is using an anonymous class like this.

say.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
       output.setText(input.getText());
   }
});

You can find more details about Anonymous Classes at https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Upvotes: 1

Shriram
Shriram

Reputation: 4411

Add the actionListener using anonumousinnerclass or implementing ActionListener

say.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(input.getText());
               //do your logic 
            }
        });

Upvotes: 0

Related Questions