Solved

Word Wrap Checkboxes and Labels needed

Posted on 1997-11-24
6
433 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
table example 4 32
Windows 10 IE Certificate Issue 10 51
hibernate example issues from command prompt 10 50
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…

830 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