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:
See also this answer to List of useful environment settings in Java.
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With