Search code examples
javaswingjavafxjavafx-8jtextarea

List System Properties in a textarea using javax.swing/javafx


how can I display all System Properties in a javafx or swing Textarea; in a format like "key1"=value1 "key2"=value2 I tried *textarea.setText(System.getProperties().toString()),* but that just listed it like this;

{javafx.version=8.0.72, java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=C:\Program Files\Java\jdk1.8.0_77\jre\bin, java.vm.version=25.77-b03, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.o......

Solution

  • A version of this using Java 8 features would look like

    TextArea textArea = new TextArea();
    
    // or, in Swing, JTextArea textArea = new JTextArea();
    
    textArea.setText( System.getProperties().entrySet().stream()
        .map(e -> String.format("\"%s\" = %s", e.getKey(), e.getValue()))
        .collect(Collectors.joining("\n")));
    

    If you don't need quotes around the keys, you can replace the map step with the simpler

        .map(Object::toString)