I have recently came upon odd situation; HTML formatted text in my Swing components do not show up. I have searched for similar questions, and found one, but it does not seem to resolve in a solution that applies to my case (even though there is an accepted answer)
I have also played around with the HtmlDemo
class on the Oracle tutorials. The code copied from there to my project does not work, the same way as my own code doesn't. However if I compile the whole project, or run it via JWS, everything looks OK.
I have tried to isolate the problem with smaller cases (such as the commented JLabel
s) but still no joy. Currently I am really puzzled; almost certain that I miss a detail somewhere but can't seem to locate it.
Below is a method that returns a JScrollPane
to be placed in a JTabbedPane
, which should depict the relevant pieces of the code. Oh, and btw I am currently using Sun JDK 1.6.0 Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Any help/comments are appreciated!
JTabbedPane tabbedPane = new JTabbedPane();
JComponent panel1 = makeInfoPanel(v);
tabbedPane.addTab("Node Info", null, panel1,"info");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JComponent panel2 = makeDetailsPanel(v);
tabbedPane.addTab("Details", null, panel2,"other info");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
protected JComponent makeInfoPanel(Node v) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400,200));
panel.setLayout(new GridLayout(0,1));
//JLabel title = new JLabel("<html><b><u>T</u>wo</b><br>lines</html>");
String msg = "Below are the items this node represents";
//JLabel title = new JLabel("<html><font size=-2>" + msg +"</font></html>");
JLabel title = new JLabel(msg);
title.setHorizontalAlignment(JLabel.LEFT);
panel.add(title);
JLabel stuff = new JLabel();
StringBuilder labelText = new StringBuilder("<html><h3>Items</h3>");
for(Stuff s : v.getStuff()){
labelText.append("<font size=8><i>" + s.getName() + "</i></font>");
labelText.append(" nbsp; <font size=6>" + s.getDb().toUpperCase() + "</font>");
labelText.append("<br>");
}
labelText.append("</html>");
stuff.setText(labelText.toString());
panel.add(stuff);
return new JScrollPane(panel);
}