Force JEditorPane within JScrollPane to shrink + re-wrap

谁说我不能喝 提交于 2019-12-13 02:14:24

问题


Having an issue with a JScrollPane expanding its child JEditorPane just fine but forcing horizontal scroll bars when resizing it down again (instead of forcing the JEditorPane to recalculate wrapping).

The basic flow of code is as follows:

JFrame f = new JFrame();
JEditorPane jep = new JEditorPane();
JScrollPane jsp = new JScrollPane(jep);
f.add(jsp);

回答1:


It's a hack, but the best way I could find (without using ugly ScrollPaneManagers) was to implement a ComponentListener on the JScrollPane to resize the child component whenever it was resized.

jsp.addComponentListener(new ComponentListener() {
    @Override
    public void componentShown(ComponentEvent e) {}

    @Override
    public void componentResized(ComponentEvent e) {
        Dimension jspSize = ((JScrollPane)e.getComponent()).getViewport().getSize();
        jep.setBounds(0, 0, jspSize.width, jspSize.height);
    }

    @Override
    public void componentMoved(ComponentEvent e) {}

    @Override
    public void componentHidden(ComponentEvent e) {}
});


来源:https://stackoverflow.com/questions/13968271/force-jeditorpane-within-jscrollpane-to-shrink-re-wrap

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