8

Is there an easy way, using Java Swing, to display the Java system property names and values on my workstation?

Basically, what I'm looking for is a Java Swing application that displays something like this:

System Properties

JREN
  • 3,572
  • 3
  • 27
  • 45
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111

3 Answers3

7

See also this answer to List of useful environment settings in Java.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I did a search before I asked / answered the question. I really did. – Gilbert Le Blanc Jun 28 '13 at 13:59
  • 1
    @GilbertLeBlanc Yes, I *did* notice you answered your own question. I just thought my answer was better, and since you did not mention why it was **not** also an answer to your question.. But if it makes you feel better, I know what it feels like.. See [this question](http://stackoverflow.com/questions/10836832/show-an-animated-bg-in-swing) for a Q&A I intended to keep all to myself. In the end I had to concede the other answer was indeed, better than my own. You might not agree in this case, no harm done, but just know - yes, I've been there.. ;) – Andrew Thompson Jun 28 '13 at 15:44
  • @Andrew Thompson: No problem. Your answer is better. I've spent all week crawling to 20k, and this question / answer would have done it in 10 minutes. :-) – Gilbert Le Blanc Jun 28 '13 at 17:10
  • +1 to your answer, BTW. I have to agree the tool-tips were a nice touch. :) & congrats on the 20K! – Andrew Thompson Jun 28 '13 at 17:13
5

The code is pretty straightforward. Create a JTable inside of a JScrollPane, inside of a JFrame.

I had to type a few lines of code to build a table model for the JTable. I sorted the property names to make them easier to find.

The override of the JTable prepareRenderer method shows tool tips for all of the cells. The cells that need the tool tip display are the two value cells with path strings.

The system property names on your system may not be the same as the system property names on other systems. Windows and Unix each have their own unique set of system property names.

import java.awt.Component;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class SystemProperties implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame("System Properties");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTable table = new JTable(createSystemPropertiesTableModel()) {
            private static final long   serialVersionUID    = 4957089825220999913L;

            @Override
            public Component prepareRenderer(TableCellRenderer renderer,
                    int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (c instanceof JComponent) {
                    JComponent jc = (JComponent) c;
                    String s = getValueAt(row, column).toString();
                    jc.setToolTipText(s);
                }
                return c;
            }
        };
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane);

        frame.pack();
        frame.setVisible(true);
    }

    private DefaultTableModel createSystemPropertiesTableModel() {
        DefaultTableModel model = new DefaultTableModel();

        model.addColumn("Property");
        model.addColumn("Value");

        Properties p = System.getProperties();
        Set<Object> keys = p.keySet();
        SortedSet<Object> sortedKeys = new TreeSet<Object>(keys);
        Iterator<Object> iter = sortedKeys.iterator();

        while (iter.hasNext()) {
            String key = iter.next().toString();
            String value = p.getProperty(key);
            String[] row = { key, value };
            model.addRow(row);
        }

        return model;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SystemProperties());
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • +1 for tooltips; see also this related [example](http://stackoverflow.com/a/9134371/230513) using `AbstractTableModel`. – trashgod Jun 28 '13 at 13:11
2

You can access system properties using System.getProperties(). Then all you have to do is to iterate it's keys and manipulate the data the way you want.

public static void main(String[] args) {
    Properties systemProperties = System.getProperties();
    Enumeration<?> e = systemProperties.propertyNames();

    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        String value = systemProperties.getProperty(key);
        System.out.println(key + " -- " + value);
    }
}
Marlon Bernardes
  • 13,265
  • 7
  • 37
  • 44