1

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.

Kushan
  • 10,657
  • 4
  • 37
  • 41
Viktor
  • 59
  • 8

2 Answers2

1

You can use HTML tag like this,

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

Use line separator as <br/>

Kushan
  • 10,657
  • 4
  • 37
  • 41
1

System.getProperty("line.separator") is the OS dependent line separator.
You can even use it System.lineSeparator() (without error prone string parameter).
But what you want has no relation with the OS line separators.
You need a line separator for a SWING widget text, here a JLabel.

You could use html tag to perform a break line in the JLabel text with <br/> tag.

But you should not use the toString() method to create the String that you will use to set the JLabel text.
toString() is for debugging, not for the functional messages of the application.
Create rather a specific method to do it.

For example :

final static String HTML_BR = "<br/>";

private String createLabelMsg(MyClass myClass) {

    String partner = "<html>DRIVE:" 
            + myClass.getSkill(Skills.DRIVE) + HTML_BR + "VEHICLE:"
            + myClass.getSkill(Skills.VEHICLE) + HTML_BR + "ACCURACY:"
            + myClass.getSkill(Skills.ACCURACY) + HTML_BR + "WEAPONS:"
            + myClass.getSkill(Skills.WEAPONS) + HTML_BR + "REFLEX:"
            + myClass.getSkill(Skills.REFLEX) + HTML_BR + " STRATEGY:"
            + myClass.getSkill(Skills.STRATEGY) + HTML_BR + "CHARISMA:"
            + myClass.getSkill(Skills.CHARISMA) + HTML_BR + "HACKING:"
            + myClass.getSkill(Skills.HACKING) + HTML_BR + "SPEED:" 
            + myClass.getSkill(Skills.SPEED) + HTML_BR + "STEALTH:" 
            + myClass.getSkill(Skills.STEALTH) + HTML_BR
            + "</html>";
    return partner;
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215