Line wrap in a JTextArea causes JScrollPane to missbehave with MiGLayout

佐手、 提交于 2019-12-21 02:33:06

问题


I am having trouble with the same thing as this guy:

MigLayout JTextArea is not shrinking when used with linewrap=true

and I used the solution described in one of the answers; to set the minimum size explicitly. This works fine if one places the JPanel which contains the JTextArea directly in a JFrame, and then resizes the window.

However, when placing the panel which contains the JTextArea inside a JScrollPane, the same problem occurs again. Why is this, and how can one fix it?

Cheers

EDIT: An example

public class MiGTest2 extends JFrame{   
public MiGTest2(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
    JTextArea textArea  = new JTextArea();
    textArea.setLineWrap(true);
    panel.add(textArea, "wmin 10");
    //panel.add(new JTextField());
    JScrollPane scrollPane = new JScrollPane(panel);
    //add(panel);
    add(scrollPane);
    pack();
}
public static void main(String[] args){
    new MiGTest2().setVisible(true);
}
}

If you uncomment //add(panel);, and comment add(scrollPane);, shrinking the window size will also shrink the JTextArea. That is, it does not work with a JScrollPane. Also note how the layout manager seems to flip out and starts "shaking" all its contents when shrinking the size of the window after first enlarging it


回答1:


I had a very similar problem and following the answer in the mentioned question did not help me either. However, it did provide a valuable idea -- the problem is in the width of the JTextArea with wrap enabled.

What worked for me was setting both minimum and preferred width at the component level using command width. For example, width 10:500:.




回答2:


I've had similar problems with JTextAreas and wrapping when used with JScrollPanes.

A solution that worked for me was to create a custom panel that implements the Scrollable interface and overrides the getScrollableTracksViewportWidth() method to return true. This forces causes the scroll pane to only scroll vertically and lets line wrapping in the JTextArea work as expected.

/**
 * A panel that, when placed in a {@link JScrollPane}, only scrolls vertically and resizes horizontally as needed.
 */
public class OnlyVerticalScrollPanel extends JPanel implements Scrollable
{
    public OnlyVerticalScrollPanel()
    {
        this(new GridLayout(0, 1));
    }

    public OnlyVerticalScrollPanel(LayoutManager lm)
    {
        super(lm);
    }

    public OnlyVerticalScrollPanel(Component comp)
    {
        this();
        add(comp);
    }

    @Override
    public Dimension getPreferredScrollableViewportSize()
    {
        return(getPreferredSize());
    }

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

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

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

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

and MigTest2 becomes:

public class MiGTest2 extends JFrame
{   
    public MiGTest2()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
        JTextArea textArea  = new JTextArea();
        textArea.setLineWrap(true);
        panel.add(textArea, "wmin 10");
        //panel.add(new JTextField());

        //Wrap panel with the OnlyVerticalScrollPane to prevent horizontal scrolling
        JScrollPane scrollPane = new JScrollPane(new OnlyVerticalScrollPanel(panel));
        //add(panel);
        add(scrollPane);
        pack();
    }

    public static void main(String[] args)
    {
        new MiGTest2().setVisible(true);
    }
}



回答3:


Normally, you would put the JTextArea into your JScrollPane. Like this:

JTextArea area = new JTextArea();
JScrollPane scroll = new JScrollPane(area);
JPanel panel = new JPanel();
panel.add(scroll);



回答4:


Not really sure what you're trying to achieve here, try running this and see if it fits your need?


public class MiGTest2 extends JFrame {
    public MiGTest2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));

        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
        panel.add(new JScrollPane(textArea), "wmin 10, grow, push");

        setLayout(new MigLayout("fill"));

        JScrollPane scrollPane = new JScrollPane(panel);
        add(scrollPane, "grow, push");

        pack();
    }

    public static void main(String[] args) {
        new MiGTest2().setVisible(true);
    }
}



来源:https://stackoverflow.com/questions/6023145/line-wrap-in-a-jtextarea-causes-jscrollpane-to-missbehave-with-miglayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!