Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Ellipses (... when text is too long) in JLabel Containing HTML

796262Aug 24 2010 — edited Aug 24 2010
Hello all,

As you can see in my other post, I'm trying to mix Swing components that contain HTML with components that do not. The problem I'm facing is that certain components behave differently when they contain HTML (other than the obvious display differences).

Specifically, a normal, non-HTML JLabel will add ellipses (...) to text that is too long to display. Cool. But if a JLabel contains HTML, it will use word wrapping instead.

The question is, how do I get the non-HTML behavior in a JLabel containing HTML? I know I could use FontMetrics and change the JLabel's text based on the length of what's being displayed, but I'd really love to be able to let JLabel handle that, as it does with non-HTML text.

Here's an SSCCE that demonstrates what I'm talking about:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;

public class LabelHtmlTest {

	public LabelHtmlTest(){

		JFrame frame = new JFrame("HTML Test");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel htmlLabel = new JLabel("<html>This JLabel contains <u>html</u>. It only supports word wrapping.</html>");
		
		JLabel noHtmlLabel = new JLabel("This JLabel contains no html. It supports ellipses!");
		noHtmlLabel.setToolTipText(noHtmlLabel.getText());

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, noHtmlLabel, htmlLabel);
		splitPane.setContinuousLayout(true);

		frame.add(splitPane);

		frame.setSize(200, 200);
		frame.setVisible(true);
		splitPane.setDividerLocation(.5);
	}
	public static void main(String[] args) {
		new LabelHtmlTest();
	}
}
I'm really banging my head against the wall with this HTML / no-HTML stuff. Is there a way to guarantee behavior across components that may or may not contain HTML?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2010
Added on Aug 24 2010
3 comments
2,807 views