Search code examples
javastringnewlinetostring

How To use new line in a toString while it is in a JLabel?


my toString is the next:

public String toString() {
        String partner = "DRIVE:" + getSkill(Skills.DRIVE) + (System.getProperty("line.separator")) + "VEHICLE:"
                + getSkill(Skills.VEHICLE) + (System.getProperty("line.separator")) + "ACCURACY:"
                + getSkill(Skills.ACCURACY) + (System.getProperty("line.separator")) + "WEAPONS:"
                + getSkill(Skills.WEAPONS) + (System.getProperty("line.separator")) + "REFLEX:"
                + getSkill(Skills.REFLEX) + (System.getProperty("line.separator")) + " STRATEGY:"
                + getSkill(Skills.STRATEGY) + (System.getProperty("line.separator")) + "CHARISMA:"
                + getSkill(Skills.CHARISMA) + (System.getProperty("line.separator")) + "HACKING:"
                + getSkill(Skills.HACKING) + (System.getProperty("line.separator")) + "SPEED:" 
                + getSkill(Skills.SPEED) + (System.getProperty("line.separator")) + "STEALTH:" 
                + getSkill(Skills.STEALTH) + (System.getProperty("line.separator"));

        return partner;
    }

I tried with \n and that is not working. Neither does this.
I have to use a String NOT a Stringbuilder, so that is not good.
I will place this in a JLabel
I need to see this :
DRIVE : somerandomnumberwhatisalreadygenerated
VEHICLE : somerandomnumberwhatisalreadygenerated
The "< br >" is not working as well.


Solution

  • You can use HTML tag like this,

    JLabel label = new JLabel("<html>your string goes here</html>");
    

    Use line separator as <br/>