0

Here is my code, I want it so you type something in to the textfield and then you press the button next to it which puts the text in the textArea.

It compiles but when you click the JButton it errors out, I'm a beginner to Java anyone know what I did wrong here?

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



public class TextField extends JPanel implements ActionListener  {
	protected JTextField tf;
	protected JTextArea textArea;
	private final static String newline = "\n";


	public TextField() {

		JFrame f = new JFrame("Text Field");

		JButton button = new JButton("Click!");
		button.addActionListener(this);

		JTextArea textArea = new JTextArea(5, 20);
		textArea.setEditable(false);

		JScrollPane scrollPane = new JScrollPane(textArea);

		JTextField tf = new JTextField();

		JLabel label1 = new JLabel("Text", JLabel.CENTER);

		f.setLayout(new BorderLayout());

		f.add(scrollPane, BorderLayout.EAST);
		f.add(tf, BorderLayout.CENTER);
		f.add(label1, BorderLayout.NORTH);
		f.add(button, BorderLayout.WEST);
		f.add(textArea, BorderLayout.SOUTH);


		f.setVisible(true);
		f.pack();
	}


	private static void createAndShowGUI() {
	}




	public void actionPerformed(ActionEvent e) {

		String text = tf.getText();

		if (e.getActionCommand().equals("Click!")) {

			textArea.append(text + newline);

		}
	}

	public static void main(String[] args) {

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			TextField m = new TextField();

			public void run()
			{
				m.createAndShowGUI();

			}
		});
	}
}
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by Java_90
0

Local varible tf (line 25) hides a field tf (line 8)
Same with textArea declaration.
Errors are for programmer to read carefully.

Edited by quuba: n/a

0

Local varible tf (line 25) hides a field tf (line 8)
Same with textArea declaration.
Errors are for programmer to read carefully.

Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.