1 package snow.utils.gui; 2 3 import java.awt.event.*; 4 import javax.swing.*; 5 import java.awt.*; 6 import java.text.*; 7 8 9 public class MemoryInfoPanel extends JPanel 10 { 11 final int gap = UIManager.getFont("Label.font").getSize(); 12 final JLabel jvmMemoryLabel; 13 final JLabel appMemoryLabel; 14 15 public MemoryInfoPanel() 16 { 17 super(); 18 setLayout( new FlowLayout( FlowLayout.RIGHT, gap,0) ); 19 setOpaque(false); 20 jvmMemoryLabel = new JLabel("JVM") 21 { 22 public void updateUI() 23 { 24 super.updateUI(); 25 this.setForeground(UIManager.getColor("TextField.foreground")); 26 } 27 }; 28 jvmMemoryLabel.setForeground( UIManager.getColor("TextField.foreground") ); 29 appMemoryLabel = new JLabel("App") 30 { 31 public void updateUI() 32 { 33 super.updateUI(); 34 this.setForeground(UIManager.getColor("TextField.foreground")); 35 } 36 }; 37 appMemoryLabel.setForeground( UIManager.getColor("TextField.foreground") ); 38 add( jvmMemoryLabel ); 39 add( appMemoryLabel ); 40 addMouseListener(new MouseAdapter() 41 { 42 @Override public void mousePressed(MouseEvent me) 43 { 44 if(me.isPopupTrigger()) 45 { 46 showPopup(me); 47 return; 48 } 49 } 50 51 @Override public void mouseReleased(MouseEvent me) 52 { 53 if(me.isPopupTrigger()) 54 { 55 showPopup(me); 56 return; 57 } 58 } 59 }); 60 61 start(); 62 } 63 64 private void showPopup(MouseEvent me) 65 { 66 final JPopupMenu popup = new JPopupMenu(); 67 68 JMenuItem gcCall = new JMenuItem("Perform Garbage Collection"); 69 popup.add(gcCall); 70 gcCall.addActionListener(new ActionListener() 71 { 72 public void actionPerformed(ActionEvent ae) 73 { 74 System.gc(); 75 } 76 }); 77 78 popup.show(this, me.getX(), me.getY()); 79 } 80 81 private void start() 82 { 83 final Thread memoryDisplayUpdater = new Thread () 85 { 86 public void run() 87 { 88 while( true ) 89 { 90 try 91 { 92 Thread.sleep(4000); } 94 catch( Exception e43874 ) 95 { 96 } 97 final double jvmMemory = Runtime.getRuntime().totalMemory()/1.0e6; 98 final double appMemory = jvmMemory - Runtime.getRuntime().freeMemory()/1.0e6; 99 final DecimalFormat nf = new DecimalFormat( "#" ); 100 EventQueue.invokeLater( new Runnable () 101 { 102 public void run() 103 { 104 jvmMemoryLabel.setText("JVM " + nf.format(jvmMemory) + " MB"); 105 appMemoryLabel.setText("App " + nf.format(appMemory) + " MB"); 106 } 107 }); 108 } 109 } 110 }; 111 memoryDisplayUpdater.setDaemon(true); 112 memoryDisplayUpdater.setName("MemoryInfoPanel"); 113 memoryDisplayUpdater.start(); 114 } 115 116 117 } | Popular Tags |