Display Java System Properties with Swing
Asked Answered
E

3

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

Echino answered 28/6, 2013 at 13:4 Comment(4)
I just realized that you asked the question and answered it one minute later. Don't get me wrong, but what's the point? (I'm new to SO)Heavenly
@Marlon Bernardes: So far. +60 points. :-) I'll look up the answer on meta and post it here. And here it is: meta.stackexchange.com/questions/17463/…Echino
That's good to know! +1 for meta reference!Heavenly
Perhaps you can email the java people and ask them.Kuomintang
F
7

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

Fields answered 28/6, 2013 at 13:40 Comment(4)
I did a search before I asked / answered the question. I really did.Echino
@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 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.. ;)Fields
@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. :-)Echino
+1 to your answer, BTW. I have to agree the tool-tips were a nice touch. :) & congrats on the 20K!Fields
E
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());
    }

}
Echino answered 28/6, 2013 at 13:5 Comment(1)
+1 for tooltips; see also this related example using AbstractTableModel.Mofette
H
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);
    }
}
Heavenly answered 28/6, 2013 at 13:8 Comment(1)
You might want to substring that value, especially on the path properties.Echino

© 2022 - 2024 — McMap. All rights reserved.