PrefsDemo.java

Download PrefsDemo.java source file

// Java application showing Preferences API.  The window can be moved
// and/or resized.  Using a ComponentListener, the window's new bounds
// are saved.  The next time the application launches, it will position
// the window to the saved bounds.
//
// Written 9/2007 by Wayne Pollock, Tampa Florida USA.
// Adapted from a "tip" from:
//  java.sun.com/developer/JDCTechTips/2003/tt0715.html#1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.prefs.*;

class PrefsDemo extends JFrame implements ComponentListener
{
   private Preferences myPrefs;

   public PrefsDemo ()
   {
      setTitle( "Preferences Demo" );
      Font font = new Font( "Sanserif", Font.BOLD, 24 );

      JLabel title =
        new JLabel( "Preferences API Demo", JLabel.CENTER );
        title.setFont( font );
      add( title, BorderLayout.NORTH );

      JLabel instructions = 
         new JLabel ("<html>Move and/or resize window<br>then "
           + "relaunch this application</html>", JLabel.CENTER );
        instructions.setFont( font );
      add( instructions, BorderLayout.CENTER );

      JButton btn = new JButton("<html>Clear<br>prefs</html>" );
        JPanel p = new JPanel();
        p.add( btn );
      add ( p, "South" );

      btn.addActionListener(
        new ActionListener() {
           public void actionPerformed ( ActionEvent e )
           {  clearPrefs(); }
        }
      );

      // Obtain reference to user prefs for this app:
      myPrefs = Preferences.userNodeForPackage( PrefsDemo.class );

      setToPreferredBounds();  // Restore previous window position.

      addComponentListener( this );  // Note: done after setting bounds!

      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      setVisible( true );
   }

   void setToPreferredBounds ()
   {
      // Must include a default value when reading prefs.  Note that
      // in a production quality application you would not hard-code
      // these here; defining a final awt Rectangle at the top is
      // a better idea!

      int width  = myPrefs.getInt( "width", 400 );
      int height = myPrefs.getInt( "height", 200 );
      int x      = myPrefs.getInt( "x", 0 );
      int y      = myPrefs.getInt( "y", 0 );

      setBounds(x, y, width, height);
   }

   void updatePrefs( )  // Store current window bounds
   {
      myPrefs.putInt( "width",  (int) getWidth() );
      myPrefs.putInt( "height", (int) getHeight() );
      myPrefs.putInt( "x",      (int) getX() );
      myPrefs.putInt( "y",      (int) getY() );
   }

   void clearPrefs ()
   {
      try { myPrefs.clear(); }
      catch ( BackingStoreException e ) { System.out.println(e); }
   }

   public void componentMoved   (ComponentEvent e) { updatePrefs(); }
   public void componentResized (ComponentEvent e) { updatePrefs(); }
   public void componentShown   (ComponentEvent e) {}
   public void componentHidden  (ComponentEvent e) {}

   public static void main ( String [] args )
   {
      JFrame win = new PrefsDemo();
   }
}