-
Bug
-
Resolution: Unresolved
-
P4
-
9
-
x86_64
-
windows_7
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 9-ea+169)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+169, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
This is a problem that shows up when using HTML within a JLabel. The underlying problem would be in the javax.swing.text.html.HTMLEditoKit implementation.
The CSS style "text-align=right;" is supposed to set the alignment of text within a container. The expected behavior is that when this style is applied to a <table> then all cells within the table should receive this style.
However, the result that occurs when this style is applied to a table is that the table becomes right-aligned within it's container, and the alignment of the cells within the table are unchanged.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The test program that is included below shows the problem. The layout of the html is incorrect.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was expecting to see the text in the table cells be right aligned, and that the table would be centered.
ACTUAL -
The text within the table cells was not right aligned, but the table was right-aligned instead of being centered.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.imageio.ImageIO;
public class Test3 {
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new Test3();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Test3() {
String sample = "<html><body><center><h2>Heading, paragraph and table</h2></center><p>This example should show a centered heading, a paragraph of text, and then a table centered underneath the text.<p><center><table border=\"1\" cellspacing=\"0\" style=\"text-align: right;\"><tr><th>First column</th><th>Second column</th></tr><tr><td>0</td><td>1</td></tr><tr><td>4</td><td>5</td></tr></table><center></body></html>";
int width = 500;
int height = 800;
// Make a buffer image to paint in to
BufferedImage image = new BufferedImage(width,
height,
BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
System.err.println("Using html string '"+sample+"'");
JLabel jl = new JLabel(sample);
jl.setSize(width, height);
jl.paint(g);
// Save the image
FileOutputStream out = null;
try {
out = new FileOutputStream("c:/tmp/sample.png");
ImageIO.write(image, "png", out);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
out.close();
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The temporary workaround is to not apply the style to the table. Instead apply the deprecated 'align="right"' attribute to each table row element.
Java(TM) SE Runtime Environment (build 9-ea+169)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+169, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
This is a problem that shows up when using HTML within a JLabel. The underlying problem would be in the javax.swing.text.html.HTMLEditoKit implementation.
The CSS style "text-align=right;" is supposed to set the alignment of text within a container. The expected behavior is that when this style is applied to a <table> then all cells within the table should receive this style.
However, the result that occurs when this style is applied to a table is that the table becomes right-aligned within it's container, and the alignment of the cells within the table are unchanged.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The test program that is included below shows the problem. The layout of the html is incorrect.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was expecting to see the text in the table cells be right aligned, and that the table would be centered.
ACTUAL -
The text within the table cells was not right aligned, but the table was right-aligned instead of being centered.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.imageio.ImageIO;
public class Test3 {
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new Test3();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Test3() {
String sample = "<html><body><center><h2>Heading, paragraph and table</h2></center><p>This example should show a centered heading, a paragraph of text, and then a table centered underneath the text.<p><center><table border=\"1\" cellspacing=\"0\" style=\"text-align: right;\"><tr><th>First column</th><th>Second column</th></tr><tr><td>0</td><td>1</td></tr><tr><td>4</td><td>5</td></tr></table><center></body></html>";
int width = 500;
int height = 800;
// Make a buffer image to paint in to
BufferedImage image = new BufferedImage(width,
height,
BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
System.err.println("Using html string '"+sample+"'");
JLabel jl = new JLabel(sample);
jl.setSize(width, height);
jl.paint(g);
// Save the image
FileOutputStream out = null;
try {
out = new FileOutputStream("c:/tmp/sample.png");
ImageIO.write(image, "png", out);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
out.close();
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The temporary workaround is to not apply the style to the table. Instead apply the deprecated 'align="right"' attribute to each table row element.