/* * MemoryUsagePanel.java * * Created on August 17, 2006, 11:24 AM */ package ika.gui; import javax.swing.*; import java.awt.*; import java.util.*; /** * A utility dialog that displays the total and the free memory. For debugging * purposes only. * @author jenny */ public class MemoryUsagePanel extends javax.swing.JPanel { /** Creates new form MemoryUsagePanel */ public MemoryUsagePanel() { initComponents(); } private static JDialog dialog = null; private java.util.Timer timer = new java.util.Timer(); private Updater updater = new Updater(); private String bytesToString (long bytes) { bytes /= 1024; if (bytes > 1024) return Long.toString(bytes / 1024) + " MB"; else return Long.toString(bytes) + " KB"; } private class Updater extends TimerTask { public void run() { long tot = Runtime.getRuntime().totalMemory(); long free = Runtime.getRuntime().freeMemory(); totalMemoryLabel.setText(bytesToString(tot)); freeMemoryLabel.setText(bytesToString(free)); bar.setValue((int)(100 - 100. * free / tot)); } } public static void showMemoryUsagePanel() { if (dialog != null){ dialog.setVisible(true); return; } final MemoryUsagePanel panel = new MemoryUsagePanel(); dialog = new JDialog(); dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { try {panel.timer.cancel();} catch (Exception e) {} dialog.setVisible(false); } }); dialog.getContentPane().add(panel); dialog.setTitle("Memory Usage"); dialog.pack(); dialog.setResizable(false); dialog.setAlwaysOnTop(true); java.awt.Dimension screen = dialog.getToolkit().getScreenSize(); java.awt.Dimension size = dialog.getSize(); dialog.setLocation((screen.width - size.width) / 2, (screen.height - size.height) / 2 ); dialog.setVisible(true); panel.timer.schedule(panel.updater, 0, 1000); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; bar = new javax.swing.JProgressBar(); jLabel1 = new javax.swing.JLabel(); totalMemoryLabel = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); freeMemoryLabel = new javax.swing.JLabel(); setLayout(new java.awt.GridBagLayout()); setPreferredSize(new java.awt.Dimension(250, 120)); bar.setMinimumSize(new java.awt.Dimension(150, 20)); bar.setPreferredSize(new java.awt.Dimension(150, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0); add(bar, gridBagConstraints); jLabel1.setText("Total Reserved Memory:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; add(jLabel1, gridBagConstraints); totalMemoryLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); totalMemoryLabel.setText("-"); totalMemoryLabel.setMaximumSize(new java.awt.Dimension(100, 16)); totalMemoryLabel.setMinimumSize(new java.awt.Dimension(50, 16)); totalMemoryLabel.setPreferredSize(new java.awt.Dimension(100, 16)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0); add(totalMemoryLabel, gridBagConstraints); jLabel3.setText("Free Memory:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 0); add(jLabel3, gridBagConstraints); freeMemoryLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); freeMemoryLabel.setText("-"); freeMemoryLabel.setMaximumSize(new java.awt.Dimension(100, 16)); freeMemoryLabel.setMinimumSize(new java.awt.Dimension(50, 16)); freeMemoryLabel.setPreferredSize(new java.awt.Dimension(100, 16)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0); add(freeMemoryLabel, gridBagConstraints); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JProgressBar bar; private javax.swing.JLabel freeMemoryLabel; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel totalMemoryLabel; // End of variables declaration//GEN-END:variables }