Reputation: 189
I need to to have text formatted in HTML. The text was therefore enclosed in HTML tags with proper color and alignment applied. The color settings works, but not the alignment set by 'text-align: center;'. Looking to center align HTML text in the 'botlabel'.
Cleaning and rebuilding the project did not help. Any thoughts as to what might be overriding this alignment setting? Any help is much appreciated. Thank you.
Here's the code
public class TestMessageDialogV2 extends JFrame {
public static void main(String[] args) {
final String html = "<html><h3 style='color: #ff0000; padding: 3px;'>The folder does not exist at this path.</h3>"
+ "<ol style='padding: 3px;'><li><span style='color: #000000;'>a folder path</span></li>"
+ "</ol><h3 style='color: #ff0000;padding: 3px;'>Invalid files selected:</h3>"
+ "<ol style='padding: 3px;'><li>"
+ "<span style='color: #000000;'>The file does not exist at this path.<br />some file</span></li>"
+ "</ol><br clear=all />"
+ "</html>";
UIManager.put("OptionPane.background", Color.WHITE.brighter());
UIManager.put("Panel.background", Color.WHITE.brighter());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout() );
panel.setBackground( Color.WHITE.brighter() );
final JEditorPane editorPane = new JEditorPane("text/html", html);
editorPane.setEditable(false);
//editorPane.setPreferredSize();
editorPane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(editorPane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
JScrollPane scrollPane = new JScrollPane (
editorPane
, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(400,120));
JLabel botlabel = new JLabel();
botlabel.setText(
"<html>"
+ "<br clear=all />"
+ "<h3 style='color: #0033CC; text-align: center; '>"
//+ com.fox.dv.utils.iofile.DoHtml.SPACES
+ "Press Ok to continue or Cancel to exit.</h3>"
+ "</html>"
);
botlabel.setBorder(BorderFactory.createLineBorder( Color.black, 1 ) );
botlabel.setHorizontalTextPosition(JLabel.CENTER);
botlabel.setVerticalAlignment(SwingConstants.CENTER);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(botlabel, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(
null
, panel
, "Title"
, JOptionPane.ERROR_MESSAGE
);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static final long serialVersionUID = -2116426209967837157L;
}
Upvotes: 3
Views: 1774
Reputation: 205865
Because support for HTML in Swing Components is limited, specify JLabel.CENTER
for botlabel
.
JLabel botlabel = new JLabel("", JLabel.CENTER);
botlabel.setText(
"<html>"
+ "<br clear=all />"
+ "<h3 style='color: #0033CC'>"
+ "Press Ok to continue or Cancel to exit.</h3>"
+ "</html>"
);
Upvotes: 5