Hi :)

I am Writing a program that takes text from a JTextArea and uses the string to do things upon clicking a button. I used an ActionListener for the button. My problem is that I recieve an empty string when I try to access the text from the JTextArea from another method. I'm sure its a simple solution, but my googleing has gotten me nowhere :(

public class Braille{
private static JTextArea textarea;
public Braille(){
//some code
JTextArea textarea = new JTextArea(36, 40);
JButton button = new JButton("button");
panel.add(textarea);
panel.add(button);
textarea.setText("Test String");
button.addActionListener(this);
//some code
}

public void actionPerformed(ActionEvent event) {
		pnt();	
	}

public static void pnt(){
textarea.getText(); // this is null :(
//some code
}}

Is there a way to use the text from a JTextArea in a different method (I think that's what its called)?

Thx :)

You have declared two text areas with the same name.

You have declared two text areas with the same name.

Oh yes, i see. That slipped in while I was cutting and pasting snippets :P I fixed it, that must be the reason why it was null. But if I remove the first declaration, I get a compile error :( . I did however find a workaround by using a CaretListener.

thx for the help :)

you should have kept the declaration that is NOT in the method.

you should have kept the declaration that is NOT in the method.

Hah! That did it :D Thank you very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.