import javax.swing.*;
import java.awt.*;
import java.awt.event.*; // step 1
public class JTextAreaDemo extends JFrame //step 2
implements ActionListener{
private JTextArea ta1 = new JTextArea(10,20);
private JButton btn = new JButton("Add");
private JButton btn2 = new JButton("Append");
private JButton btn3 = new JButton("Get text in TextArea");
public JTextAreaDemo() {
getContentPane().setLayout(new FlowLayout());
getContentPane().add(ta1);
getContentPane().add(btn);
getContentPane().add(btn2);
getContentPane().add(btn3);
btn.addActionListener(this);//step 4:
btn2.addActionListener(this);
btn3.addActionListener(this);
} //end constructor
public void actionPerformed(ActionEvent teckyong){//step 3
if (teckyong.getSource() == btn){
ta1.setText("ABC"); // Add text, overwrite the previous
} //end if
else if (teckyong.getSource() == btn2){
ta1.append("ABC\n");
}
else if (teckyong.getSource() == btn3){
String s = ta1.getText(); // Get what is in the TextArea
JOptionPane.showMessageDialog(null,s);
}
} //end actionPerformed
public static void main(String[] args) {
JTextAreaDemo f = new JTextAreaDemo();
f.setTitle("OOPG");
f.setSize(300, 300);
f.setVisible(true);
} //end main
} //end class
Showing posts with label GUI. Show all posts
Showing posts with label GUI. Show all posts
Wednesday, November 29, 2006
JTextArea : setText, getText and append methods
Subscribe to:
Posts (Atom)