Java by API/javax.swing/JTextArea

Материал из Java эксперт
Перейти к: навигация, поиск

JTextArea: addCaretListener(CaretListener listener)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane, BorderLayout.CENTER);
    
    CaretListener listener = new CaretListener() {
      public void caretUpdate(CaretEvent caretEvent) {
        System.out.println("Dot: "+ caretEvent.getDot());
        System.out.println("Mark: "+caretEvent.getMark());
      }
    };
    textArea.addCaretListener(listener);
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}





JTextArea: addUndoableEditListener(UndoableEditListener lis)

 
import java.awt.BorderLayout;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Undo Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    UndoManager manager = new UndoManager();
    textArea.getDocument().addUndoableEditListener(manager);
    JToolBar toolbar = new JToolBar();
    JButton undoButton = new JButton(new UndoAction(manager, (String) UIManager
        .get("AbstractUndoableEdit.undoText")));
    toolbar.add(undoButton);
    Container content = frame.getContentPane();
    content.add(toolbar, BorderLayout.NORTH);
    content.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}
abstract class UndoRedoAction extends AbstractAction {
  UndoManager undoManager = new UndoManager();
  String errorMessage = "Cannot undo";
  String errorTitle = "Undo Problem";
  protected UndoRedoAction(UndoManager manager, String name) {
    super(name);
    undoManager = manager;
  }
  public void setErrorMessage(String newValue) {
    errorMessage = newValue;
  }
  public void setErrorTitle(String newValue) {
    errorTitle = newValue;
  }
  protected void showMessage(Object source) {
    if (source instanceof Component) {
      JOptionPane.showMessageDialog((Component) source, errorMessage, errorTitle,
          JOptionPane.WARNING_MESSAGE);
    } else {
      System.err.println(errorMessage);
    }
  }
}
class UndoAction extends UndoRedoAction {
  public UndoAction(UndoManager manager, String name) {
    super(manager, name);
    setErrorMessage("Cannot undo");
    setErrorTitle("Undo Problem");
  }
  public void actionPerformed(ActionEvent actionEvent) {
    try {
      undoManager.undo();
    } catch (CannotUndoException cannotUndoException) {
      showMessage(actionEvent.getSource());
    }
  }
}





JTextArea: append(String str)

 

import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) {
    JTextArea ta = new JTextArea("Initial Text");
    ta.append("some text");
  }
}





JTextArea: getActionMap()

 

import java.awt.ruponent;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTextArea component = new JTextArea();
    NextFocusAction nextFocusAction = new NextFocusAction();
    PrevFocusAction prevFocusAction = new PrevFocusAction();
    
    component.getActionMap().put(nextFocusAction.getValue(Action.NAME), nextFocusAction);
    component.getActionMap().put(prevFocusAction.getValue(Action.NAME), prevFocusAction);
  }
}
class NextFocusAction extends AbstractAction{
  public NextFocusAction(){
    super("Move Focus Forwards");
  }
  public void actionPerformed(ActionEvent evt) {
    ((Component) evt.getSource()).transferFocus();
  }
}
class PrevFocusAction extends AbstractAction {
  public PrevFocusAction(){
    super("Move Focus Backwards");
  }
  public void actionPerformed(ActionEvent evt) {
    ((Component) evt.getSource()).transferFocusBackward();
  }
}





JTextArea: getActions()

 
import javax.swing.*;
import javax.swing.text.*;
import java.util.Hashtable;
import java.awt.event.*;
import java.awt.BorderLayout;
public class KeymapExample {
  public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);
    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord(); 
    newmap.addActionForKeyStroke(u, actionU);
    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j=0; j < actionList.length; j+=1)
      lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);
    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action)lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);
    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action)lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);
    area.setKeymap(newmap);
    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("www.\n jexp \n .ru.");
    f.pack();
    f.setVisible(true);
  }
  public static class UpWord extends TextAction {
    public UpWord() {
      super("uppercase-word-action");
    }
    public void actionPerformed(ActionEvent e) {
      JTextComponent comp = getTextComponent(e);
      if (comp == null) return;
      Document doc = comp.getDocument();
      int start = comp.getSelectionStart();
      int end = comp.getSelectionEnd();
      try {
        int left = javax.swing.text.Utilities.getWordStart(comp, start);
        int right = javax.swing.text.Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right-left);
        doc.remove(left, right-left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start); // restore previous position/selection
        comp.setSelectionEnd(end);
      } catch (BadLocationException ble) { return; }
    }
  }
}





JTextArea: getCaretPosition()

 
 
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTextComponent c = new JTextArea();
    c.getCaretPosition();
    if (c.getCaretPosition() < c.getDocument().getLength()) {
      char ch = c.getText(c.getCaretPosition(), 1).charAt(0);
    }
    // Set the caret color
    c.setCaretColor(Color.red);
  }
}





JTextArea: getDocument()

 
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Document;
public class MainClass {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Sharing Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();
    JTextArea textarea1 = new JTextArea();
    Document document = textarea1.getDocument();
    JTextArea textarea2 = new JTextArea(document);
    JTextArea textarea3 = new JTextArea(document);
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.add(new JScrollPane(textarea1));
    content.add(new JScrollPane(textarea2));
    content.add(new JScrollPane(textarea3));
    frame.setSize(300, 400);
    frame.setVisible(true);
  }
}





JTextArea: getFont()

 
import java.awt.Font;
import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) {
    JTextArea textarea = new JTextArea();
    // Get the default tab size
    int tabSize = textarea.getTabSize(); // 8
    // Change the tab size
    tabSize = 4;
    textarea.setTabSize(tabSize);
    Font font = textarea.getFont();
    System.out.println(font);
  }
}





JTextArea: getHighlighter()

 

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
public class Main {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("MultiHighlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea comp = new JTextArea(5, 20);
    comp.setText("this is a test");
    frame.getContentPane().add(new JScrollPane(comp), BorderLayout.CENTER);
    String charsToHighlight = "a";
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();
    String text = comp.getText().toUpperCase();
    for (int j = 0; j < text.length(); j += 1) {
      char ch = text.charAt(j);
      if (charsToHighlight.indexOf(ch) >= 0)
        h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter);
    }
    frame.pack();
    frame.setVisible(true);
  }
}





JTextArea: getKeymap()

 
import javax.swing.*;
import javax.swing.text.*;
import java.util.Hashtable;
import java.awt.event.*;
import java.awt.BorderLayout;
public class KeymapExample {
  public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);
    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord(); 
    newmap.addActionForKeyStroke(u, actionU);
    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j=0; j < actionList.length; j+=1)
      lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);
    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action)lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);
    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action)lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);
    area.setKeymap(newmap);
    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("www.\n jexp \n .ru.");
    f.pack();
    f.setVisible(true);
  }
  public static class UpWord extends TextAction {
    public UpWord() {
      super("uppercase-word-action");
    }
    public void actionPerformed(ActionEvent e) {
      JTextComponent comp = getTextComponent(e);
      if (comp == null) return;
      Document doc = comp.getDocument();
      int start = comp.getSelectionStart();
      int end = comp.getSelectionEnd();
      try {
        int left = javax.swing.text.Utilities.getWordStart(comp, start);
        int right = javax.swing.text.Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right-left);
        doc.remove(left, right-left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start); // restore previous position/selection
        comp.setSelectionEnd(end);
      } catch (BadLocationException ble) { return; }
    }
  }
}





JTextArea: getLineCount()

 
/*line 0 starts at 0, ends at 5
line 1 starts at 5, ends at 12
line 2 starts at 12, ends at 16
offset 0 is on line 0
offset 1 is on line 0
offset 2 is on line 0
offset 3 is on line 0
offset 4 is on line 0
offset 5 is on line 1
offset 6 is on line 1
offset 7 is on line 1
offset 8 is on line 1
offset 9 is on line 1
offset 10 is on line 1
offset 11 is on line 1
offset 12 is on line 2
offset 13 is on line 2
offset 14 is on line 2
offset 15 is on line 2
offset 16 is on line 2
offset 17 is on javax.swing.text.BadLocationException: Can"t translate offset to line
*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
public class MainClass {
  public static void main(String[] args) {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane(ta);
    ta.append("www.\n");
    ta.append("jexp\n");
    ta.append(".ru");
    try {
      for (int n = 0; n < ta.getLineCount(); n += 1)
        System.out.println("line " + n + " starts at " + ta.getLineStartOffset(n) + ", ends at "
            + ta.getLineEndOffset(n));
      System.out.println();
      int n = 0;
      while (true) {
        System.out.print("offset " + n + " is on ");
        System.out.println("line " + ta.getLineOfOffset(n));
        n += 1;
      }
    } catch (BadLocationException ex) {
      System.out.println(ex);
    }
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
    f.setSize(150, 150);
    f.setVisible(true);
  }
}





JTextArea: getLineEndOffset(int line)

 
/*line 0 starts at 0, ends at 5
line 1 starts at 5, ends at 12
line 2 starts at 12, ends at 16
offset 0 is on line 0
offset 1 is on line 0
offset 2 is on line 0
offset 3 is on line 0
offset 4 is on line 0
offset 5 is on line 1
offset 6 is on line 1
offset 7 is on line 1
offset 8 is on line 1
offset 9 is on line 1
offset 10 is on line 1
offset 11 is on line 1
offset 12 is on line 2
offset 13 is on line 2
offset 14 is on line 2
offset 15 is on line 2
offset 16 is on line 2
offset 17 is on javax.swing.text.BadLocationException: Can"t translate offset to line
*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
public class MainClass {
  public static void main(String[] args) {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane(ta);
    ta.append("www.\n");
    ta.append("jexp\n");
    ta.append(".ru");
    try {
      for (int n = 0; n < ta.getLineCount(); n += 1)
        System.out.println("line " + n + " starts at " + ta.getLineStartOffset(n) + ", ends at "
            + ta.getLineEndOffset(n));
      System.out.println();
      int n = 0;
      while (true) {
        System.out.print("offset " + n + " is on ");
        System.out.println("line " + ta.getLineOfOffset(n));
        n += 1;
      }
    } catch (BadLocationException ex) {
      System.out.println(ex);
    }
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
    f.setSize(150, 150);
    f.setVisible(true);
  }
}





JTextArea.getLineOfOffset(int offset)

 
/*line 0 starts at 0, ends at 5
line 1 starts at 5, ends at 12
line 2 starts at 12, ends at 16
offset 0 is on line 0
offset 1 is on line 0
offset 2 is on line 0
offset 3 is on line 0
offset 4 is on line 0
offset 5 is on line 1
offset 6 is on line 1
offset 7 is on line 1
offset 8 is on line 1
offset 9 is on line 1
offset 10 is on line 1
offset 11 is on line 1
offset 12 is on line 2
offset 13 is on line 2
offset 14 is on line 2
offset 15 is on line 2
offset 16 is on line 2
offset 17 is on javax.swing.text.BadLocationException: Can"t translate offset to line
*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
public class MainClass {
  public static void main(String[] args) {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane(ta);
    ta.append("www.\n");
    ta.append("jexp\n");
    ta.append(".ru");
    try {
      for (int n = 0; n < ta.getLineCount(); n += 1)
        System.out.println("line " + n + " starts at " + ta.getLineStartOffset(n) + ", ends at "
            + ta.getLineEndOffset(n));
      System.out.println();
      int n = 0;
      while (true) {
        System.out.print("offset " + n + " is on ");
        System.out.println("line " + ta.getLineOfOffset(n));
        n += 1;
      }
    } catch (BadLocationException ex) {
      System.out.println(ex);
    }
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
    f.setSize(150, 150);
    f.setVisible(true);
  }
}





JTextArea: getLineStartOffset(int line)

 
/*line 0 starts at 0, ends at 5
line 1 starts at 5, ends at 12
line 2 starts at 12, ends at 16
offset 0 is on line 0
offset 1 is on line 0
offset 2 is on line 0
offset 3 is on line 0
offset 4 is on line 0
offset 5 is on line 1
offset 6 is on line 1
offset 7 is on line 1
offset 8 is on line 1
offset 9 is on line 1
offset 10 is on line 1
offset 11 is on line 1
offset 12 is on line 2
offset 13 is on line 2
offset 14 is on line 2
offset 15 is on line 2
offset 16 is on line 2
offset 17 is on javax.swing.text.BadLocationException: Can"t translate offset to line
*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
public class MainClass {
  public static void main(String[] args) {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane(ta);
    ta.append("www.\n");
    ta.append("jexp\n");
    ta.append(".ru");
    try {
      for (int n = 0; n < ta.getLineCount(); n += 1)
        System.out.println("line " + n + " starts at " + ta.getLineStartOffset(n) + ", ends at "
            + ta.getLineEndOffset(n));
      System.out.println();
      int n = 0;
      while (true) {
        System.out.print("offset " + n + " is on ");
        System.out.println("line " + ta.getLineOfOffset(n));
        n += 1;
      }
    } catch (BadLocationException ex) {
      System.out.println(ex);
    }
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
    f.setSize(150, 150);
    f.setVisible(true);
  }
}





JTextArea: insert(String str, int pos)

 

import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) {
    JTextArea ta = new JTextArea("Initial Text");
    int pos = 5;
    ta.insert("some text", pos);
  }
}





JTextArea: isManagingFocus()

 
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
  public static void main(String args[]) {
    String title = (args.length == 0 ? "TextArea Example" : args[0]);
    JFrame frame = new JFrame(title);
    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(0, 2));
    JTextArea leftTextArea = new JTextArea();
    content.add(leftTextArea);
    leftTextArea.paste();
    JTextArea rightTextArea = new JTextArea() {
      public boolean isManagingFocus() {
        return false;
      }
    };
    rightTextArea.paste();
    JScrollPane rightPane = new JScrollPane(rightTextArea);
    content.add(rightPane);
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}





JTextArea: paste()

 
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea leftTextArea = new JTextArea();
    frame.add(leftTextArea);
    leftTextArea.paste();
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}





JTextArea: read(Reader in, Object desc)

 
import java.awt.BorderLayout;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);
    
    FileReader reader = null;
    try {
      reader = new FileReader("fileName.txt");
      nameTextField.read(reader, "fileName.txt");
    } catch (IOException exception) {
      System.err.println("Load oops");
      exception.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException exception) {
          System.err.println("Error closing reader");
          exception.printStackTrace();
        }
      }
    }
    
    frame.setSize(250, 100);
    frame.setVisible(true);
  }
}





JTextArea: replaceRange(String str, int start, int end)

 
import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) {
    JTextArea ta = new JTextArea("Initial Text");
    int start = 0;
    int end = 5;
    ta.replaceRange(null, start, end);
  }
}





JTextArea: setCaretColor(Color c)

 
 
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTextComponent c = new JTextArea();
    c.getCaretPosition();
    if (c.getCaretPosition() < c.getDocument().getLength()) {
      char ch = c.getText(c.getCaretPosition(), 1).charAt(0);
    }
    // Set the caret color
    c.setCaretColor(Color.red);
  }
}





JTextArea: setLineWrap(boolean wrap)

 
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
public class MainClass extends JFrame {
  static String sometext = "Text Text Text Text Text Text Text Text Text Text Text Text ";
  public MainClass() {
    super("Simple SplitPane Frame");
    setSize(450, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JTextArea jt1 = new JTextArea(sometext);
    JTextArea jt2 = new JTextArea(sometext);
    jt1.setLineWrap(true);
    jt2.setLineWrap(true);
    jt1.setMinimumSize(new Dimension(150, 150));
    jt2.setMinimumSize(new Dimension(150, 150));
    jt1.setPreferredSize(new Dimension(250, 200));
    JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jt1, jt2);
    getContentPane().add(sp, BorderLayout.CENTER);
  }
  public static void main(String args[]) {
    MainClass ssb = new MainClass();
    ssb.setVisible(true);
  }
}





JTextArea: setTabSize(int size)

 
import java.awt.Font;
import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) {
    JTextArea textarea = new JTextArea();
    // Get the default tab size
    int tabSize = textarea.getTabSize(); // 8
    // Change the tab size
    tabSize = 4;
    textarea.setTabSize(tabSize);
    Font font = textarea.getFont();
    System.out.println(font);
  }
}





JTextArea: setWrapStyleWord(boolean word)

 
/*line 0 starts at 0, ends at 5
line 1 starts at 5, ends at 12
line 2 starts at 12, ends at 16
offset 0 is on line 0
offset 1 is on line 0
offset 2 is on line 0
offset 3 is on line 0
offset 4 is on line 0
offset 5 is on line 1
offset 6 is on line 1
offset 7 is on line 1
offset 8 is on line 1
offset 9 is on line 1
offset 10 is on line 1
offset 11 is on line 1
offset 12 is on line 2
offset 13 is on line 2
offset 14 is on line 2
offset 15 is on line 2
offset 16 is on line 2
offset 17 is on javax.swing.text.BadLocationException: Can"t translate offset to line
*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
public class MainClass {
  public static void main(String[] args) {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane(ta);
    ta.append("www.\n");
    ta.append("jexp\n");
    ta.append(".ru");
    try {
      for (int n = 0; n < ta.getLineCount(); n += 1)
        System.out.println("line " + n + " starts at " + ta.getLineStartOffset(n) + ", ends at "
            + ta.getLineEndOffset(n));
      System.out.println();
      int n = 0;
      while (true) {
        System.out.print("offset " + n + " is on ");
        System.out.println("line " + ta.getLineOfOffset(n));
        n += 1;
      }
    } catch (BadLocationException ex) {
      System.out.println(ex);
    }
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
    f.setSize(150, 150);
    f.setVisible(true);
  }
}





JTextArea: write(Writer out)

 
import java.awt.BorderLayout;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);
    
    FileWriter writer = null;
    try {
      writer = new FileWriter("filename.txt");
      nameTextField.write(writer);
    } catch (IOException exception) {
      System.err.println("Save oops");
      exception.printStackTrace();
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (IOException exception) {
          System.err.println("Error closing writer");
          exception.printStackTrace();
        }
      }
    }
    
    frame.setSize(250, 100);
    frame.setVisible(true);
  }
}





JTextField: setHorizontalAlignment(int pos)

 
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(0, 1));
    JTextField textField = new JTextField("Left");
    textField.setHorizontalAlignment(JTextField.LEFT);
    frame.add(textField);
    textField = new JTextField("Center");
    textField.setHorizontalAlignment(JTextField.CENTER);
    frame.add(textField);
    textField = new JTextField("Right");
    textField.setHorizontalAlignment(JTextField.RIGHT);
    frame.add(textField);
    textField = new JTextField("Leading");
    textField.setHorizontalAlignment(JTextField.LEADING);
    frame.add(textField);
    textField = new JTextField("Trailing");
    textField.setHorizontalAlignment(JTextField.TRAILING);
    frame.add(textField);
    frame.pack();
    frame.setSize(250, (int) frame.getSize().getHeight());
    frame.setVisible(true);
  }
}





new JTextArea(Document document)

 
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Document;
public class MainClass {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Sharing Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();
    JTextArea textarea1 = new JTextArea();
    Document document = textarea1.getDocument();
    JTextArea textarea2 = new JTextArea(document);
    JTextArea textarea3 = new JTextArea(document);
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.add(new JScrollPane(textarea1));
    content.add(new JScrollPane(textarea2));
    content.add(new JScrollPane(textarea3));
    frame.setSize(300, 400);
    frame.setVisible(true);
  }
}





new JTextArea(String text)

 
import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) {
    // Create a text area with some initial text
    JTextArea textarea = new JTextArea("Initial Text");
    int rows = 20;
    int cols = 30;
    textarea = new JTextArea("Initial Text", rows, cols);
  }
}





new JTextArea(String text, int rows, int columns)

 
import javax.swing.JTextArea;
public class Main {
  public static void main(String[] argv) {
    // Create a text area with some initial text
    JTextArea textarea = new JTextArea("Initial Text");
    int rows = 20;
    int cols = 30;
    textarea = new JTextArea("Initial Text", rows, cols);
  }
}