Two linked JTextPanes Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Two linked JTextPanes without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have two JTextPanes with static size and I want to connect them like two pages in text processor. If I write something in the first JTextPane (page) and it will be too long for one JTextPane then it overflow to the second JTextPane (page).

I don’t want something like this (first pane have been expanded):

picture 1

But I want something like this:

picture 2

There is my testing code:

public class Test2 extends JFrame{
    JTextPane textPane1;
    JTextPane textPane2;

    /**
     * Inicialization
     */
    public Test2() {
        textPane1 = getTextPane();
        textPane2 = getTextPane();

        getContentPane().setLayout(new GridBagLayout());

        getContentPane().add(textPane1,getGridBagConstraints(0));
        getContentPane().add(textPane2,getGridBagConstraints(1));
    }

    /**
     * Settings for text panes
     */
    private JTextPane getTextPane(){
        JTextPane pane = new JTextPane();
        pane.setEditorKit(new WrapEditorKit());
        pane.setMaximumSize(new java.awt.Dimension(100, 108));
        pane.setMinimumSize(new java.awt.Dimension(100, 108));
        pane.setPreferredSize(new java.awt.Dimension(100, 108));
        pane.setBorder(new StrokeBorder(new BasicStroke()));
        return pane;
    }

    /**
     * Setting layout for text panes.
     */
    private GridBagConstraints getGridBagConstraints(int index){
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = index;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        return gridBagConstraints;
    }

    /**
     * Main
     */
    public static void main(String[] args) { 
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new Test2();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    /**
     * Just class for wrapping text in text panes.
     */
    private static class WrapEditorKit extends StyledEditorKit {
        ViewFactory defaultFactory = new WrapColumnFactory();

        @Override
        public ViewFactory getViewFactory() {
            return defaultFactory;
        }

        private class WrapColumnFactory implements ViewFactory {
            @Override
            public javax.swing.text.View create(Element elem) {
                String kind = elem.getName();
                if (kind != null) {
                    switch (kind) {
                        case AbstractDocument.ContentElementName:
                            return new WrapLabelView(elem);
                        case AbstractDocument.ParagraphElementName:
                            return new ParagraphView(elem);
                        case AbstractDocument.SectionElementName:
                            return new BoxView(elem, javax.swing.text.View.Y_AXIS);
                        case StyleConstants.ComponentElementName:
                            return new ComponentView(elem);
                        case StyleConstants.IconElementName:
                            return new IconView(elem);
                    }
                }

                // default to text display
                return new LabelView(elem);
            }
        }

        private class WrapLabelView extends LabelView {
            public WrapLabelView(Element elem) {
                super(elem);
            }

            @Override
            public float getMinimumSpan(int axis) {
                switch (axis) {
                    case javax.swing.text.View.X_AXIS:
                        return 0;
                    case javax.swing.text.View.Y_AXIS:
                        return super.getMinimumSpan(axis);
                    default:
                        throw new IllegalArgumentException("Invalid axis: " + axis);
                }
            }
        }
    }
}

Any ideas pleas? I’ve tried many things, but nothing worked.

Answer

Looks like you need pagination See java-sl.com/Pagination_In_JEditorPane.html and may be all the 4 articlaes about pagination features (see here java-sl.com/articles.html ) – StanislavL Dec 15 ’14 at 8:00

We are here to answer your question about Two linked JTextPanes - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji