How can I add \t spacing in a String that belongs to a DefaultListModel which belongs to a JLIst?

一个人想着一个人 提交于 2019-12-08 02:41:31

问题


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:


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



来源:https://stackoverflow.com/questions/41138738/how-can-i-add-t-spacing-in-a-string-that-belongs-to-a-defaultlistmodel-which-be

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!