Two related JTextPanes

I have two JTextPanes with static size and I want to link them as two pages in a word processor. If I write something on the first JTextPane (page) and it gets too long for one JTextPane, it overflows to the second JTextPane (page).

I don't want something like this (the first panel has been expanded):

picture 1

But I want something like this:

picture 2

There is my test 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 supplication ideas? I've tried a lot of things but nothing worked.

+3


source to share


3 answers


Sounds like you need pagination. See java-sl.com/Pagination_In_JEditorPane.html and there could be all 4 articles on pagination options (see here java-sl.com/articles.html) - StanislavL Dec 15 '14 at 8:00



0


source


Why don't you try the check function that counts the words in the first pane and if the word count exceeds the limit then the second jtextpane opens automatically, then u start with it stop the function there The function will check the word count always, so call this function in action. executable implicitly before anything happens and then continue with your code. :) I think this will solve the problem



0


source


If I remember well, when you fix the size of the JTextPane, you can get the length of the content with getPrefferedSize().height

. If the preferred size is greater than> textPane, you can send the overflow to the next text pane. You can use modelToView and viewToModel to help you with this last step, or you can use FontMetrics for everything. Ask if you need details.

edit: Of course, you don't have to specify your preferredSize programmatically for this to work!

In this example, you can see an example of how to get the height of the JTextPane content to adapt the JOptionPane's size. Resize Dialog (JOptionPane) for long sentence with fixed width

In your case, you should do like this:

    ((AbstractDocument)jtp.getDocument()).setDocumentFilter(new DocumentFilter() {
        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            fb.insertString(offset, string, attr);
            if(jtp.getPreferredSize().height>Editeur.this.getHeight()) {
                //your code here to remove the excess
            }
        }
        @Override
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String str, AttributeSet attrs) throws BadLocationException {
            fb.replace(offset, length, str, attrs);
            if(str!=null && str.length()!=0) {//Has there been any insert ?
                if(jtp.getPreferredSize().height>Editeur.this.getHeight()) {
                    //your code here to remove the excess
                }
            }
        }

    });

      

Ask if you need more information

0


source







All Articles