Solved

Word Wrap Checkboxes and Labels needed

Posted on 1997-11-24
6
448 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:dmag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1231128
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
 
LVL 1

Author Comment

by:dmag
ID: 1231129
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
 
LVL 1

Accepted Solution

by:
blacklion earned 200 total points
ID: 1231130
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dmag
ID: 1231131
Thanks! (note for those using 1.0, just change getSize() to preferredSize() or bounds().)
0
 
LVL 1

Expert Comment

by:blacklion
ID: 1231132
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
 
LVL 2

Expert Comment

by:thor918
ID: 13877258
Hi this works good.exept that every time it is written the old text isn't removed.
any ideas?
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question