Word Wrap Checkboxes and Labels needed

I have an app with long lables and checkbox captions. The standard AWT doesn't allow multiple lines in it's lables (even if it did, I'd want it to wrap automagically).

Is there any way to get my widgets to "Do the Right Thing"? (i.e. automatically grow the label verticaly and wrap the text.) Java 1.0 solutions preferred, and it must work on labels, checkboxes, and pull-down listboxes.
LVL 1
dmagAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jpk041897Commented:
Unless someone knows of a pre made solution, you will have to subclass the controls.

The process is fairly easy for labels and check boxes, not so much for listboxes.


Let me know if (and when) you become interested in such a solution and I could post some code and suggestions using 1.02 (Its the oldest version I have).
0
dmagAuthor Commented:
I understand that the default labels/checkboxes don't do wrapping. And that I could write one myself given enough time and energy. But I have to think that SOMEONE has already encountered this problem and written a solution.

For 200 points,  I don't want 'suggestions'-- I want working code. If you wrote something (or have the time and energy to write something) by all means, post it.

All it has to do is automatically wrap text in labels and checkboxes. Java 1.02 is fine.
0
blacklionCommented:
Here is my implementation of multiline label using Java 1.1. The only thing us have to do is to convert method names to Java 1.0.

/**
 * Copyright (c) 1997 Alekseyev Oleg (Black Lion).
 */
import java.awt.*;
import java.util.*;

/**
 * Label with support of wrapping lines.
 */
public final class MultilineLabel extends Canvas
{
/////////////////////////////////////////////////////////
      public MultilineLabel()
            {
                  this("");
            }
      public MultilineLabel (String label)
            {
                  this(label,true);
            }
      public MultilineLabel (String label, boolean transparent)
            {
                  this.transparent = transparent;
                  setText(label);
            }
/////////////////////////////////////////////////////////
      private String label;
      public final void setText (String label)
            {
                  this.label = label.trim();
                  lines.removeAllElements();
                  ys   .removeAllElements();
                  repaint();
            }
      public final String getText()
        {
          return label;
    }
/////////////////////////////////////////////////////////
      private boolean transparent;
  public final void setTransparent (boolean transparent)
        {
          this.transparent = transparent;
      repaint();
    }
  public final boolean getTransparent()
        {
          return transparent;
    }
/////////////////////////////////////////////////////////
      private Vector lines = new Vector();
      private Vector ys    = new Vector();
      public final void paint (Graphics g)
            {
                  String line;
                  int    y;
                  if (lines.size() == 0 && label != null && !label.equals(""))
                        {
                              // calculating positions
                              int width = getSize().width;

                              FontMetrics fm = g.getFontMetrics();
                              int fontheight = fm.getHeight();

                              line = "";
                              y    = fm.getAscent();

                              StringTokenizer st = new StringTokenizer(label," \n\r",true);
                              while (st.hasMoreTokens())
                                    {
                                          String t = st.nextToken();
                                          boolean newlinechar = t.equals("\n") || t.equals("\r");
                                          if (newlinechar || fm.stringWidth (line+t) > width)
                                                {
                                                      lines.addElement(line);
                                                      ys   .addElement(new Integer(y));
                                                      y += fontheight;
                                                      line = !newlinechar ? t : "";
                                                }
                                           else
                                                line += t;
                                    }
                              lines.addElement(line);
                              ys   .addElement(new Integer(y));
                        }
                  // paint with vertical centering
                  y = ((Integer)ys.lastElement()).intValue();
                  int dy = (getSize().height - (y+g.getFontMetrics().getDescent()))/2;
                  Enumeration e2 = ys.elements();
                  for (Enumeration e1 = lines.elements(); e1.hasMoreElements();)
                        {
                              line  =  (String) e1.nextElement();
                              y     = ((Integer)e2.nextElement()).intValue();
                              g.drawString (line, 0, y+dy);
                        }
            }
      public final void update (Graphics g)
            {
                  if (!transparent)
                        super.update(g);
                   else
                        paint(g);
            }
/////////////////////////////////////////////////////////
      public final Dimension getMinimumSize()
            {
                  FontMetrics fm = getGraphics().getFontMetrics();

                  int width = 0;
                  StringTokenizer st = new StringTokenizer(label," \n");
                  while (st.hasMoreTokens())
                        {
                              int w = fm.stringWidth (st.nextToken());
                              if (w > width)
                                    width = w;
                        }

                  return new Dimension (width, getHeightByWidth(width,fm));
            }
      public final Dimension getPreferredSize()
            {
                  FontMetrics fm = getGraphics().getFontMetrics();
                  String lab = label.replace('\n',' ');
                  int sw = fm.stringWidth (lab);
                  int threshold      = getToolkit().getScreenSize().width/3;
                  if (sw > threshold)
                        return new Dimension (threshold, getHeightByWidth(threshold,fm));
                  return new Dimension (sw, getHeightByWidth(sw,fm));
            }
      private final int getHeightByWidth (int width, FontMetrics fm)
            {
                  // calculating height in "y"
                  int    fontheight = fm.getHeight();
                  String line = "";
                  int    y    = fm.getAscent();

                  StringTokenizer st = new StringTokenizer(label," \n\r",true);
                  while (st.hasMoreTokens())
                        {
                              String t = st.nextToken();
                              boolean newlinechar = t.equals("\n") || t.equals("\r");
                              if (newlinechar || fm.stringWidth (line+t) > width)
                                    {
                                          y += fontheight;
                                          line = !newlinechar ? t : "";
                                    }
                               else
                                    line += t;
                        }

                  return y+fm.getDescent();
            }
/////////////////////////////////////////////////////////
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

dmagAuthor Commented:
Thanks! (note for those using 1.0, just change getSize() to preferredSize() or bounds().)
0
blacklionCommented:
Also I saw implementaion using just multiple AWT labels. But that code still have to wrap lines, but does not have to draw them.
0
thor918Commented:
Hi this works good.exept that every time it is written the old text isn't removed.
any ideas?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.