/* * ======================================================================= * DemoTextArea.java : This applet exercises the capabilities of the class * JTextArea. The contents of the text area may be manipulated by using * the text field and buttons displayed. A message may be typed in the * text field box and then positioned using the button operations. * You can also select a text segment and press delete to remove it. Other * buttons are used for cat, copy and paste. * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by : Mark Austin March, 2001 * ======================================================================= */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DemoTextArea extends JApplet { // 1. Declare the references for the following objects. Container container; JLabel label = null; JTextField textField = null; JTextArea textArea = null; JButton insertButton = null; JButton deleteButton = null; JButton cutButton = null; JButton copyButton = null; JButton pasteButton = null; public void init() { // 2. Get a handle to the applet's container. Container container = this.getContentPane(); // 3. Create the fonts for label and text area. Font labelFont = new Font("SanSerif", Font.BOLD, 14); Font textFont = new Font("Dialog", Font.PLAIN, 12); // 4. Use the gridbag layout for the applet. GridBagLayout gridbag = new GridBagLayout(); container.setLayout(gridbag); GridBagConstraints c = new GridBagConstraints(); // 5. Add a label. c.fill = c.BOTH; c.insets = new Insets(2,2,2,2); c.gridx = 0; c.gridy = 0; c.gridwidth = 5; c.gridheight = 1; c.anchor = c.WEST; c.weightx = 1.0; c.weighty = 1.0; label = new JLabel("Enter Text: "); label.setFont(labelFont); gridbag.setConstraints(label, c); container.add(label); // add the label // 6. Add a text field. c.anchor = c.CENTER; c.gridx = 0; c.gridy = 1; textField = new JTextField(); gridbag.setConstraints(textField, c); container.add(textField); // add the text-field // 7. Add the Insert button. c.gridx = 0; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1; c.fill = c.BOTH; insertButton = new JButton("Insert"); insertButton.setBackground(Color.lightGray); gridbag.setConstraints(insertButton, c); container.add(insertButton); // add insert button ButtonListener inButtonListener = new ButtonListener(); insertButton.addActionListener(inButtonListener); // 8. Add the Delete button. c.gridx = 1; c.gridy = 2; deleteButton = new JButton("Delete"); deleteButton.setBackground(Color.lightGray); gridbag.setConstraints(deleteButton, c); container.add(deleteButton); // add the delete button ButtonListener dlButtonListener = new ButtonListener(); deleteButton.addActionListener(dlButtonListener); // 9. Add the Cut button. c.gridx = 2; c.gridy = 2; cutButton = new JButton("Cut"); cutButton.setBackground(Color.lightGray); gridbag.setConstraints(cutButton, c); container.add(cutButton); // add the cut button ButtonListener ctButtonListener = new ButtonListener(); cutButton.addActionListener(ctButtonListener); // 10. Add the Copy button. c.gridx = 3; c.gridy = 2; copyButton = new JButton("Copy"); copyButton.setBackground(Color.lightGray); gridbag.setConstraints(copyButton, c); container.add(copyButton); // add the copy button ButtonListener cpButtonListener = new ButtonListener(); copyButton.addActionListener(cpButtonListener); // 11. Add the Paste button. c.gridx = 4; c.gridy = 2; pasteButton = new JButton("Paste"); pasteButton.setBackground(Color.lightGray); gridbag.setConstraints(pasteButton, c); container.add(pasteButton); // add the paste button ButtonListener psButtonListener = new ButtonListener(); pasteButton.addActionListener(psButtonListener); // 12. Add the text area. c.gridx = 0; c.gridy = 3; c.gridwidth = 5; c.gridheight = 3; c.weightx = 1.0; c.weighty = 1.0; c.anchor = c.CENTER; c.fill = c.BOTH; String someText = "The Swing 'JTextArea' is a " + "light-weight component."; textArea = new JTextArea(someText, // To be displayed initially 20, // Number of rows. 30); // Number of columns. textArea.setFont(textFont); textArea.setBackground(Color.white); textArea.setSelectionColor(Color.yellow); textArea.setTabSize(5); // The tab size. textArea.setLineWrap(true); // Wrap the line at the container end. JScrollPane scrollPane = new JScrollPane(textArea); gridbag.setConstraints(scrollPane, c); container.add(scrollPane); // Add the text area. } // 13. Button listener class. class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton) e.getSource(); if (b == insertButton) { try { // Insert text at the caret position. The text is // is retrieved from the text field by using getText(). textArea.insert(textField.getText(), textArea.getCaretPosition()); } catch (IllegalArgumentException excep) { } } else if (b == deleteButton) { textArea.replaceSelection(""); // Selected text is deleted. } else if (b == cutButton) { textArea.cut(); // JTextArea cut operation. } else if (b == copyButton) { textArea.copy(); // JTextArea copy operation. } else if (b == pasteButton) { textArea.paste(); // JTextArea paste operation. } } } }