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();
}
});
}
}