3

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.

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
  • Why not one big TextPane? – Marv Dec 14 '14 at 21:56
  • 1
    I'm doing a simple text processor. One JTextPane represents just one page. I've simplified example, that there remained only the essentials. – Jan Brzobohatý Dec 14 '14 at 22:16
  • 1
    Not sure I understand the design. What happens if you have 3 pages of text? Do you need 3 text panes? – camickr Dec 14 '14 at 22:30
  • Yes I need even more then 3 text panes. If I write something in the first text pane and it will be too long for one text pane then it overflow to the second textPane and if it will be still too long for two text panes then there will be third text pane. All will be interconnected. – Jan Brzobohatý Dec 14 '14 at 22:53
  • 2
    Looks like you need pagination See http://java-sl.com/Pagination_In_JEditorPane.html and may be all the 4 articlaes about pagination features (see here http://java-sl.com/articles.html) – StanislavL Dec 15 '14 at 08:00
  • Linked textboxes are more complicated but also possible – StanislavL Dec 15 '14 at 08:01
  • Ou, I overlooked this comment. Thank you very much StanislavL. That is exactly what I was looking for. – Jan Brzobohatý Dec 20 '14 at 14:19

3 Answers3

0

Why don't u try a check function which counts the words in first pane and if the word count exceeds the limit then automatically open the second jtextpane then u will starting over it nd stop the function there by The function will check the word count on ever even so call this function in action performed implicitly first before anything happens nd then continue with your code. :)I guess it will solve the problem

  • Sorry, I forgot to write that text will change the size, font, and other parameters. Even will be possible to inserted images into the text, so this option would not work. – Jan Brzobohatý Dec 14 '14 at 22:23
0

If I well remember, when you fix the size of JTextPane, you can get the length of the content with getPrefferedSize().height. If the preferred size is > than the textPane itself, you can send the overflow to the next textPane. You can use modelToView and viewToModel to help you in this last step, or you can use FontMetrics for everything. Ask if you need details.

edit : Of course, you have to not specify the 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 size of a JOptionPane. Resize dialog message (JOptionPane) for long sentence with fixed width

In your case, you should do sth like :

    ((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 details

Community
  • 1
  • 1
Sharcoux
  • 5,546
  • 7
  • 45
  • 78
  • Thank you very much man. I did not have any idea about these functions (viewToModel and modelToView). It works. But I have last question for you. What do you mean by set fix size of JTextPane without setting a preferred size? How can I do that? I had solved problem by putting every JTextPane to JViewport and set preferred size to JViewPort. After that I checked if height of JTextPane is bigger than preferred size of JViewPort. It works for overflowing but there are some issues with scrolling in main frame because of these JViewports. Do you have any idea how to do that without JViewports? – Jan Brzobohatý Dec 20 '14 at 11:41
  • Ok, see my edit to get more information about how you could fix this without JViewports. Let me know if it worked. – Sharcoux Dec 21 '14 at 13:24
0

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