Package sim.util.gui

Class WordWrap

java.lang.Object
sim.util.gui.WordWrap
All Implemented Interfaces:
Serializable

public class WordWrap extends Object implements Serializable
WordWrap is a simple word-wrapping class which provides word-wrap either to columns of raw text; or to some number of pixels (given a font). It's not terribly efficient but works reasonably well. Tabs are considered to be the same length as spaces. The input is the string to wrap, and the output is the same string with returns inserted in the appropriate location.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String[]
    A useful auxillary method: once you're word-wrapped your text, you can use this to break it into multiple strings at the \n position.
    static String
    toHTML(String text)
    A useful auxillary method: once you've word-wrapped your text, you can use this to convert it into 'HTML' style, where < is converted into &lt;, & is converted into &amp;, and \n or \r are converted into <br>.
    static String
    wrap(String string, int numColumns)
    Wraps a string to a given number of columns.
    static String
    wrap(String string, int numPixels, FontMetrics metrics)
    Wraps a string to a given number of pixels in width, given a font whose metrics are provided as well.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WordWrap

      public WordWrap()
  • Method Details

    • wrap

      public static String wrap(String string, int numColumns)
      Wraps a string to a given number of columns.
    • wrap

      public static String wrap(String string, int numPixels, FontMetrics metrics)
      Wraps a string to a given number of pixels in width, given a font whose metrics are provided as well.
    • split

      public static String[] split(String str)
      A useful auxillary method: once you're word-wrapped your text, you can use this to break it into multiple strings at the \n position.
    • toHTML

      public static String toHTML(String text)
      A useful auxillary method: once you've word-wrapped your text, you can use this to convert it into 'HTML' style, where < is converted into &lt;, & is converted into &amp;, and \n or \r are converted into <br>.

      You can use this to make multi-line buttons and multi-line menus like this:

      
              String myText = "Here is the big text that we want to have word-wrapped";
              int myNumberOfPixels = 50; // our word-wrap pixel length
              JButton button = new JButton();
              String wrappedText = sim.util.WordWrap.wrap(myText, myNumberOfPixels, button.getFontMetrics(button.getFont()));
              button.setText("<html>" + sim.util.WordWrap.toHTML(wrappedText) + "</html>");