Share document model between JTextArea in Java

Description

The following code shows how to share document model between JTextArea.

Example


/*from   w w  w .  j  av  a2  s  .  c  o m*/
    
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Document;

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

The code above generates the following result.

Share document model between JTextArea in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer