Search code examples
javauser-interfaceactionlistenersettext

Click "send" settext from text field to textarea


Hope someone could give me a wee bit of advice. I am really new to Java (1-2 weeks) and have been using tutorials to get this far but nothing I try allows me to do a (simple) task.

When I click the button send in my GUI, I want it to set text/add text to the JTextArea on my GUI of which I typed in to the JTextField. I have provided my code below so someone can guide me to where I have went wrong.

package firstjavagui;

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

public class FirstJavaGui extends JPanel implements ActionListener {
    private static ActionListener e;
    protected JTextField tf;
    protected JTextArea ta;
    protected JButton send;
    private final static String newL = "\n";

    public FirstJavaGui() {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public class e implements ActionListener {  
        @Override
        public void actionPerformed(ActionEvent e) { 
            String text = tf.getText();

            if (send.isSelected()) {
                ta.setText(text + newL);
            }    
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Thomas' first application");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(575,300);

        JMenuBar mb = new JMenuBar();

        JMenu m1 = new JMenu("File");
        JMenu m2 = new JMenu("Help");

        mb.add(m1);
        mb.add(m2);

        JMenuItem m21 = new JMenuItem("Save");
        JMenuItem m22 = new JMenuItem("Exit");

        m1.add(m21);
        m1.add(m22);

        JButton send = new JButton("Send");

        JButton reset = new JButton("Start/Restart");

        JLabel enter = new JLabel("Enter text here:");

        JTextField text = new JTextField(25);

        JPanel p = new JPanel();
        p.add(enter);
        p.add(text);
        p.add(send);
        p.add(reset);

        JTextArea ta = new JTextArea();

        frame.getContentPane().add(BorderLayout.SOUTH,p);
        frame.getContentPane().add(BorderLayout.NORTH, mb);
        frame.getContentPane().add(BorderLayout.CENTER, ta);

        send.addActionListener(e);

        frame.setVisible(true);
    }
}

Be greatly appreciated if someone could assist me slightly.

I think it is because all my buttons etc are within my main so my actions can not recognize the "send" as the button...but if I write my gui outside of the main method it doesn't compile.. I am new to this but I am learning. Just need a small hand.


Solution

  • Welcome to Java. You are getting there with your code but there are a few little problems.

    The first issue surrounds "e". You have two things called e, one is a class and one is a variable referring to an object:

    // Reference to an object
    private static ActionListener e;
    ...
    // Class
    public class e implements ActionListener{
    ...
    

    Now you never give your object reference a value so it will be null. In the case of ActionListeners, objects that respond to events in the UI, this is not a good idea. You need to create an instance of class that implements ActionListener in order to respond to events. Here is a typical example:

    ...
    ExitListener listener = new ExitListener();
    button.addActionListener(listener)
    ...
    private class ExitListener implements ActionListener {
        @Override
        public void actionPerformed (ActionEvent e) {
            System.exit(0);
        }
    }
    

    ExitListener is an ActionListener. In order to respond to an event you need to make an instance of ExitListener and pass that instance to the button. By the way, "e" is a very bad name for a class - always make your class names descriptive.

    The second issue is a classic, everyone has this problem when they start with Java. The problem surrounds the use of static. I won't go into great detail as it is a big subject but basically from a method that is declared static (such as main(...)) you can only access variables declared in the class that are also marked as static. For a static variable there is only ever one actual variable. For a non-static variable there is one variable for each instance of the class.

    Basically for your test program you can choose to make everything static (including your class currently called "e") or you can create an instance of FirstJavaGui and then you can use the non-statics.