tobi tobi - 6 months ago 12
Java Question

Putting 3 dots at the beginning of JLabel

When the text in JLabel is too long there are visible 3 dots at the end of text. Is it possible to put them at the beginning?

Answer

You may consider using FontMetrics. class to see the length of your text under the current font.

_________________________________
|
| This is some really long text that I want to fit in the small label
|________________________________

       ^^^ YOUR LABEL ^^^  

Say you want to fit that long text into that label.
Here is what you can do (and this is just a wild guess and I am making this on the fly)

  1. Start with your three dots ... in a String.
  2. Start adding appending characters to it, one by one.
  3. Get the width of your JLabel.
  4. Use FontMetrics to measure the length of your text , in pixels, as you append more characters
  5. Keep adding more characters as long as the pixel length of the text is less than the width of your JLabel
  6. Once it becomes greater than the width of the JLabel, get out of the loop.
  7. Set this newly formed text as the text of your JLabel

You should end up like this:

_________________________________
|
| ...This is some really long tex
|________________________________

       ^^^ YOUR LABEL ^^^  

Here is an easy way to get started with FontMetrics. Avoid the bickering there. Just do what the accepted answer says: Java: Friendlier way to get an instance of FontMetrics

SSCCE is in accordance with what the OP really wants rather than what I explained

package stack;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class BackwardsDots extends JFrame{

    JLabel label = new JLabel(){
                        @Override
                        public Dimension getPreferredSize(){
                            return new Dimension(200,100);
                        }
                    };
    String text = "This is a design requirement and not my whim";
    FontMetrics fm;
    Font theFontBeingUsed;
//--------------------------------------------------------------------------------  
    public BackwardsDots(){
        getContentPane().add(label);
        pack();

        theFontBeingUsed = new Font("Ubuntu",Font.BOLD,14);
        fm = Toolkit.getDefaultToolkit().getFontMetrics(theFontBeingUsed);



        label.setText(trimmedStringCalculator(text));
        label.setToolTipText(text);
        label.setBorder(BorderFactory.createDashedBorder(Color.RED));
        label.addComponentListener(new ComponentListener(){

            @Override
            public void componentHidden(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void componentMoved(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void componentResized(ComponentEvent arg0) {
                label.setText(trimmedStringCalculator(text));
            }

            @Override
            public void componentShown(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }

        });

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
//--------------------------------------------------------------------------------
    private String trimmedStringCalculator(String inputText){
        String ellipses = "...";
        String textToBeDisplayed = "";

        int widthOfJLabel = label.getWidth();

        for(int i = text.length()-1; i >= 0; i--){
            if(fm.stringWidth(ellipses + textToBeDisplayed) <= widthOfJLabel){
                textToBeDisplayed = text.charAt(i) + textToBeDisplayed;
            }
        }

        String finalText;
        if(textToBeDisplayed.equals(inputText)){
            finalText = inputText;
        }else{
            finalText = ellipses.concat(textToBeDisplayed);
        }

        return finalText;
    }
//--------------------------------------------------------------------------------  
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                new BackwardsDots();
            }
        });
    }
}

Output
enter image description here