1 8 9 package demo; 10 11 import swingwt.awt.*; 12 import swingwt.awt.event.*; 13 import swingwtx.swing.*; 14 import swingwtx.swing.event.*; 15 import swingwtx.custom.validation.*; 16 17 import java.util.*; 18 19 public class ValidationTest extends JFrame { 20 21 ValidatableJTextField txtFullName; 22 ValidatableJTextField txtTown; 23 ValidatableJTextField txtAge; 24 ValidatableJTextField txtPhone; 25 ValidationGroup vg; 26 27 public ValidationTest() { 28 29 setTitle("ValidationGroup Sample"); 30 setSize(new Dimension(300, 200)); 31 32 getContentPane().setLayout(new BorderLayout()); 33 34 vg = new ValidationGroup(); 35 36 JPanel pnlMain = new JPanel(); 38 pnlMain.setLayout(new TableLayout(0, 2)); 39 40 pnlMain.add(new JLabel("Full Name:")); 41 42 txtFullName = new ValidatableJTextField(); 43 txtFullName.setPreferredSize(new Dimension(200, 25)); 44 txtFullName.addValidationListener(new ValidationListener() { 46 public void validating(ValidationEvent e) { 47 if (txtFullName.getText().trim().length() == 0) 48 e.setValid(false, "Please enter the full name of the customer."); 49 else { 50 if (txtFullName.getText().indexOf(" ") == -1) 51 e.setValid(false, "Please enter at least the first and last name of the customer."); 52 else 53 e.setValid(true); 54 } 55 } 56 }); 57 pnlMain.add(txtFullName); 58 vg.add(txtFullName); 60 61 pnlMain.add(new JLabel("Town:")); 62 63 txtTown = new ValidatableJTextField(); 64 txtTown.setPreferredSize(new Dimension(200, 25)); 65 txtTown.addValidationListener(new ValidationListener() { 66 public void validating(ValidationEvent e) { 67 if (txtTown.getText().trim().length() == 0) 68 e.setValid(false, "Please enter the customer's town."); 69 else 70 e.setValid(true); 71 } 72 }); 73 pnlMain.add(txtTown); 74 vg.add(txtTown); 76 pnlMain.add(new JLabel("Age:")); 77 78 txtAge = new ValidatableJTextField(); 79 txtAge.setPreferredSize(new Dimension(200, 25)); 80 txtAge.addValidationListener(new ValidationListener() { 81 public void validating(ValidationEvent e) { 82 if (!isNumber(txtAge.getText())) 83 e.setValid(false, "Please enter the customer's age, as a whole number."); 84 else 85 e.setValid(true); 86 } 87 public boolean isNumber(String no) { 88 try { Integer.parseInt(no); } catch (NumberFormatException e) { return false; } return true; 89 } 90 }); 91 pnlMain.add(txtAge); 92 vg.add(txtAge); 94 pnlMain.add(new JLabel("Phone #")); 95 96 txtPhone = new ValidatableJTextField(); 97 txtPhone.setPreferredSize(new Dimension(200, 25)); 98 txtPhone.addValidationListener(new ValidationListener() { 99 public void validating(ValidationEvent e) { 100 if ( 101 (txtPhone.getText().trim().length() == 0) || 102 (txtPhone.getText().trim().length() != 8) 103 ) 104 e.setValid(false, "Please enter the customer's phone number in the format nnn-nnnn."); 105 else 106 e.setValid(true); 107 } 108 }); 109 pnlMain.add(txtPhone); 110 vg.add(txtPhone); 112 113 JPanel pnlButtons = new JPanel(); 115 pnlButtons.setLayout(new FlowLayout()); 116 117 JButton btnCancel = new JButton("Cancel"); 118 btnCancel.setMnemonic('c'); 119 btnCancel.addActionListener(new ActionListener() { 120 public void actionPerformed(ActionEvent e) { 121 dispose(); 122 } 123 }); 124 pnlButtons.add(btnCancel); 125 126 JButton btnOk = new JButton("Ok"); 127 btnOk.setMnemonic('o'); 128 final ValidationTest valTest = this; 129 btnOk.addActionListener(new ActionListener() { 130 public void actionPerformed(ActionEvent e) { 131 if (!vg.checkValidation()) 132 JOptionPane.showMessageDialog(valTest, "Validation Failed.", "Error", JOptionPane.ERROR_MESSAGE); 133 else 134 dispose(); 135 } 136 }); 137 pnlButtons.add(btnOk); 138 setDefaultButton(btnOk); 139 140 ValidationFocusManager fm = new ValidationFocusManager(); 142 Vector comps = new Vector(); 143 comps.add(txtFullName.getComponent()); 144 comps.add(txtTown.getComponent()); 145 comps.add(txtAge.getComponent()); 146 comps.add(txtPhone.getComponent()); 147 comps.add(btnCancel); 148 comps.add(btnOk); 149 fm.setComponents(comps); 150 comps = null; 151 FocusManager.setCurrentManager(fm); 152 153 getContentPane().add(pnlMain, BorderLayout.CENTER); 155 getContentPane().add(pnlButtons, BorderLayout.SOUTH); 156 157 160 show(); 162 while(true){ 163 try { 164 Thread.sleep(1000); 165 } catch (InterruptedException e) { 166 e.printStackTrace(); 168 } 169 } 170 } 171 172 public static void main(String [] args) { 173 new ValidationTest(); 174 } 175 176 } 177 178 182 class ValidationFocusManager extends FocusManager { 183 Vector comps = null; 184 public Vector getComponents() { return comps; } 185 public void setComponents(Vector v) { comps = v; } 186 public void focusNextComponent(Component c) { 187 for (int i = 0; i < comps.size(); i++) { 188 if (comps.get(i).equals(c)) 189 if (i < comps.size() -1) { 190 ((Component) comps.get(i+1)).grabFocus(); 191 return; 192 } 193 } 194 } 195 public void focusPreviousComponent(Component c) { 196 for (int i = 0; i < comps.size(); i++) { 197 if (comps.get(i).equals(c)) 198 if (i > 0) { 199 ((Component) comps.get(i-1)).grabFocus(); 200 return; 201 } 202 } 203 } 204 } 205 | Popular Tags |