Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiine label  RSS feed

 
Lakshmi Anantharaman
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following piece of code to change a long text
to different lines of code and attach it to a Label . But but Label displays the text with the html tags . What is going wrong ?
I am able to import javax.swing.taxt.html.* just to check whether the version of swing I am using supports Html !
private String formatLabel(String labelText){

String formattedLabel = new String(" <html>" );
int beginIndex = 0;
if(labelText.length() > 30 )
{
for (int x=30;x<labelText.length();x+=30){<br /> formattedLabel =
forrmattedLabel+labelText.substring(beginIndex,x)
+"
";
beginIndex = x;
}
labelText= formattedLabel+"</html>"; }
return labelText;

}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I am missing something but I didn't see any HTML tags being applied. What does the returned string look like from that method?
 
anandh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean to say that your labelText string is an HTML string?
 
Premila
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can have multiline as follows:
String s="A long sentence A long sentence A long sentence A long sentence A long sentence A long sentence";
JLabel label=new JLabel();
label.setText("<html><body>"+s+"</body></html>");

With the above code, u woun't see any html tags and also according to the width of ur label the text get wrapped as
in the case of a textpane/editorpane

Originally posted by Laksh Anan:
I have the following piece of code to change a long text
to different lines of code and attach it to a Label . But but Label displays the text with the html tags . What is going wrong ?
I am able to import javax.swing.taxt.html.* just to check whether the version of swing I am using supports Html !
private String formatLabel(String labelText){

String formattedLabel = new String(" <html>" );
int beginIndex = 0;
if(labelText.length() > 30 )
{
for (int x=30;x<labelText.length();x+=30){<br /> formattedLabel =
forrmattedLabel+labelText.substring(beginIndex,x)
+"
";
beginIndex = x;
}
labelText= formattedLabel+"</html>"; }
return labelText;

}

 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method is not working for me. It just ends the label with ... It is not wrapping text.
 
Mike Boni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to put <HTML> at the beginning of the label, and then
within the text to go to the next line.
This is the method I use (in my subtype of JLabel):
/**
* Set the Text of this label.
* This method has been enhanced to recognize embedded "\n" characters. If a "\n" is
* found within the text, the entire text is converted to an HTML formatted label.
* This conversion is skipped if the label text is already HTML formatted. Color and Font
* are preserved, using additional HTML formatting codes.
*
* @param text The text of the label
*/
public void setText(String text)
{
if (text == null | | text.indexOf("\n") == -1 | | text.substring(0,6).equalsIgnoreCase("<HTML>"))
{
super.setText(text);
return;
}
rawText = text;
StringTokenizer parser = new StringTokenizer(text, "\n", false);
StringBuffer buffer = new StringBuffer(text.length() + 80);
buffer.append("<HTML>");
buffer.append(htmlFont(this.getFont(), this.getForeground()));
buffer.append(parser.nextToken());
while (parser.hasMoreTokens())
{
buffer.append("
");
buffer.append(parser.nextToken());
}
buffer.append(htmlEndFont(this.getFont()));
buffer.append("</HTML>");
super.setText(buffer.toString());
//System.out.println(buffer.toString()); // DEBUG
} // setText()

 
Mike Boni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ack, my HTML tags within the code dissapeared!
you need to start with {HTML}, and embed a {P} within the code.
(substitute a bracket for a brace)
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to do it with HTML. A prior post says it can be done with String a = "" + someString + ""; That is what is not working for me.
 
Premila
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry in my last reply, there was a mistake. please note the correction.
String s="A long sentence A long sentence A long sentence A long sentence A long sentence A long sentence";
String x="{html}{body}"; // substitute < for { and > for }
String y="{/html}{/body}";
JLabel label=new JLabel();
label.setText(x+s+y);

With the above code, u woun't see any html tags and also according to the width of ur label the text get wrapped as
in the case of a textpane/editorpane
Originally posted by Premila:
U can have multiline as follows:
String s="A long sentence A long sentence A long sentence A long sentence A long sentence A long sentence";
JLabel label=new JLabel();
label.setText("<html><body>"+s+"</body></html>");

With the above code, u woun't see any html tags and also according to the width of ur label the text get wrapped as
in the case of a textpane/editorpane


[This message has been edited by Premila (edited September 18, 2001).]
[This message has been edited by Premila (edited September 18, 2001).]
[This message has been edited by Premila (edited September 18, 2001).]
[This message has been edited by Premila (edited September 18, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!