Search code examples
javaswingjframeactionlistenerjtextarea

How can I append Text to JTextArea with an own ActionListener Class after pressing Buttons


Hey I want a JFrame which has to Buttons "LeftButton" and "RightButton" and an JTextArea. After I press one of the two I want that the JTextArea writes which Buttons has been pressed in a new Line. In order to do this I want to use a MyActionListener Class with Referens to the JTextArea, which implements Actionlistener.

I´ve tried to give the ActionPerformed the JTextArea and realized I have to create Setters on their own. Then I realized that MyActionListener Class requires also an Object like JTextArea which is the same as in the JFrame Class. Then I found out that I have to update the JTextArea in the JFrame class as well and here I am a little bit stuck right now. I tried to put the Setters into the JFrame Class and call them from MyActionListener with no success and I tried to do something like A_18_c.south = south

package Aufgabe_18;

import javax.swing.*;
import java.awt.*;

public class A_18_c extends JFrame {
    private Button LeftButton;
    private Button RightButton;
    private JScrollPane scroll;
    private JTextArea south;
    private MyActionListener MAL;

    public static void main(String[] args) {
        A_18_c l = new A_18_c("Aufgabe18c");
    }


    public A_18_c(String title) {
        super(title);
        setSize(300, 150);
        this.setLocation(300, 300);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        MAL = new MyActionListener(south);

        south = new JTextArea(5, 20);
        south.setEditable(false);
        JScrollPane sroll = new JScrollPane(south);
        this.add(sroll, BorderLayout.SOUTH);

        LeftButton = new Button("Left Button");
        LeftButton.setOpaque(true);
        LeftButton.addActionListener(MAL);
        this.add(LeftButton, BorderLayout.WEST);

        RightButton = new Button("Right Button");
        RightButton.setOpaque(true);
        RightButton.addActionListener(MAL);
        this.add(RightButton, BorderLayout.EAST);

        setVisible(true);
    }
}

MyActionListener:

package Aufgabe_18;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyActionListener implements ActionListener{

    private final JTextArea south;

    public MyActionListener(JTextArea south)
    {
        this.south = south;
    }

    private void setTextLeftButton(JTextArea south){
        south.append("Left Button \n");
    }

    private void setTextRightButton(JTextArea south){
        south.append("Right Button \n");
    }

@Override
        public void actionPerformed(ActionEvent e) {
        String a;
        Object src = e.getSource();
        Button b = null;
        b = (Button) src;
        a = b.getString();
        if (a == "LeftButton")
            setTextRightButton(south);
        if (a == "RightButton")
            setTextRightButton(south);
    }
}

I expect the JTextArea to write which Button has been pressed, but nothing happens after pressing. No Errors popping up.


Solution

  • I tried compiling your code on JDK8, it gave errors and I can see it has few issues.

    First of all this:

    MAL = new MyActionListener(south);
    
    south = new JTextArea(5, 20);
    south.setEditable(false);
    

    You are passing Null as parameter to your Listener. You have to initialize "south" first before passing it in the constructor to MAL. Also Button didn't have any method as getString. It has getLabel or getText for JButton. Also as @vince has said, add space in the in "LeftButton". I have tweaked a little with your code. Below is the working code. For simplicity I have added the custom Listener in the same file and replaced Button with JButton (You are already using swing's JFrame so its better try to use all swing components). You will get the gist with this:

    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.*;
    
    public class Test extends JFrame {
        private JButton LeftButton;
        private JButton RightButton;
        private JScrollPane scroll;
        private JTextArea south;
        private MyActionListener MAL;
    
        public static void main(String[] args) {
            Test l = new Test("Aufgabe18c");
        }
    
        public Test(String title) {
            super(title);
            setSize(300, 150);
            this.setLocation(300, 300);
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
            //initialize south
            south = new JTextArea(5, 20);
            south.setEditable(true);
    
            //pass it to your Listener
            MAL = new MyActionListener(south);
            JScrollPane scroll = new JScrollPane(south);
            this.add(scroll, BorderLayout.SOUTH);
    
            LeftButton = new JButton("Left Button");
            LeftButton.setOpaque(true);
            LeftButton.addActionListener(MAL);
            this.add(LeftButton, BorderLayout.WEST);
    
            RightButton = new JButton("Right Button");
            RightButton.setOpaque(true);
            RightButton.addActionListener(MAL);
            this.add(RightButton, BorderLayout.EAST);
    
            setVisible(true);
        }
    
    
    public class MyActionListener implements ActionListener{
    
        private final JTextArea south;
    
        public MyActionListener(JTextArea south)
        {
            this.south = south;
        }
    
        private void setTextLeftButton(JTextArea south){
            south.append("Left Button \n");
        }
    
        private void setTextRightButton(JTextArea south){
            south.append("Right Button \n");
        }
    
    @Override
            public void actionPerformed(ActionEvent e) {
            String a;
            Object src = e.getSource();
            JButton b = null;
            b = (JButton) src;
            a = b.getText();
            if (a == "Left Button")
                setTextLeftButton(south);
            else
                setTextRightButton(south);
        }
    }
    }