Solved

Word Wrap Checkboxes and Labels needed

Posted on 1997-11-24
6
414 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
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now