2

I've heard somewhere that it's not possible to add \t (tab) spacing in GUIs, is this true? Does \t only apply to System...println etc?

In any case, how would I be able to cause tabbed spacing?

Current Code and Preview of JList

     Code | String.format("%s \t %s \t %s", string1, string2, string3);
     Preview | string1 string2 string3

Expected Preview

     string1         string2           string3

 

Other tests

I did try adding a space String between e.g.

     String space = "        ";
     String.format("%s %s %s %s %s", string1, space, string2, space, string3);

But the spacing isn't consistent with multiple JList models.

Got a solution to this?

Thanks.

1 Answers1

3

JList supports HTML format, so you may use it that way :

DefaultListModel<String> model = new DefaultListModel<>();

StringBuilder builder = new StringBuilder();

builder.append("<html><pre>");
builder.append(String.format("%s \t %s \t %s", "str1", "str2", "str3"));
builder.append("</pre></html>");

model.addElement(builder.toString());

See : Can Items in a JList be formatted as HTML

Community
  • 1
  • 1
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • 1
    This works quite well! Now I have another problem - which is more of my fault for not realising that \t doesnt work. Whenever I do the suggested, it works how \t is expected to, but not what I'm aiming for equal spacing so that string2 and string3 are completely aligned. This can't happen if string1 had different length. Any ideas? I'll mark it as answered since you answered the original question, but please if you can let me know if this is possible. Thanks! – Mohammed Zakariya Dec 14 '16 at 11:01
  • The above answer works perfectly but I also have the same problem as @MohammedZakariya because I also want equal space inbetween – LordDraagon Apr 15 '18 at 10:46