1

I'm struggling with the following issue:

I have multiple JScrollPanes at different positions in the layout. Everything works when while using only one JScrollPane. Unfortunately the second one is getting me into deep troubles.

Furthermore, there are the following requirements:

-A JTextPane has to be used (richt text formating is required)

-No third party libraries (would make life easier, but it isn't currently allowed here)

-The JTextPane should still behave as it currently does (Explanation: There are 2 JTextPanes separeted by a JSplitter. They behave differntly due to a different count of JScrollPanes. The goal is that the behave in all situations the same (first one))

Below is the code I've wrote:

public class ScrollExample extends JPanel {
    public ScrollExample() {
        super(new BorderLayout());
        JTextPane textPane1 = new JTextPane();
        textPane1.setEditorKit(new WrapEditorKit());

        JTextPane textPane2 = new JTextPane();
        textPane2.setEditorKit(new WrapEditorKit());

        JScrollPane scrollPaneText1 = new JScrollPane(textPane1);
        scrollPaneText1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        JScrollPane scrollPaneText2 = new JScrollPane(textPane2);
        scrollPaneText2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(scrollPaneText2, BorderLayout.CENTER);
        panel.add(new JButton("Example"), BorderLayout.NORTH);

        JScrollPane secondScrollPane = new JScrollPane(panel);
        secondScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        secondScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPaneText1, secondScrollPane);
        splitPane.setDividerLocation(100);

        add(splitPane);

        textPane1.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
        textPane2.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
    }

    public static void main(String[] args) {
        ScrollExample example = new ScrollExample();
        JFrame frame = new JFrame("Example");
        frame.setLayout(new BorderLayout());
        frame.add(example, BorderLayout.CENTER);
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setBounds(100, 50, 600, 400);
                frame.setVisible(true);
            }
        });
    }

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

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

    public class WrapEditorKit extends StyledEditorKit {
        protected ViewFactory _factory = new WrapColumnFactory();

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

    public class WrapColumnFactory implements ViewFactory {
        @Override
        public View create(Element elem) {
            switch(elem.getName()) {
                case AbstractDocument.ContentElementName:
                    return new WrapLabelView(elem);
                case AbstractDocument.ParagraphElementName:
                    return new ParagraphView(elem);
                case AbstractDocument.SectionElementName:
                    return new BoxView(elem, View.Y_AXIS);
                case StyleConstants.ComponentElementName:
                    return new ComponentView(elem);
                case StyleConstants.IconElementName:
                    return new IconView(elem);
            }
        return new LabelView(elem);
        }
    }
}

Explanation:

-The inner classes are needed for the right behaviour of the JTextPane (it will break "long" words instead of messing up the UI).

-The upper portion of the JSplitPane shows how the JTextPane behaves correctly (text wrapping and adding scrollbar (vertical) when needed)

-The lower portion adds a random button and the JTextPane (incl. the JScrollPane). The button is just for illustration as in this region many other components should take part of the UI.

The issue now is in the lower part of the JSpitPane. The JScrollPane of the JTextPane doesn't behave the same way as it does when there isn't a second JScrollPane.

Does anyone know or could give me a hint how to get the same behaviour on both JTextPane's JScrollPanes?

EDIT #1:

@rdonuk came up with a solution that works. I'd still prefer a solution without relying on using any of the set(Preferred|Maximum|Minimum) methods. Furthermore, i came up with a solution that works in my specific case, but might not work with other LayoutManager. Furthermore, I don't like that approach either and I'm still looking for a better solution.

EDIT #2:

Adjusted requirements for clarification.

EDIT #3:

Third solution added (provided by @MadProgrammer).

Solution 1

See below for @rdonuk's solution

Solution 2

I simply overwrote the getPreferredSize methods of the JScrollPanes:

public class ScrollExample extends JPanel {
    public ScrollExample() {
        super(new BorderLayout());
        JTextPane textPane1 = new JTextPane();
        textPane1.setEditorKit(new WrapEditorKit());

        JTextPane textPane2 = new JTextPane();
        textPane2.setEditorKit(new WrapEditorKit());

        JScrollPane scrollPaneText1 = new JScrollPane(textPane1) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1,1);
            }
        };
        scrollPaneText1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        JScrollPane scrollPaneText2 = new JScrollPane(textPane2) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1,1);
            }
        };
        scrollPaneText2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(scrollPaneText2, BorderLayout.CENTER);
        panel.add(new JButton("Example"), BorderLayout.NORTH);

        JScrollPane secondScrollPane = new JScrollPane(panel);
        secondScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        secondScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPaneText1, secondScrollPane);
        splitPane.setDividerLocation(100);

        add(splitPane);

        textPane1.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
        textPane2.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
    }

    public static void main(String[] args) {
        ScrollExample example = new ScrollExample();
        JFrame frame = new JFrame("Example");
        frame.setLayout(new BorderLayout());
        frame.add(example, BorderLayout.CENTER);
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setBounds(100, 50, 600, 400);
                frame.setVisible(true);
            }
        });
    }

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

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

    public class WrapEditorKit extends StyledEditorKit {
        protected ViewFactory _factory = new WrapColumnFactory();

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

    public class WrapColumnFactory implements ViewFactory {
        @Override
        public View create(Element elem) {
            switch(elem.getName()) {
                case AbstractDocument.ContentElementName:
                    return new WrapLabelView(elem);
                case AbstractDocument.ParagraphElementName:
                    return new ParagraphView(elem);
                case AbstractDocument.SectionElementName:
                    return new BoxView(elem, View.Y_AXIS);
                case StyleConstants.ComponentElementName:
                    return new ComponentView(elem);
                case StyleConstants.IconElementName:
                    return new IconView(elem);
            }
        return new LabelView(elem);
        }
    }
}

Solution 3

See below for @MadProgrammer's solution

EDIT #4:

Both of the solutions given by @MadProgrammer and @rdonuk work and might be better than mine in general but as the final UI is quite complex and both of the solutions either require a lot of work or won't work in that specific environment I'll stick to my solution (Solution #2).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Code Monkey
  • 78
  • 1
  • 8
  • What is your GUI supposed to look like? – Gilbert Le Blanc Mar 22 '16 at 19:35
  • Take a look at the `Scrollable` interface, which can be used to provide hints to the `JScrollPane` about what it's "preferred" viewport size should be – MadProgrammer Mar 22 '16 at 20:58
  • @GilbertLeBlanc : The example above is an abstraction for simplicity. In the end, the user interface will have many components, which wont fit on the screen (restricted to the screen width, there won't be a horizontal scrollbar on the most outer JScrollPane). Therefore, the outer JScrollPane (bottom of the JSplitter) has been added. The content will be dynamic and depends on the user interaction. But the JTextPane (incl. JScrollPane) should behave as it does in the upper part of the JSplitPane. – Code Monkey Mar 23 '16 at 09:32
  • @GilbertLeBlanc: Part #2: The content of the JTextPane is dynamic as well. All might fit pretty well or it might be a mess. Therefore, it should contain scrollbars as the height of the component will be restricted later on (and the width should fit the screen, which currently is the problem). – Code Monkey Mar 23 '16 at 09:34
  • @MadProgrammer : I've already taken a look at Scrollable and have overriden the `getScrollableTracksViewPortWidth()` to return `false`, as suggested in other questions. That didn't work at all (broke the resizing of the component). As it is part of a component and the JTextPane's conent is dynamic as well , I have no idea how to provide the "preferred" viewport size. Any suggestions? – Code Monkey Mar 23 '16 at 09:41
  • [`Scrollable#getPreferredScrollableViewportSize`](https://docs.oracle.com/javase/7/docs/api/javax/swing/Scrollable.html#getPreferredScrollableViewportSize())? – MadProgrammer Mar 23 '16 at 10:49
  • @MadProgrammer : Wasn't able to make it work with that interface / method. Could you please provide some sample code? Thank you. – Code Monkey Mar 23 '16 at 12:50
  • Can you clarify the requirements you have, cause the question has grown and I'm no longer certain I understand either the problem you what it is you are trying to achieve – MadProgrammer Mar 23 '16 at 21:38
  • @MadProgrammer : I slightly adjusted the requirements above. The example above has been kept simple. In reality the JTextPane is part of a way more complex component. In the example above you'll find 2 JTextPanes (incl. JScrollPane). The JTextPane in the upper half of the JSplitter behaves as it should (Text Wrap when needed) whereas the JTextPane in the lower part of the JSplitter doesn't adjust its width and therefore doesn't add additional scrollbars. What i want to achieve can be done with solution #1 (posted by @rdonuk) and solution #2. Unfortunately I currently can't provide a screenshot – Code Monkey Mar 24 '16 at 09:26
  • If you run the original code and one of the solutions, you'll see how the behaviour of the second JTextPane has changed. in the solution both of the JTextPanes behave exactly the same. If you have a solution to solve this with `Scrollable` I'd be very interested in it, but keep in mind, that the JTextPane is part of a way more complex component and it might be very painful to calculate a "preferred" size (at least i don't see an easy solution yet). – Code Monkey Mar 24 '16 at 09:29
  • Can you tell me why `panel` needs to be in another `JScrollPane`, this seems to be the source of your problem – MadProgrammer Mar 24 '16 at 09:55
  • @MadProgrammer : I've built a dynamic UI which changes its layout and its components based on user interaction (something like a workflow, which might get quite complex and huge - but mainly a BoxLayout (Y_AXIS)). It is obvious that there will be a huge amount of controls on the screen which won't fit the screen size. Therefore, a JScrollPane is added which contains all those components. I currently don't see a better approach / design regarding usability and efficiency for the users. The JTextPane (Text Warp with scrollbars) is just a tiny part of the whole. – Code Monkey Mar 24 '16 at 10:03

3 Answers3

2

Make a JPanel which implements Scrollable and provide a solution for getPreferredScrollableViewportSize

RestrcitedPane

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.Scrollable;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

public class ScrollExample extends JPanel {

    public ScrollExample() {
        super(new BorderLayout());
        JTextPane textPane1 = new JTextPane();
        textPane1.setEditorKit(new WrapEditorKit());

        JTextPane textPane2 = new JTextPane();
        textPane2.setEditorKit(new WrapEditorKit());

        JScrollPane scrollPaneText1 = new JScrollPane(textPane1);
        scrollPaneText1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        JScrollPane scrollPaneText2 = new JScrollPane(textPane2);
        scrollPaneText2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        JPanel panel = new RestrictedPanel();
        panel.setLayout(new BorderLayout());
        panel.add(scrollPaneText2, BorderLayout.CENTER);
        panel.add(new JButton("Example"), BorderLayout.NORTH);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPaneText1, panel);
        splitPane.setDividerLocation(100);

        add(splitPane);

        textPane1.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
        textPane2.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
    }

    public class RestrictedPanel extends JPanel implements Scrollable {

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(600, 200);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return true;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return true;
        }

    }

    public static void main(String[] args) {
        ScrollExample example = new ScrollExample();
        JFrame frame = new JFrame("Example");
        frame.setLayout(new BorderLayout());
        frame.add(example, BorderLayout.CENTER);
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setBounds(100, 50, 600, 400);
                frame.setVisible(true);
            }
        });
    }

    public class WrapLabelView extends LabelView {

        public WrapLabelView(Element elem) {
            super(elem);
        }

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

    public class WrapEditorKit extends StyledEditorKit {

        protected ViewFactory _factory = new WrapColumnFactory();

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

    public class WrapColumnFactory implements ViewFactory {

        @Override
        public View create(Element elem) {
            switch (elem.getName()) {
                case AbstractDocument.ContentElementName:
                    return new WrapLabelView(elem);
                case AbstractDocument.ParagraphElementName:
                    return new ParagraphView(elem);
                case AbstractDocument.SectionElementName:
                    return new BoxView(elem, View.Y_AXIS);
                case StyleConstants.ComponentElementName:
                    return new ComponentView(elem);
                case StyleConstants.IconElementName:
                    return new IconView(elem);
            }
            return new LabelView(elem);
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Works and it is definitely a solution. Nonetheless, it feels somehow "wrong" for me. To explain: The (TextWrap) JTextPane is part of a custom component. Shouldn't that custom component take care of its layout (where the JTextPane is part of) instead of a RestrictedPane which has to be added everywhere, where the custom component is being used? In solution #2 i overwrote the getPreferredSize method (of my (sub)custom component) which lead to the same result. As a swing novice I'd really appreciate if you could explain why your solution is the way to go. – Code Monkey Mar 24 '16 at 11:26
  • The link you've posted before deals with the `setXSize` methods where some comments suggest that custom components may override the getXSize methods (and the comments aren't clear as well). As mentioned in the first comment (char limit reached), I'd really appreciate a short explanation. – Code Monkey Mar 24 '16 at 11:28
  • Unfortunately - after some testing - it doesn't exactly behave as i want. The example you've given works well and is ok in the example i've given. In the real application there won't be a BorderLayout where all the items arrange properly. E.g. if you work with a BoxLayout (Y_AXIS) and add more and more components, you'll end up with an outer scrollbar that doesn't work. I've tried to adjust the returned Dimension without success. Any suggestions? – Code Monkey Mar 24 '16 at 14:59
  • The problem is with the outer `JScrollPane` which is allowing the components to expand to their preferred size without limitation, `BoxLayout` is even worse then `BorderLayout` because it's using the `maximumSize` of the components to make decisions – MadProgrammer Mar 24 '16 at 22:01
1

Please try code below. In a nutshell; I just fixed the scrollPaneText2 size and added a resize listener to your main panel. So if user resize the window, size of the scrollPaneText2 being fixed again according to the new size.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

public class ScrollExample extends JPanel {

    JScrollPane secondScrollPane;
    JScrollPane scrollPaneText2;
    JPanel panel;

    public ScrollExample() {
        super(new BorderLayout());

        addComponentListener(new ResizeListener());

        JTextPane textPane1 = new JTextPane();
        textPane1.setEditorKit(new WrapEditorKit());

        JTextPane textPane2 = new JTextPane();
        textPane2.setEditorKit(new WrapEditorKit());

        JScrollPane scrollPaneText1 = new JScrollPane(textPane1);
        scrollPaneText1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        scrollPaneText2 = new JScrollPane(textPane2);
        scrollPaneText2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneText2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        panel = new JPanel(new BorderLayout());
        panel.add(scrollPaneText2, BorderLayout.WEST);
        panel.add(new JButton("Example"), BorderLayout.NORTH);

        secondScrollPane = new JScrollPane(panel);
        secondScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        secondScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPaneText1, secondScrollPane);
        splitPane.setDividerLocation(100);

        add(splitPane);

        textPane1.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
        textPane2.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
    }

    public void adjustComponents() {
        panel.remove(scrollPaneText2);
        Dimension dimension = new Dimension();
        int nScrollWidth = secondScrollPane.getVerticalScrollBar().getWidth();
        dimension.setSize(secondScrollPane.getVisibleRect().getWidth()-nScrollWidth, scrollPaneText2.getHeight());
        scrollPaneText2.setPreferredSize(dimension);
        scrollPaneText2.setMaximumSize(dimension);

        panel.add(scrollPaneText2);
        repaint();
    }

    public static void main(String[] args) {
        final ScrollExample example = new ScrollExample();
        final JFrame frame = new JFrame("Example");
        frame.setLayout(new BorderLayout());
        frame.add(example, BorderLayout.CENTER);
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setBounds(100, 50, 600, 400);
                frame.setVisible(true);
            }
        });
    }

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

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

    public class WrapEditorKit extends StyledEditorKit {
        protected ViewFactory _factory = new WrapColumnFactory();

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

    public class WrapColumnFactory implements ViewFactory {
        @Override
        public View create(Element elem) {
            switch(elem.getName()) {
                case AbstractDocument.ContentElementName:
                    return new WrapLabelView(elem);
                case AbstractDocument.ParagraphElementName:
                    return new ParagraphView(elem);
                case AbstractDocument.SectionElementName:
                    return new BoxView(elem, View.Y_AXIS);
                case StyleConstants.ComponentElementName:
                    return new ComponentView(elem);
                case StyleConstants.IconElementName:
                    return new IconView(elem);
            }
        return new LabelView(elem);
        }
    }

    public class ResizeListener extends ComponentAdapter {
        @Override
        public void componentResized(ComponentEvent e) {
            ScrollExample scrollExample = (ScrollExample) e.getSource();
            scrollExample.adjustComponents();
        }
    }
}
rdonuk
  • 3,921
  • 21
  • 39
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Mar 22 '16 at 20:57
  • @rdonuk : the solution seems to work, but as @MadProgrammer mentioned I'd prefer a solution without relying on `set(Preferred|Maximum|Minimum)Size` if possible. I'd upvote your answer but unfortunately it isn't possible due to my reputation points (new account). If I can't find a solution without `set(Preferred|Maximum|Minimum)Size` I'll mark this as the answer that solved my problem. – Code Monkey Mar 23 '16 at 09:49
  • Ok than. Please post here if you find an other solution. – rdonuk Mar 23 '16 at 10:04
  • @rdonuk : added your approach to the possible solutions. Furthermore, I've added another solution that works for my case. But I'm still far away from being happy with it. – Code Monkey Mar 23 '16 at 12:51
  • @CodeMonkey please check my second solution. – rdonuk Mar 23 '16 at 13:15
  • @rdonuk : Unfortunately your second solutions doesn't seem to work (copy/pasted your whole source code). the JTextPane on the bottom still doesn't work. No scrollbars / text wrap. Or am I missing something? – Code Monkey Mar 23 '16 at 13:44
  • Sorry, its my bad. I missed something. I deleted second solution. – rdonuk Mar 23 '16 at 14:01
0

See my initial post. There are 3 solutions that would work in the example code.

Code Monkey
  • 78
  • 1
  • 8