Contoh Program berikut ini
mendemokan penanganan inputan Textarea di java. Inputan textarea ini dapat
dibuat dengan objek JTextArea. Program akan menampilkan dua text-area dimana
sebagian atau seluruh tulisan yang dipilih pada text-area yang pertama akan
dicopy / dipindahkan ke text-area yang kedua.
Berikut ini contoh programnya
01
|
import java.awt.*;
|
|
02
|
|
03
|
import java.awt.event.*;
|
|
04
|
|
05
|
import javax.swing.*;
|
|
06
|
|
07
|
public class TextAreaDemo
extends JFrame {
|
|
08
|
|
09
|
private JTextArea
textArea1, textArea2;
|
|
10
|
|
11
|
private JButton
btnCopy;
|
|
12
|
|
13
|
public TextAreaDemo()
{
|
|
14
|
|
15
|
super ("Menampilkan
Textarea");
|
|
16
|
|
17
|
Box
box = Box.createHorizontalBox();
|
|
18
|
|
19
|
String
string = "Ini hanya contoh aja yach";
|
|
20
|
|
21
|
textArea1
= new JTextArea(string, 10, 15);
|
|
22
|
|
23
|
box.add(new JScrollPane(textArea1));
|
|
24
|
|
25
|
btnCopy
= new JButton("Copy >>");
|
|
26
|
|
27
|
box.add(btnCopy);
|
|
28
|
|
29
|
btnCopy.addActionListener(
|
|
30
|
|
31
|
new ActionListener()
{
|
|
32
|
|
33
|
public void actionPerformed(ActionEvent
e) {
|
|
34
|
|
35
|
textArea2.setText(textArea1.getSelectedText());
|
|
36
|
|
37
|
}
|
|
38
|
|
39
|
}
|
|
40
|
|
41
|
);
//end of addActionListener
|
|
42
|
|
43
|
textArea2
= new JTextArea(10,15);
|
|
44
|
|
45
|
textArea2.setEditable(false);
|
|
46
|
|
47
|
box.add(new JScrollPane(textArea2));
|
|
48
|
|
49
|
Container
container = getContentPane();
|
|
50
|
|
51
|
container.add(box);
|
|
52
|
|
53
|
setSize
(425, 200);
|
|
54
|
|
55
|
setResizable(false);
|
|
56
|
|
57
|
setLocationRelativeTo
(null);
|
|
58
|
|
59
|
setVisible
(true);
|
|
60
|
|
61
|
}
|
|
62
|
|
63
|
public static void main
(String args[]) {
|
|
64
|
|
65
|
JFrame.setDefaultLookAndFeelDecorated(true);
|
|
66
|
|
67
|
TextAreaDemo
test = new TextAreaDemo();
|
|
68
|
|
69
|
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
70
|
|
71
|
}
|
|
72
|
|
73
|
}
|
Thanks for reading & sharing KEPOIN IT
0 comments:
Post a Comment