/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.* ; 

2. import java.awt.event.* ; 

3. import java.awt.* ; 

4.  

5. public class TxCompsSw 

6.     extends JApplet 

7.     implements ActionListener 

8.

9.   JTextArea ta ; 

10.   JTextField tf ; 

11.   JButton btadd, btclear ; 

12.   JScrollPane sp ; 

13.   Container cp ; 

14.  

15.   public void actionPerformed( ActionEvent ev ) 

16.   { 

17.     String strtx ; 

18.     Object src = ev.getSource(); 

19.  

20.     if ( src == btadd  ||  src == tf ) 

21.     { 

22.       strtx = tf.getText() ; 

23.       ta.append ( strtx ); 

24.       ta.append ( "\n" ); 

25.     } 

26.     else 

27.     { 

28.       ta.setText ( "" ) ; 

29.     } 

30.     tf.requestFocus() ; 

31.   } 

32.  

33.   public TxCompsSw () 

34.   { 

35.     ta = new JTextArea 

36.      ( "This is a JTextArea" , 6, 25 ) ; 

37.     sp = new JScrollPane( ta ); 

38.     tf = new JTextField ( "Oh, well" , 15 ) ; 

39.  

40.     btadd = new JButton ( "Add to TextArea" ) ; 

41.     btclear = new JButton ( "Clear TextArea" ) ; 

42.   } 

43.  

44.   public void init() 

45.   { 

46.     btadd.addActionListener ( this ) ; 

47.     btclear.addActionListener ( this ) ; 

48.     tf.addActionListener ( this ) ; 

49.  

50.     ta.setEditable ( false ) ; 

51.  

52.     cp = getContentPane(); 

53.     cp.setLayout( new FlowLayout() ); 

54.     cp.add ( tf ) ; 

55.     cp.add ( btadd ) ; 

56.     cp.add ( btclear ) ; 

57.     cp.add ( sp ) ; 

58.  

59.     cp.setBackground ( Color.pink ) ; 

60.     tf.requestFocus (); 

61.   } 

62.

63. /*======== 1.4+ : ============= 

64. import javax.swing.text.MaskFormatter

65. ... 

66. MaskFormatter mf = new MaskFormatter( "#-?-A-*-U" ); 

67. tf = new JFormattedTextField ( mf ) ; 

68.  

69. 7-x-f-$-H 

70. 4-s-7-a-B 

71.  

72. ====== default font ======== 

73. UIManager.put("TextArea.font"

74.    new Font("Monospaced",Font.BOLD,14) ); 

75. */