// Fig. 23.4: PropertiesTest.java // Demonstrates class Properties of the java.util package. import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PropertiesTest extends JFrame { private JLabel status; private Properties table; private JTextArea display; public PropertiesTest() { super( "Properties Test" ); table = new Properties(); Container c = getContentPane(); JPanel northPanel = new JPanel(); northPanel.setLayout( new BorderLayout() ); JPanel northSubPanel = new JPanel(); JPanel southPanel = new JPanel(); northSubPanel.add( new JLabel( "Property value" ) ); final JTextField propVal = new JTextField( 10 ); northSubPanel.add( propVal ); northPanel.add( northSubPanel, BorderLayout.NORTH ); northSubPanel.add( new JLabel( "Property name (key)" ) ); final JTextField propName = new JTextField( 10 ); northSubPanel.add( propName ); display = new JTextArea( 4, 35 ); JButton put = new JButton( "Put" ); put.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { Object val = table.put( propName.getText(), propVal.getText() ); if ( val == null ) showStatus( "Put: " + propName.getText() + " " + propVal.getText() ); else showStatus( "Put: " + propName.getText() + " " + propVal.getText() + "; Replaced: " + val.toString() ); listProperties(); } } ); southPanel.setLayout( new GridLayout( 1, 5 ) ); southPanel.add( put ); JButton clear = new JButton( "Clear" ); clear.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { table.clear(); showStatus( "Table in memory cleared" ); listProperties(); } } ); southPanel.add( clear ); JButton getProperty = new JButton( "Get property" ); getProperty.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { String val = table.getProperty( propName.getText() ); if ( val != null ) showStatus( "Get property: " + propName.getText() + " " + val ); else showStatus( "Get: " + propName.getText() + " not in table" ); listProperties(); } } ); southPanel.add( getProperty ); JButton save = new JButton( "Save" ); save.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { try { FileOutputStream output; output = new FileOutputStream( "props.dat" ); table.store( output, "Sample Properties" ); output.close(); listProperties(); } catch( IOException ex ) { showStatus( ex.toString() ); } } } ); southPanel.add( save ); JButton load = new JButton( "Load" ); load.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { try { FileInputStream input; input = new FileInputStream( "props.dat" ); table.load( input ); input.close(); listProperties(); } catch( IOException ex ) { showStatus( ex.toString() ); } } } ); southPanel.add( load ); status = new JLabel(); northPanel.add( status, BorderLayout.SOUTH ); c.add( northPanel, BorderLayout.NORTH ); c.add( new JScrollPane( display ), BorderLayout.CENTER ); c.add( southPanel, BorderLayout.SOUTH ); setSize( 550, 225 ); show(); } public void listProperties() { StringBuffer buf = new StringBuffer(); String pName, pVal; Enumeration enum = table.propertyNames(); while( enum.hasMoreElements() ) { pName = enum.nextElement().toString(); pVal = table.getProperty( pName ); buf.append( pName ).append( '\t' ); buf.append( pVal ).append( '\n' ); } display.setText( buf.toString() ); } public void showStatus( String s ) { status.setText( s ); } public static void main( String args[] ) { PropertiesTest app = new PropertiesTest(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } /************************************************************************** * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/