views:

1968

answers:

2

Hai,

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....

+1  A: 

Decreasing the size of a JEditorPane in a JScrollPane and then reducing it, is not possible. You may want to use a JTextArea instead.

luiscubal
+2  A: 

Actually it is possible, luiscubal. Here is how,

To the JScrollPane add a ComponentListener for resize events

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
}

Look luiscubal it is possible. Don't be so quick to announce things in Java as not possible. The swing api is quiet flexible and can do a lot of the work for you. However, if you use JComponents in ways they weren't made to be used you will end up with problems and have two options.

  1. subclass subclass subclass basically create your own component.
  2. find a work around, like the above solution.