OnJava8-Examples/ui/TextArea.java

35 lines
1011 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// ui/TextArea.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// Using the JTextArea control.
import javax.swing.*;
import java.awt.*;
import java.util.*;
import onjava.*;
import static onjava.SwingConsole.*;
2015-06-15 17:47:35 -07:00
public class TextArea extends JFrame {
private JButton
b = new JButton("Add Data"),
c = new JButton("Clear Data");
private JTextArea t = new JTextArea(20, 40);
private Map<String,String> m = new HashMap<>();
public TextArea() {
// Use up all the data:
m.putAll(Countries.capitals());
b.addActionListener(e -> {
for(Map.Entry me : m.entrySet())
t.append(me.getKey() + ": "+ me.getValue()+"\n");
});
c.addActionListener(e -> t.setText(""));
setLayout(new FlowLayout());
add(new JScrollPane(t));
add(b);
add(c);
}
public static void main(String[] args) {
run(new TextArea(), 475, 425);
}
2015-09-07 11:44:36 -06:00
}