Simple Editor Demo : TextArea « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » TextArea 




Simple Editor Demo
Simple Editor Demo
 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/

// SimpleEditor.java
//An example showing several DefaultEditorKit features. This class is designed
//to be easily extended for additional functionality.
//

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;

public class SimpleEditor extends JFrame {

  private Action openAction = new OpenAction();

  private Action saveAction = new SaveAction();

  private JTextComponent textComp;

  private Hashtable actionHash = new Hashtable();

  public static void main(String[] args) {
    SimpleEditor editor = new SimpleEditor();
    editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    editor.setVisible(true);
  }

  // Create an editor.
  public SimpleEditor() {
    super("Swing Editor");
    textComp = createTextComponent();
    makeActionsPretty();

    Container content = getContentPane();
    content.add(textComp, BorderLayout.CENTER);
    content.add(createToolBar(), BorderLayout.NORTH);
    setJMenuBar(createMenuBar());
    setSize(320240);
  }

  // Create the JTextComponent subclass.
  protected JTextComponent createTextComponent() {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    return ta;
  }

  // Add icons and friendly names to actions we care about.
  protected void makeActionsPretty() {
    Action a;
    a = textComp.getActionMap().get(DefaultEditorKit.cutAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
    a.putValue(Action.NAME, "Cut");

    a = textComp.getActionMap().get(DefaultEditorKit.copyAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
    a.putValue(Action.NAME, "Copy");

    a = textComp.getActionMap().get(DefaultEditorKit.pasteAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));
    a.putValue(Action.NAME, "Paste");

    a = textComp.getActionMap().get(DefaultEditorKit.selectAllAction);
    a.putValue(Action.NAME, "Select All");
  }

  // Create a simple JToolBar with some buttons.
  protected JToolBar createToolBar() {
    JToolBar bar = new JToolBar();

    // Add simple actions for opening & saving.
    bar.add(getOpenAction()).setText("");
    bar.add(getSaveAction()).setText("");
    bar.addSeparator();

    // Add cut/copy/paste buttons.
    bar.add(textComp.getActionMap().get(DefaultEditorKit.cutAction))
        .setText("");
    bar.add(textComp.getActionMap().get(DefaultEditorKit.copyAction))
        .setText("");
    bar.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction))
        .setText("");
    return bar;
  }

  // Create a JMenuBar with file & edit menus.
  protected JMenuBar createMenuBar() {
    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    menubar.add(file);
    menubar.add(edit);

    file.add(getOpenAction());
    file.add(getSaveAction());
    file.add(new ExitAction());
    edit.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));
    edit.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));
    edit.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));
    edit.add(textComp.getActionMap().get(DefaultEditorKit.selectAllAction));
    return menubar;
  }

  // Subclass can override to use a different open action.
  protected Action getOpenAction() {
    return openAction;
  }

  // Subclass can override to use a different save action.
  protected Action getSaveAction() {
    return saveAction;
  }

  protected JTextComponent getTextComponent() {
    return textComp;
  }

  // ********** ACTION INNER CLASSES ********** //

  // A very simple exit action
  public class ExitAction extends AbstractAction {
    public ExitAction() {
      super("Exit");
    }

    public void actionPerformed(ActionEvent ev) {
      System.exit(0);
    }
  }

  // An action that opens an existing file
  class OpenAction extends AbstractAction {
    public OpenAction() {
      super("Open"new ImageIcon("icons/open.gif"));
    }

    // Query user for a filename and attempt to open and read the file into
    // the
    // text component.
    public void actionPerformed(ActionEvent ev) {
      JFileChooser chooser = new JFileChooser();
      if (chooser.showOpenDialog(SimpleEditor.this!= JFileChooser.APPROVE_OPTION)
        return;
      File file = chooser.getSelectedFile();
      if (file == null)
        return;

      FileReader reader = null;
      try {
        reader = new FileReader(file);
        textComp.read(reader, null);
      catch (IOException ex) {
        JOptionPane.showMessageDialog(SimpleEditor.this,
            "File Not Found""ERROR", JOptionPane.ERROR_MESSAGE);
      finally {
        if (reader != null) {
          try {
            reader.close();
          catch (IOException x) {
          }
        }
      }
    }
  }

  // An action that saves the document to a file
  class SaveAction extends AbstractAction {
    public SaveAction() {
      super("Save"new ImageIcon("icons/save.gif"));
    }

    // Query user for a filename and attempt to open and write the text
    // component's content to the file.
    public void actionPerformed(ActionEvent ev) {
      JFileChooser chooser = new JFileChooser();
      if (chooser.showSaveDialog(SimpleEditor.this!= JFileChooser.APPROVE_OPTION)
        return;
      File file = chooser.getSelectedFile();
      if (file == null)
        return;

      FileWriter writer = null;
      try {
        writer = new FileWriter(file);
        textComp.write(writer);
      catch (IOException ex) {
        JOptionPane.showMessageDialog(SimpleEditor.this,
            "File Not Saved""ERROR", JOptionPane.ERROR_MESSAGE);
      finally {
        if (writer != null) {
          try {
            writer.close();
          catch (IOException x) {
          }
        }
      }
    }
  }
}
           
         
  














Related examples in the same category
1.Creating a JTextArea Component
2.Insert some text after the 5th character
3.Delete the first 5 characters
4.Enumerating the Lines in a JTextArea Component
5.Modifying Text in a JTextArea Component
6.Fancier custom caret classFancier custom caret class
7.Show line start end offsets in a JTextAreaShow line start end offsets in a JTextArea
8.A TransferHandler and JTextArea that will accept any drop at allA TransferHandler and JTextArea that will accept any drop at all
9.Drop: TextAreaDrop: TextArea
10.Drag and drop: TextArea 2Drag and drop: TextArea 2
11.Caret SampleCaret Sample
12.TextArea Background ImageTextArea Background Image
13.Cut Paste SampleCut Paste Sample
14.TextArea SampleTextArea Sample
15.TextArea ExampleTextArea Example
16.Wrap textareaWrap textarea
17.Replace text in text areaReplace text in text area
18.TextField ExampleTextField Example
19.TextArea Elements 2TextArea Elements 2
20.TextArea ElementsTextArea Elements
21.TextArea Views 2TextArea Views 2
22.TextArea Views 3TextArea Views 3
23.TextArea with UnicodeTextArea with Unicode
24.Internationalized Graphical User Interfaces: unicode cut and pasteInternationalized Graphical User Interfaces: unicode cut and paste
25.Text Component Demo 2Text Component Demo 2
26.Text Component DemoText Component Demo
27.Text Input DemoText Input Demo
28.Text Components Sampler DemoText Components Sampler Demo
29.TextArea Share ModelTextArea Share Model
30.Set the start of the selection; ignored if new start is < end
31.Set the end of the selection; ignored if new end is > start
32.Set the caret color
33.Setting the Tab Size of a JTextArea Component
34.Moving the Focus with the TAB Key in a JTextArea Component
35.Enabling Word-Wrapping and Line-Wrapping in a JTextArea Component
36.Append some text to JTextArea
37.Replace the first 3 characters with some text
38.Enable word-wrapping
39.Enumerate the content elements with a ElementIterator
40.Highlight of discontinous string
41.Copy selected text from one text area to another
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.