new line in a Label or a Button  
Author Message
Marco





PostPosted: 2005-3-28 15:10:00 Top

java-programmer, new line in a Label or a Button It is possible, in awt, to have a phrase divided in more of one line? I
want display in a Button this:

save
with
name

and not this:

save with name

But if i write:
Button b=new
Button("save"+System.getProperty("line.separator")+"with"+System.getProperty("line.separator")+"name");
I see only strange characters, and not new lines.



 
Steve W. Jackson





PostPosted: 2005-3-29 2:51:00 Top

java-programmer >> new line in a Label or a Button In article <4247ad86$0$18158$email***@***.com>,
"Marco" <email***@***.com> wrote:

> It is possible, in awt, to have a phrase divided in more of one line? I
> want display in a Button this:
>
> save
> with
> name
>
> and not this:
>
> save with name
>
> But if i write:
> Button b=new
> Button("save"+System.getProperty("line.separator")+"with"+System.getProperty("
> line.separator")+"name");
> I see only strange characters, and not new lines.

Both of the JLabel and JButton classes in Swing support using HTML for
their string values. I've never used any AWT, so I can't say for
certain whether they do or not -- but I suspect that they do.

So to set up a JButton like you want, you could simply create a new
JButton like this:

JButton btn = new JButton("<html>save<br>with<br>name</html>");

The closing "html" tag isn't strictly necessary, but I consider it bad
form not to close the tag.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Marco





PostPosted: 2005-3-29 3:03:00 Top

java-programmer >> new line in a Label or a Button > So to set up a JButton like you want, you could simply create a new
> JButton like this:
>
> JButton btn = new JButton("<html>save<br>with<br>name</html>");
>

I read this, but with a Button the result is:

<html>save<br>with<br>name</html>

and not:

save
with
name

I know that swing was wroten in awt, then I think that if I can do anything
with swing, I can do with awt to. Or not? And how?


 
 
Thomas Weidenfeller





PostPosted: 2005-3-29 15:14:00 Top

java-programmer >> new line in a Label or a Button Marco wrote:
> It is possible, in awt, to have a phrase divided in more of one line? I
> want display in a Button this:

You have to implement your very own button, or switch to Swing.

/Thomas



--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
 
 
Marco





PostPosted: 2005-3-30 0:07:00 Top

java-programmer >> new line in a Label or a Button > You have to implement your very own button

How?


 
 
Marco





PostPosted: 2005-3-30 1:39:00 Top

java-programmer >> new line in a Label or a Button >> You have to implement your very own button
> How?

import java.io.*;
import java.awt.*;


public class ButtonMultiline extends Button
{
String[] texts;

public ButtonMultiline(String[] t)
{
super();
texts=t;
}
public void paint(Graphics g)
{
super.paint(g);
Dimension d=this.getSize();
int dy=d.height/(texts.length+1)+1;
FontMetrics metricaFont = getFontMetrics(g.getFont());
for (int i=0;i<texts.length;i++)
{g.drawString(texts[i],(d.width-metricaFont.stringWidth(texts[i]))/2,dy/4+dy*(i+1));}
}
}