Tim Tim - 3 months ago 17
Java Question

Java swing getting input from a JTextField

I see something like this asked around a lot, but it hasn't answered my problem. I am fairly new at Java. I am trying to get some input from a

JTextField
and return it as a
String
so I can use it for comparison in a different class. This is what I see as an answer, I'd like to be able to use
str
in any other part of the class.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class ClassFrame extends JFrame {

private static final long serialVersionUID = 2451829341034438685L;

public static JButton inputButton = new JButton("Send");
public static JTextArea editTextArea = new JTextArea("Type Here!");
public static JTextArea uneditTextArea = new JTextArea();

public ClassFrame(String title) {
//SET LAYOUT MANAGER (How it arranges components)
setLayout(new BorderLayout());
//////CREATE SWING COMPONENTS////////////
//OUTPUT TEXT AREA
uneditTextArea.setEditable(false);

//INPUT TEXT AREA
editTextArea.setBackground(Color.BLUE);
editTextArea.setForeground(Color.WHITE);

//SET CONTENT PANE
Container c = getContentPane();

//ADD COMPONENTS TO CONTENT PANE
c.add(uneditTextArea, BorderLayout.CENTER);
c.add(editTextArea, BorderLayout.SOUTH);
c.add(inputButton, BorderLayout.WEST);

ClassFrame.inputButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
String str = editTextArea.getText();
editTextArea.setText(" ");
System.out.println(str);
}
});
}
}

Answer

See my comments.

package applet;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class ClassFrame extends JFrame {


    private static final long serialVersionUID = 2451829341034438685L;

    public static JButton inputButton = new JButton("Send");
    public static JTextArea editTextArea = new JTextArea("Type Here!");
    public static JTextArea uneditTextArea = new JTextArea();

    // Your String, defined here and usable throughout the class

    private String myString;

    public ClassFrame(String title) {

        // Learn to indent your code properly so that it's more readable to both you
        // and others

        //SET LAYOUT MANAGER (How it arranges components)
        setLayout(new BorderLayout());
        //////CREATE SWING COMPONENTS////////////
        //OUTPUT TEXT AREA
        uneditTextArea.setEditable(false);

        //INPUT TEXT AREA
        editTextArea.setBackground(Color.BLUE);
        editTextArea.setForeground(Color.WHITE);

        //SET CONTENT PANE
        Container c = getContentPane();

        //ADD COMPONENTS TO CONTENT PANE        
        c.add(uneditTextArea, BorderLayout.CENTER);
        c.add(editTextArea, BorderLayout.SOUTH);
        c.add(inputButton, BorderLayout.WEST);

        ClassFrame.inputButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                // Using the class field myString to receive text from text area

                myString = editTextArea.getText();

                // Don't do this.  A blank space is bad.

                //editTextArea.setText(" ");

                // Do this instead.  An empty string is a more clear indication of what's going on

                editTextArea.setText("");

                System.out.println(myString);                
            }
        });
    }
}

I would suggest you do some reading on variable scoping in Java. Google it.

Comments