Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Java System Properties with Swing

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

like image 413
Gilbert Le Blanc Avatar asked Jun 28 '13 13:06

Gilbert Le Blanc


2 Answers

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

like image 189
Andrew Thompson Avatar answered Oct 13 '22 19:10

Andrew Thompson


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());
    }

}
like image 5
Gilbert Le Blanc Avatar answered Oct 13 '22 21:10

Gilbert Le Blanc