5

I am implementing a Comment box facility in my application which user can resize using mouse. This comment box contains a scrollpane which instead contains a JEditorPane in which user can insert comment. I have added the editor pane inside a scroll pane for the following reason:

auto scolling of jeditorpane

When the user resizes the comment box, I am setting the desired size for JScrollPane and the JEditorPane. When the user is increasing the size of the comment box, the size of these components are increasing as desired but when the size of the comment box is decreased, the size of the JEditorPane does not decrease even after setting the size. This leads to the scrollbars inside the scrollpane.

I tried using setPreferrredSize, setSize, setMaximumSize for JEditorPane. Still the size of the editor pane is not reducing. I tried calling revalidate() or updateUI() after the setting of size but no use.

I am using Java 1.4.2.

Please provide me some insight....

4

3 回答 3

9

我意识到这早已得到回答,但为了将来参考,您需要做的就是覆盖getScrollableTracksViewportWidth()始终返回 true,例如。

JEditorPane pane = new JEditorPane() {
    public boolean getScrollableTracksViewportWidth() {
        return true;
    }
};
panel.add(new JScrollPane(pane));
于 2011-10-25T12:59:19.840 回答
4

实际上这是可能的,luiscubal。这里是如何,

向 JScrollPane 添加一个用于调整大小事件的 ComponentListener

public static void main(String...args) {
    //our test frame
    JFrame frame = new JFrame("JEditorPane inside JScrollPane resizing");
    frame.setLayout(new BorderLayout());

    //our editing pane
    final JEditorPane editor = new JEditorPane();

    //our simple scroll pane
    final JScrollPane scroller = new JScrollPane(editor);

    //NOTE: this is the magic that is kind of a workaround
            // you can also implement your own type of JScrollPane
            // using the JScrollBar and a JViewport which is the 
            // preferred method of doing something like this the 
            // other option is to create a JEditorPane subclass that
            // implements the Scrollable interface.
    scroller.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            editor.setSize(new Dimension(
                               scroller.getWidth()-20, 
                               scroller.getHeight()-20));
        }
    });

    //just use up the entire frame area.
    frame.add(scroller, BorderLayout.CENTER);

    //quick and dirty close event handler
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(320, 240); //something not too big
    frame.setLocationRelativeTo(null); //centers window on screen
    frame.setVisible(true); // normally done in a SwingUtilities.invokeLater
}

看起来很可能。不要这么快宣布 Java 中的事情是不可能的。swing api 非常灵活,可以为您完成很多工作。但是,如果您以不适合使用的方式使用 JComponents,您最终会遇到问题并且有两种选择。

  1. subclass subclass subclass 基本上是创建你自己的组件。
  2. 找到解决方法,如上述解决方案。
于 2009-10-06T11:57:19.630 回答
-1

减小 JScrollPane 中的 JEditorPane 的大小然后再减小它是不可能的。您可能想改用 JTextArea。

于 2008-11-11T18:32:03.753 回答