001 package com.croftsoft.core.gui; 002 003 import java.awt.*; 004 import java.util.Hashtable; 005 006 import com.croftsoft.core.lang.NullArgumentException; 007 008 /********************************************************************* 009 * A Panel of TextFields with identifying Labels. 010 * 011 * <p> 012 * Created TextField objects are available via an accessor method. 013 * </p> 014 * 015 * <p> 016 * Example: 017 * <code> 018 * <pre> 019 * LabeledFieldsPanel1 labeledFieldsPanel1 = new LabeledFieldsPanel1 ( 020 * new String [ ] { "username", "password" } ); 021 * 022 * TextField passwordTextField 023 * = labeledFieldsPanel1.getTextField ( "password" ); 024 * 025 * passwordTextField.setEchoChar ( '*' ); 026 * </pre> 027 * </code> 028 * </p> 029 * 030 * <p> 031 * Java 1.1 compatible. 032 * </p> 033 * 034 * @version 035 * 2001-08-08 036 * @since 037 * 2001-03-28 038 * @author 039 * <a href="https://www.croftsoft.com/">David W. Croft</a> 040 *********************************************************************/ 041 042 public final class LabeledFieldsPanel1 043 extends Panel 044 ////////////////////////////////////////////////////////////////////// 045 ////////////////////////////////////////////////////////////////////// 046 { 047 048 private final Hashtable labelNameToTextFieldMap = new Hashtable ( ); 049 050 ////////////////////////////////////////////////////////////////////// 051 ////////////////////////////////////////////////////////////////////// 052 053 public LabeledFieldsPanel1 ( 054 String [ ] labelNames, 055 Color panelBackgroundColor, 056 Color textFieldBackgroundColor ) 057 ////////////////////////////////////////////////////////////////////// 058 { 059 super ( new BorderLayout ( ) ); 060 061 NullArgumentException.check ( labelNames ); 062 063 Panel labelsPanel = new Panel ( 064 new GridLayout ( labelNames.length, 1, 4, 4 ) ); 065 066 Panel fieldsPanel = new Panel ( 067 new GridLayout ( labelNames.length, 1, 4, 4 ) ); 068 069 for ( int i = 0; i < labelNames.length; i++ ) 070 { 071 String labelName = labelNames [ i ]; 072 073 labelsPanel.add ( new Label ( labelName ) ); 074 075 TextField textField = new TextField ( ); 076 077 labelNameToTextFieldMap.put ( labelName, textField ); 078 079 fieldsPanel.add ( textField ); 080 081 if ( textFieldBackgroundColor != null ) 082 { 083 textField.setBackground ( textFieldBackgroundColor ); 084 } 085 } 086 087 if ( panelBackgroundColor != null ) 088 { 089 setBackground ( panelBackgroundColor ); 090 } 091 092 add ( labelsPanel, BorderLayout.WEST ); 093 094 add ( fieldsPanel, BorderLayout.CENTER ); 095 } 096 097 public LabeledFieldsPanel1 ( 098 String labelName, 099 Color panelBackgroundColor, 100 Color textFieldBackgroundColor ) 101 ////////////////////////////////////////////////////////////////////// 102 { 103 this ( new String [ ] { labelName }, 104 panelBackgroundColor, textFieldBackgroundColor ); 105 } 106 107 public LabeledFieldsPanel1 ( String [ ] labelNames ) 108 ////////////////////////////////////////////////////////////////////// 109 { 110 this ( labelNames, null, null ); 111 } 112 113 public LabeledFieldsPanel1 ( String labelName ) 114 ////////////////////////////////////////////////////////////////////// 115 { 116 this ( new String [ ] { labelName }, null, null ); 117 } 118 119 ////////////////////////////////////////////////////////////////////// 120 ////////////////////////////////////////////////////////////////////// 121 122 public TextField getTextField ( String labelName ) 123 ////////////////////////////////////////////////////////////////////// 124 { 125 return ( TextField ) labelNameToTextFieldMap.get ( labelName ); 126 } 127 128 ////////////////////////////////////////////////////////////////////// 129 ////////////////////////////////////////////////////////////////////// 130 }