0

I have a Set words that I would like to display in the Swing JTextField/ or JTextArea. Only these components only accept strings. How do I convert this Set into a String?

mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

0

You can convert the Set to a String, and then pass it to the JTextField/JTextArea.

Simple using toString() on the set returns a String that looks like this: "[a, b, c, d]".

If you want to get rid of the brackets, you can call toString().replace("[", "").replace("]", "")

kabb
  • 2,474
  • 2
  • 17
  • 24
0

Basically, you could iterate the Set and build a String value of the elements, for example...

StringBuilder sb = new StringBuilder(128);
for (String value : values) {
    if (sb.length() > 0) {
        // For JTextArea
        sb.append(System.getProperty("line.separator"));
        // For JTextField
        sb.append(", ");
    }
    sb.append(sb);
}
String output = sb.toString();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366