0

I'm creating a GUI with two JPanels, one for typing and another to show the same text that I type in the first JPanel. How can I make the second JPanel printable? Is there a way to show in the second JPanel the same text that I type in the first JPanel?

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
aka_dudy
  • 3
  • 2
  • 2
    show us what you've tried so far ...and read this https://stackoverflow.com/help/minimal-reproducible-example – JavaMan Nov 10 '20 at 18:05
  • 1
    *Is there a way to show in the second JPanel the same text that I type in the first JPanel?* Yes, there is. Create two JPanels, each with a JTextArea. Copy the contents of the first JTextArea to the second JTextArea. – Gilbert Le Blanc Nov 10 '20 at 18:53

1 Answers1

2

You'll want to have them share the same Document.

textArea2.setDocument(textArea1.getDocument());
public void createAndShowGUI() {

    // ... frame, panel etc.
    // each panel has BorderLayout

    scrollPane1 = new JScrollPane();
    panel1.add(scrollPane1, BorderLayout.CENTER);

    scrollPane2 = new JScrollPane();
    panel2.add(scrollPane2, BorderLayout.CENTER);
    
    textArea1 = new JTextArea();
    scrollPane1.setViewportView(textArea1);
    textArea1.requestFocus();

    textArea2 = new JTextArea();
    scrollPane2.setViewportView(textArea2);
    textArea2.setEditable(false);

    textArea1.getDocument().addDocumentListener(new MyDocumentListener());
    textArea2.getDocument().addDocumentListener(new MyDocumentListener());
}

class MyDocumentListener implements DocumentListener {
    public void insertUpdate(DocumentEvent e) {
        textArea2.setDocument(textArea1.getDocument());
        textArea1.setCaretPosition(textArea1.getDocument().getLength());
        textArea2.setCaretPosition(textArea2.getDocument().getLength());
    }
    public void removeUpdate(DocumentEvent e) {
        textArea2.setDocument(textArea1.getDocument());
        textArea1.setCaretPosition(textArea1.getDocument().getLength());
        textArea2.setCaretPosition(textArea2.getDocument().getLength());
    }
    public void changedUpdate(DocumentEvent e) {
        // Plain text components don't fire these events.
    }
}

From: The Tutorials and this and this.

EDIT: Here is the full example of this implementation:

import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.event.*;

class DocumentListenerTest {

    JFrame frame;
    JPanel panel, panel1, panel2;
    JScrollPane scrollPane1, scrollPane2;
    JTextArea textArea1, textArea2;
    JSplitPane splitPane;

    public DocumentListenerTest() {
        createAndShowGUI();
    }

    public void createAndShowGUI() {

        frame = new JFrame("Copy Text");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setMinimumSize(new Dimension(200, 200));

        panel = new JPanel();
        panel.setLayout(new BorderLayout(5, 5));
        frame.setContentPane(panel);

        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        panel.add(panel1);

        panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        panel.add(panel2, BorderLayout.SOUTH);

        scrollPane1 = new JScrollPane();
        panel1.add(scrollPane1, BorderLayout.CENTER);

        scrollPane2 = new JScrollPane();
        panel2.add(scrollPane2, BorderLayout.CENTER);

        textArea1 = new JTextArea();
        scrollPane1.setViewportView(textArea1);
        textArea1.requestFocus();

        textArea2 = new JTextArea();
        scrollPane2.setViewportView(textArea2);
        textArea2.setEditable(false);

        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane1, scrollPane2);
        panel.add(splitPane, BorderLayout.CENTER);
        splitPane.setDividerLocation(0.5);
        splitPane.setResizeWeight(0.5);

        textArea1.getDocument().addDocumentListener(new MyDocumentListener());
        textArea2.getDocument().addDocumentListener(new MyDocumentListener());

        frame.pack();
        frame.setVisible(true);
    }

    class MyDocumentListener implements DocumentListener {
        public void insertUpdate(DocumentEvent e) {
            textArea2.setDocument(textArea1.getDocument());
            //textArea1.setCaretPosition(textArea1.getDocument().getLength());
            //textArea2.setCaretPosition(textArea2.getDocument().getLength());
            textArea2.setCaretPosition(textArea1.getCaretPosition());
        }
        public void removeUpdate(DocumentEvent e) {
            textArea2.setDocument(textArea1.getDocument());
            //textArea1.setCaretPosition(textArea1.getDocument().getLength());
            //textArea2.setCaretPosition(textArea2.getDocument().getLength());
            textArea2.setCaretPosition(textArea1.getCaretPosition());
        }
        public void changedUpdate(DocumentEvent e) {
            // Plain text components don't fire these events.
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DocumentListenerTest();
            }
        });
    }
}

Here is a sample gif: enter image description here

Peter Alsen
  • 320
  • 2
  • 5
  • 15