By default JLabel
cuts off text on the right with 3 dots, if the text is too long to be displayed completely like this:
(Image is from a small backup application I'm working on). As you can see the last JLabel
above the "Cancel"-button is cut off on the right. This behavior is clearly not desirable, as the more relevant part of the text is cut off.
I'd like the resulting label to look like in this image (right column, sry for the bad resolution):
So far I've tried to change the alignment of the text within the label to JLabel.RIGHT
, alter the components orientation to ComponentOrientation.RIGHT_TO_LEFT
, set horizontalTextPosition
, all to no avail:
import javax.swing.*;
import java.awt.*;
public class Backup
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(()->{
JLabel label = new JLabel("Some test text 1 2 3 4 5 6 7 8 9 10 abcdefghjiklmnopqrstuvwxyz", JLabel.RIGHT);
label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
label.setHorizontalTextPosition(JLabel.RIGHT);
JFrame frame = new JFrame();
frame.add(label);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
What I've tried as well, using HTML:
<html>
<body>
<p style="width:300px;overflow-x:hidden;direction:rtl">
kladsjhglakjsjdghlekfalksjdvhlkjdsnkljhsdlkvjhasdkjhfslkdjhcksdjhvflkasjvhlkajdlkajvsdhvlkjsadhaaaaaaaaaaaaa
</p>
</body>
</html>
While this works just in the way it's supposed to in my browser, swing doesn't seem to support a sufficient set of style-properties to support this behavior.
It shouldn't be too hard to code a own implementation that fulfills the requirement of doing precisely this. Nevertheless I was wondering whether there was a "swing-way" of achieving this.