I looked at this problem for hours.

Closest I can come is the javax.swing.Scrollable interface.  Source from 
1.3 download.

Here is the method and the comment from Scrollable.
     /**
      * Return true if a viewport should always force the width of this
      * Scrollable to match the width of the viewport.  For example a noraml
      * text view that supported line wrapping would return true here, since it
      * would be undesirable for wrapped lines to disappear beyond the right
      * edge of the viewport.  Note that returning true for a Scrollable
      * whose ancestor is a JScrollPane effectively disables horizontal
      * scrolling.
      * <p>
      * Scrolling containers, like JViewport, will use this method each
      * time they are validated.
      *
      * @return True if a viewport should force the Scrollables width to 
match its own.
      */
     boolean getScrollableTracksViewportWidth();


Here's the implementation in JEditorPane.

     // --- Scrollable  ----------------------------------------
     /**
      * Returns true if a viewport should always force the width of this
      * <code>Scrollable</code> to match the width of the viewport.
      *
      * @return true if a viewport should force the Scrollables width to
      * match its own, false otherwise
      */
     public boolean getScrollableTracksViewportWidth() {
         if (getParent() instanceof JViewport) {
             JViewport port = (JViewport)getParent();
             TextUI ui = getUI();
             int w = port.getWidth();
             Dimension min = ui.getMinimumSize(this);
             Dimension max = ui.getMaximumSize(this);
             if ((w >= min.width) && (w <= max.width)) {
                 return true;
             }
         }
         return false;
     }

Notice in the above code that
1) The name of the method suggests that a WIDTH (an int or possibly a 
float) will be returned not a boolean.

2) The editor must be a CHILD of a JViewPort (in reality a JScrollPane) not 
a DESCENDENT of a JViewPort if any wrapping is to be done.  My app needs 
multiple editors separated by other non-editable components in a vertically 
scrolling pane.  I do not want a JViewPort controlling each editor.  I want 
the contents of each editor to be displayed in its entirety, growing and 
shrinking as desired within an encompassing JScrollPane.  This is a 
reasonable thing to do.  And JTextArea with wrapping enabled does this just 
fine.

3)  The check for trueness relies upon a call to the JViewPort .getWidth() 
method.  What does the width of a component have to do with whether or not 
to do a wrap!  It has to do with WHERE to do the wrap if DESIRED.  Also 
Someone please explain to me when the predicate will return false.

4) The comments in the interface and the implementation are not true, this 
function does not work as advertised but only in limited an undocumented 
circumstances.

5) This, IMHO, should be a boolean that can be set true or false not 
computed and the actual width that is used by the BasicTextUI to trigger a 
wrap should be keyed to the preferred size of the parent, ALONG A 
PARTICULAR AXIS, whatever it may be.

I extended JTextPane and implemented this method to simply return true.  It 
did not not have any effect.  Wrapping did not occur when typing in text or 
when inserting text using paste when the parent was anything other than a 
JScrollable.

I then implemented    Dimension getPreferredScrollableViewportSize(); from 
Scrollable and tried to give it a value.  But you have to return a 
Dimension which is both x and y axis.  I tried JViewPort.getPreferredSize() 
and got an infinite loop so I then tried everything else.  I only want to 
tell it the width to use not the height.  Nothing worked and with some 
rather strange and unpredictable results during testing.  And yes I tried a 
JViewPort inside a JPane.

JEditorPane should

NOT require a JViewport as the parent,
HAVE control over wrapping made available much like JTextArea.

Tracing when a JEditorPane does wrapping is a nightmare come true.

My other 10 problems with JTextPane can wait till later.

It truly staggers the imagination that after 20+ years we still don't have 
a styled editor that works as advertised.

If anybody has any ideas or references please let me know,

Pete


At 06:47 AM 7/13/2001 -0600, you wrote:
>Hi..
>Is there any direct apis to wordwrap in JTextPane?.
>Regards
>prakash
>_______________________________________________
>Advanced-swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/advanced-swing


_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to