1

Any hints on what black magic is needed to make the JTextAreaS shrink properly the way they already properly grows on resize ? I tried every combination of setting preferred size and implementing Scrollable on various things as indicated by different pieces of documentation and other SO answers without seeing any change.

The 'extra' JPanelS are not junk, I just cut out the other stuff in them to produce a more readable test case:

public class WeirdShrinking {
    public static void main(String[] args) {
        JDialog jDialog = new JDialog((JFrame) null, true);

        JPanel content = (JPanel) jDialog.getContentPane();

        JPanel wrapper = new JPanel();
        wrapper.setLayout(new MigLayout("wrap 1", "[grow]", ""));

        for (int i = 0; i < 5; i++) {
            JPanel panel = new JPanel();
            panel.setLayout(new MigLayout("", "[grow]", ""));
            JTextArea description = new JTextArea();
            description.setEditable(false);
            description.setOpaque(false);
            description.setLineWrap(true);
            description.setWrapStyleWord(true);
            description.setFont(new Font("Arial", Font.PLAIN, 12));
            description
                    .setText("Lorem ipsum ddsfolor sit amet, consectetur adipiscing elit. Curabitur viverra vehicula fermentum. Sed ac libero ut massa aliquam ornare. Nunc porttitor interdum turpis, porta viverra purus aliquam quis. In dignissim faucibus nunc, non iaculis sapien. In rutrum eleifend pharetra. Aliquam velit dui, pulvinar ut est ut, sagittis congue ligula. Etiam tincidunt varius consequat.");
            panel.add(description, "growx");
            wrapper.add(panel, "growx, wrap");
        }

        content.add(new JScrollPane(wrapper, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));

        jDialog.setSize(300, 200);
        jDialog.setVisible(true);
    }
}
Torque
  • 3,319
  • 2
  • 27
  • 39
  • Does it work as expected if you use `description.setLineWrap(false);`? – John Nov 06 '14 at 20:19
  • Only in the sense that I expect that to make the lines not wrap, which is what happens, leading scrolling being broken even worse. – Torque Nov 06 '14 at 20:22
  • Ok. Otherwise I would have suggested [that solution](http://stackoverflow.com/questions/2475787/miglayout-jtextarea-is-not-shrinking-when-used-with-linewrap-true)... – John Nov 06 '14 at 20:24
  • Why don't you add the text areas to a scrollpane? – MadProgrammer Nov 06 '14 at 20:32
  • @John, the solutions posted there are amongst the many I tried unsuccessfully :( MadProgrammer, I'm adding the JPanelS to a scrollpane. The JPanelS contain, like I posted, other stuff that I have removed here for brevity, but I can't just permanently remove all other visual elements from the GUI. – Torque Nov 06 '14 at 20:38
  • But why not add the `JTextArea`'s themsevles to a `JScrollPane`, and then add that to sub panels....? – MadProgrammer Nov 06 '14 at 23:48
  • You mean have scrollpanes inside scrollpanes ? I wouldn't want multiple scroll bars, that would look pretty horrible. – Torque Nov 07 '14 at 06:03

1 Answers1

0

After some research and a lot of trial and error, I finally solved my problem by combining two solutions from other threads:

  • Force the container panels to report a minimum size that is unaffected by growing
  • Provide a minimum column width to MigLayout

In order for future people with this issue to be able to solve it too, I'm posting the code to a working version of my above example, with the two critical changes marked:

public class WeirdShrinking {
    public static void main(String[] args) {
        JDialog jDialog = new JDialog((JFrame) null, true);

        JPanel content = (JPanel) jDialog.getContentPane();
        content.setLayout(new MigLayout("wrap 1", "[grow]", ""));

        JPanel wrapper = new JPanel();
        wrapper.setLayout(new MigLayout("wrap 1", "[grow]", ""));

        for (int i = 0; i < 5; i++) {
            // *** Change 1, this obviously should be done in a proper class ***
            JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(1, 1);
                }
            };
            panel.setLayout(new MigLayout("", "[grow]", ""));
            JTextArea description = new JTextArea();
            description.setEditable(false);
            description.setOpaque(false);
            description.setLineWrap(true);
            description.setFont(new Font("Arial", Font.PLAIN, 12));
            description
                    .setText("Lorem ipsum ddsfolor sit amet, consectetur adipiscing elit. Curabitur viverra vehicula fermentum. Sed ac libero ut massa aliquam ornare. Nunc porttitor interdum turpis, porta viverra purus aliquam quis. In dignissim faucibus nunc, non iaculis sapien. In rutrum eleifend pharetra. Aliquam velit dui, pulvinar ut est ut, sagittis congue ligula. Etiam tincidunt varius consequat.");
            // *** Change 2 ***
            panel.add(description, "growx, wmin 10");
            wrapper.add(panel, "growx, wrap");
        }

        content.add(new JScrollPane(wrapper, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER), "growx");

        jDialog.setSize(300, 200);
        jDialog.setVisible(true);
    }
}
Torque
  • 3,319
  • 2
  • 27
  • 39