/* * This code was written by Anna Louise Tito, May 2010 * * If you use my code please acknowledge my work, and let me know, I am always * interested to see how my work inspires others. * */ package Label; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Label; import java.awt.Panel; import java.util.Vector; public class MultilineLabel extends Panel { private static final int DEFAULT_MAXL = 30; private String stringToSplit; private GridBagConstraints gc; private int maxCharLength; /* * Constructors. The default number of iterations is 30 * I have used a Gridbag layout for the returned panel. * If you want to use a different layout manager you will * need to alter the constructors and then the split() class * where the labels are created and added to the panel. */ public MultilineLabel() { this.stringToSplit = null; this.maxCharLength = DEFAULT_MAXL; //set up panel layout this.setLayout(new GridBagLayout()); this.gc = new GridBagConstraints(); } public MultilineLabel(String inString) { this.stringToSplit = inString; this.maxCharLength = DEFAULT_MAXL; //set up panel layout this.setLayout(new GridBagLayout()); this.gc = new GridBagConstraints(); } public MultilineLabel(String inString, int inMaxLength) { this.stringToSplit = inString; this.maxCharLength = inMaxLength; //set up panel layout this.setLayout(new GridBagLayout()); this.gc = new GridBagConstraints(); } /* * Getters */ public String getDescription() { return stringToSplit; } public GridBagConstraints getGc() { return gc; } public int getItterations() { return this.maxCharLength; } /* * Setters */ public void setString(String desc) { this.stringToSplit = desc; } public void setIterations(int inMaxLength) { this.maxCharLength = inMaxLength; } /* * Public split methods. * If no string was set the function returns null */ public Panel split(String inString) { this.stringToSplit = inString; return split(); } public Panel split() { if(this.stringToSplit != null) { Vector Strings = new Vector(); Strings = splitString(); int i, y; //if you want to use a different layout manager change it here and in the constructors for (i = 0, y = 0; i < Strings.size(); i++, y++) { gc.gridx = 0; gc.gridy = y; this.add(new Label((String) Strings.elementAt(i)), gc); } return this; } return null; } /* * Split methods. * This function iterates through the string to the MaxLength number of characters it * then checks if the character is a space if it is not it counts back till it finds a * space then splits the string adding the split portion to the Vector. * This is a JAVA 2.0 (& J2ME) compatible function so I have not used Generics. If * you are using later libraries try the Swing multiline label or * make sure you add the generic type to the Vector. */ protected Vector splitString() { int i, j, it = 0, ssStart = 0; StringBuffer splitter = new StringBuffer(stringToSplit); Vector splitString = new Vector(); for (i = 0; i < stringToSplit.length(); i++) { if (it == maxCharLength) { if (splitter.charAt(i) == ' ') { splitString.add((splitter.substring(ssStart, i)).trim()); ssStart = i; } else { StringBuffer subString =new StringBuffer((splitter.substring(ssStart, i)).trim()); for (j = (subString.length() - 1); j >= 0; j--) { if (subString.charAt(j) == ' ') { splitString.add((subString.substring(0, j)).trim()); i = i - (i - (ssStart + j)) + 1; ssStart = i; break; } } } it = 1; } else { it++; } /*This checks if it is the last character, if it is it adds the final portion * of the string to the Vector. */ if (i == (stringToSplit.length() - 1)) { splitString.add((splitter.substring(ssStart, i + 1)).trim()); ssStart = i; } } return splitString; } }