I want my JLabel
to draw spaces but for some reason they don't get drawn. This is an example of something that the JLabel
should draw, but it doesn't do so for some reason:
=========
==========
==========
==========
=========
=======
Is there a way for it to do so? Not with borders but with <html>
or \n
or something else.
The code for the JLabel
text:
label.setText("<html>");
for (int i = 0; i < image.getHeight(); i ) {
for (int j = 0; j < image.getWidth(); j ) {
char symbol = getSymbol(i,j,image);
line = symbol;
if (line.length() >= image.getWidth()) {
label.setText(label.getText() "" line "<br>");
line = "";
} // end of if
System.out.print(symbol);
}
System.out.println();
}
label.setText(label.getText() "<br></html>");
CodePudding user response:
Let's assume you want to draw the following:
x
x
x
In plain HTML you can add
-encoded spaces (non breaking spaces) or use preformatted text.
Example using encoded spaces:
String txt = " x<br> x<br>x";
JLabel label = new JLabel("<html>" txt "</html>");
Example using preformatted text:
String txt = " x\n x\nx";
JLabel label = new JLabel("<html><pre>" txt "</pre></html>");
CodePudding user response:
Something like this using the CSS margin property should also work:
myJLabel.setVerticalAlignment(JLabel.TOP);
myJLabel.setHorizontalAlignment(JLabel.LEFT);
myJLabel.setText("<html><p style=margin-top:20>"
"<p style=margin-left:100>First Line"
"<p style=margin-left:100>Second Line"
"<p style=margin-left:100>Third Line"
"<p style=margin-left:100>======="
"<p style=margin-top:5>"
"<p style=margin-left:100>Fourth Line"
"<p style=margin-left:100>Fifth Line"
"<p style=margin-left:100>Sixth Line");
Which in a JLabel, should produce something like this:
Perhaps place the "<p style=margin-top:5>"
and "<p style=margin-left:100>"
into String variables and work it in your for
loop as you build your String for the JLabel.