Results 1 to 2 of 2
  1. #1
    edsporty is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default JList item text does not truncate when it's HTML

    When the String to display in the JList is HTML, the item in the list is not truncated when the String is longer than the cell width of the list.

    These are the strings used in the list
    Java Code:
    if (hasAnswer() == false){
         return "<html><font color=\"gray\"> Unanswered </font>" + getQuestionTitle() + "</html>";
        		return getQuestionTitle();
    }
    else {
         return "<html><font color=\"green\"> Answered &nbsp;&nbsp;&nbsp;&nbsp;</font>    " + getQuestionTitle() +	"<font color=\"green\">&nbsp;&nbsp; Answer: </font>" +  getQuestionAnswer() + "</html>";
        		return getQuestionTitle() + "   " +  getQuestionAnswer();
        	}
    Instead of being on 1 line, if the getQuestionTitle() or getQuestionAnswer() are too long this is the result:
    Unanswered
    This is an example. This is an example. This is an example. This is an example.
    and
    Answered
    This is an example. This is an example. This is an example. This is an example.
    Answer:
    This is an example. This is an example. This is an example. This is an example.


    But I want them on 1 line with "..." shown at the end, not 2 or 4 lines.
    When I take out the HTML it truncates as expected, but I need the different coloured text at the beginning.



    Thanks

  2. #2
    edsporty is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default fixed it myself with workaround

    I used string.substring(fromIndex, toIndex); to truncate it myself

    eg:
    Java Code:
    String q = getQuestionTitle();
    if (q.length() > 30){
         String shortQ = q.substring(0, 30)+"...";
         return "<html><font color=\"gray\"> Unanswered </font>" + shortQ +"</html>";
    }
    else {
         return "<html><font color=\"gray\"> Unanswered </font>" + q + "</html>";
    }

Similar Threads

  1. Add JList item from resultset
    By mine0926 in forum NetBeans
    Replies: 1
    Last Post: 06-17-2010, 09:47 AM
  2. Replies: 3
    Last Post: 11-11-2009, 09:00 PM
  3. Replies: 9
    Last Post: 07-21-2009, 02:41 AM
  4. How do I display a JList with icons and text?
    By aneesahamedaa in forum AWT / Swing
    Replies: 2
    Last Post: 09-04-2008, 08:49 AM
  5. Replies: 0
    Last Post: 05-26-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •