Solved

I want a multi-line label!

Posted on 1998-06-21
4
260 Views
Last Modified: 2008-02-01
Hello!
I want a multi-line label.
I have tried using awt label, swing label and many more but none works.
\n, \u0013 and \u0010 gives a little box and \u0013\u0010 gives me two.
I'm getting crazy, this should be really easy.

Of course, the text must break automatically when it reaches the end of the label.

Thanks!
0
Comment
Question by:martinag
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 50 total points
ID: 1223463
here is a multi line label with a wrapping feature too that wraps the text when label is resized.

       WrappingLabel.java--->


       import java.awt.*;
       import java.util.*;

       public class WrappingLabel extends Component {
       private String m_label = "";
       public WrappingLabel(String label)
       {
       this.m_label = label;
       }
       public WrappingLabel ()
       {
       }
       public synchronized void setText(String label)
       {
       this.m_label = label;
       invalidate();
       repaint();
       }
       private Dimension computeSize()
       {
       Font fn = getFont();
       if (fn == null)
       return new Dimension(50, 20);
       FontMetrics fm = getFontMetrics(fn);
       int height = fm.getHeight();
       int descent = fm.getDescent();

       StringTokenizer strtok = new StringTokenizer(m_label, "\n");

       // find number of lines and maximum line width
       int nlines = 0;
       int maxwidth = 10;
       while (strtok.hasMoreTokens()) {
       String line = strtok.nextToken();
       nlines ++;
       int cwidth = 0;
       if (line != null)
       cwidth = fm.stringWidth(line);
       if (maxwidth < cwidth)
       maxwidth = cwidth;
       }
       if (nlines == 0) nlines = 1;
       return new Dimension(maxwidth + 6, nlines * fm.getHeight() + 4);
       }
       public void paint(Graphics g)
       {
       FontMetrics fm = getFontMetrics(getFont());
       int height = fm.getHeight();
       int y = 2 + height;
       int x = 3;
       Dimension currSize = getSize();
       if (currSize.width == 0 || currSize.height == 0)
       return;
       StringTokenizer strtok = new StringTokenizer(m_label, "\n\r ", true);
       String accumilated = "";
       int accumilatedWidth = 0;
       int spaceWidth = fm.stringWidth(" ");
       while (strtok.hasMoreTokens()) {
       String currWord = strtok.nextToken();
       String delimiter = " ";
       if (strtok.hasMoreTokens())
       delimiter = strtok.nextToken();

       int currWordWidth  = fm.stringWidth(
       (accumilated.equals("") ? "" : " ") + currWord);
       accumilatedWidth = fm.stringWidth(accumilated);
       if ( accumilatedWidth + currWordWidth > currSize.width) {
       if (accumilated.length() != 0){
       g.drawString(accumilated, x, y);
       y += height;
       }
       accumilated = currWord;
       accumilatedWidth = fm.stringWidth(currWord);
       } else {
       accumilated += " " + currWord;
       accumilatedWidth = fm.stringWidth(accumilated);
       }
       if (delimiter.equals("\n") || delimiter.equals("\r")) {
       g.drawString(accumilated, x, y);
       y += height;
       accumilated = "";
       accumilatedWidth = 0;
       }
       }
       if (accumilated.length() != 0) {
       g.drawString(accumilated, x, y);
       y += height;
       }
       }
       public synchronized String getText()
       {
       return m_label;
       }

       public Dimension getPreferredSize()
       {
       return getMinimumSize();
       }

       public Dimension getMinimumSize()
       {
       return computeSize();
       }

       public Dimension getMaximumSize()
       {
       return getMinimumSize();
       }
       }

       **************************************************************
       Here is a sample test program that tests it

       testlabel.java--->


       import java.awt.*;
       public class testlabel extends Frame {
       public testlabel ()
       {
       super("Testing wrappinglabel");
       setLayout(new GridLayout(2,2));
       add (new WrappingLabel("hello world!"));
       add (new WrappingLabel("hello how are you world!\nhi world\nhello"));
       add (new WrappingLabel("hello world!\nhi world"));
       add (new WrappingLabel("hello how are you world!\nhi world\nhello"));
       pack();

       }
       public static void main(String args[])
       {
       new testlabel().setVisible(true);
       }
       }
0
 
LVL 4

Author Comment

by:martinag
ID: 1223464
Thanks evijay, it works almost perfect.
There is just one problem, the foreground is set to black (setForeground(Color.black)) but it is actually light grey, which is very hard to read.
Could you fix it?
0
 
LVL 4

Expert Comment

by:evijay
ID: 1223465
Hi martinag,

Here is the modified program -->
(I have added two lines in the paint method)


WrappingLabel.java --->





              import java.awt.*;
              import java.util.*;

              public class WrappingLabel extends Component {
              private String m_label = "";
              public WrappingLabel(String label)
              {
              this.m_label = label;
              }
              public WrappingLabel ()
              {
              }
              public synchronized void setText(String label)
              {
              this.m_label = label;
              invalidate();
              repaint();
              }
              private Dimension computeSize()
              {
              Font fn = getFont();
              if (fn == null)
              return new Dimension(50, 20);
              FontMetrics fm = getFontMetrics(fn);
              int height = fm.getHeight();
              int descent = fm.getDescent();

              StringTokenizer strtok = new StringTokenizer(m_label, "\n");

              // find number of lines and maximum line width
              int nlines = 0;
              int maxwidth = 10;
              while (strtok.hasMoreTokens()) {
              String line = strtok.nextToken();
              nlines ++;
              int cwidth = 0;
              if (line != null)
              cwidth = fm.stringWidth(line);
              if (maxwidth < cwidth)
              maxwidth = cwidth;
              }
              if (nlines == 0) nlines = 1;
              return new Dimension(maxwidth + 6, nlines * fm.getHeight() + 4);
              }
              public void paint(Graphics g)
              {
            if (getForeground() != null)
                  g.setColor(getForeground());
              FontMetrics fm = getFontMetrics(getFont());
              int height = fm.getHeight();
              int y = 2 + height;
              int x = 3;
              Dimension currSize = getSize();
              if (currSize.width == 0 || currSize.height == 0)
              return;
              StringTokenizer strtok = new StringTokenizer(m_label, "\n\r ", true);
              String accumilated = "";
              int accumilatedWidth = 0;
              int spaceWidth = fm.stringWidth(" ");
              while (strtok.hasMoreTokens()) {
              String currWord = strtok.nextToken();
              String delimiter = " ";
              if (strtok.hasMoreTokens())
              delimiter = strtok.nextToken();

              int currWordWidth  = fm.stringWidth(
              (accumilated.equals("") ? "" : " ") + currWord);
              accumilatedWidth = fm.stringWidth(accumilated);
              if ( accumilatedWidth + currWordWidth > currSize.width) {
              if (accumilated.length() != 0){
              g.drawString(accumilated, x, y);
              y += height;
              }
              accumilated = currWord;
              accumilatedWidth = fm.stringWidth(currWord);
              } else {
              accumilated += " " + currWord;
              accumilatedWidth = fm.stringWidth(accumilated);
              }
              if (delimiter.equals("\n") || delimiter.equals("\r")) {
              g.drawString(accumilated, x, y);
              y += height;
              accumilated = "";
              accumilatedWidth = 0;
              }
              }
              if (accumilated.length() != 0) {
              g.drawString(accumilated, x, y);
              y += height;
              }
              }
              public synchronized String getText()
              {
              return m_label;
              }

              public Dimension getPreferredSize()
              {
              return getMinimumSize();
              }

              public Dimension getMinimumSize()
              {
              return computeSize();
              }

              public Dimension getMaximumSize()
              {
              return getMinimumSize();
              }
              }



Program to test the label ------>
testlabel.java------->



     import java.awt.*;
              public class testlabel extends Frame {
              public testlabel ()
              {
              super("Testing wrappinglabel");
            WrappingLabel w;
              setLayout(new GridLayout(2,2));
              add (w = new WrappingLabel("hello world!"));
            w.setForeground(Color.gray);
              add (w = new WrappingLabel("hello how are you world!\nhi world\nhello"));
            w.setForeground(Color.red);
              add (w = new WrappingLabel("hello world!\nhi world"));
            w.setForeground(Color.green);
              add (w = new WrappingLabel("hello how are you world!\nhi world\nhello"));
            w.setForeground(Color.blue);
              pack();

              }
              public static void main(String args[])
              {
              new testlabel().setVisible(true);
              }
              }


0
 
LVL 4

Author Comment

by:martinag
ID: 1223466
Excellent!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
countHi challenge 25 84
recursion example 16 68
Java Server Faces parameter pass? 6 20
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)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

708 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

18 Experts available now in Live!

Get 1:1 Help Now