Wednesday, February 2, 2011

Setting maximum length limit in UITextField in iPhone.

There is no direct method to set the maximum length of input an UITextField should allow. But we can restrict the user to input to a certain length of characters.
Suppose we want a uitextfield only to accept upto 10 characters. Any character (other than enter and delete) should not be allowed to input if the user has already
entered 10 characters. i.e, we want to set a maximum length verifier to the UITextField. In Objective C we can do it as follows.
    We can create our own delegate for the UITextField. In this delegate we can put our logic to verify the input. And also we need to set this delegate as the UITextfield 's
delegate.

Step1.
    Writing the delegate:
    In MaxLengthDelegate.h

    @interface MaxLengthDelegate : NSObject <UITextFieldDelegate>{
        @private
            int max_length; //
      }
      @end

    In MaxLengthDelegate.m implement the following method and also modify the init method to init this with the maximum length user wants.

        - (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered
        {
            //check for enter and delete keys. These should be allowed even after the length is equal to max_length.
            if (([textField.text length] && range.length == 1) || [textEntered isEqualToString:@"\n"]) {
                return YES;
            }
   
            //check the length and return NO if it exceeds the max_length.
            if ([textField.text length] >= max_length) {
                return NO;
            }
   
            return YES;
        }

        //Setting the default max_length to 10.
        - (id) init
        {
            self = [super init];
            if (self != nil) {
                max_length = 10;
            }
            return self;
        }

        //to init delegate with the length.
        - (id) init : (int) maxLength
        {
            self = [super init];
            if (self != nil) {
                max_length = maxLength;
            }
            return self;
        }

Steps 2:
    Set the delegate of the UITextField which length needs to be verified as the maxLengthDelegate. For example, if we want to restrict input length of the UITextField (txtField) to 5, we can do it as:

    MaxLengthDelegate  maxLenDel = [[MaxLengthDelegate alloc] init: 5];
    [txtField setDelegate: maxLenDel];

Thanks

Writing simple callback in java

Callback can be used when you want to invoke some methods on the basis of some property changes. Suppose you want to call a piece of code of
another class automatically when there is a change in property. Or you have a single property that is used by more than one classes to do some
work. So when the value of this property is changed you want to notify those classes so that they can do their work with this updated value.
This can be done by callback routing.
   
    Steps to write callback routine.
Step1.
    Write one interface with the method/methods need(s) to be invoked.
   
    eg.
   
    public interface ICallback {
        public void notifyChange();
    }
   
Step2.
    Now write your binder class that will notify the object which is binded with it.
   
    public class CallbackBinder {
        private ICallback iCallback;   
       
        public void bindCallback (ICallback callBack){
            this.iCallback = callBack;
        }
       
        //so whenever the property changes notify the class object.
        public void notifyCallBack(){
            iCallback.notifyChange();
        }
   
    }

Step3.
    We need to bind the class with the callbackBinder to receive the notification. To do so we have to implement the ICallback interface and also pass the class reference to the binder.
   
    public class TestClass implements ICallback {
       
        public TestClass () {
            new CallbackBinder ().bindCallback (this);
        }
       
        //you have to define the notify method and do the work as you want.
        public void notifyChange () {
           
        }   
    }
   
    The notityChange method will be fired when the callbackBinder execute the notifyCallBack method.
   
    We can also handle multiple classes objects with the same property. Suppose a single property has to bind with multiple classes objects and on changing the value of that property we need to call all the class objects. To accomplish this we can maintain a map which will contain the property as the key and the list of ICallback objects as value. We can make the CallbackBinder class as singleton to bind all the class objects to the same instance of the binder class.
        
    private Map<String, List<ICallback>> propertyCallbackMap = new HashMap<String, List<ICallback>>();

    when the property value is changed just get the object list and call notifyChange on them.

Thanks


For free computer engineering programming books you can visit:
Free Computer Books

Wednesday, January 26, 2011

Hiding columns in jTable using swing:


   Hiding column in jTable is not same as hiding a button or any jComponent. Column in jTable is of type TableColumn. TableCoulmn does not extend JComponent and so we cannot hide a column in jTable just by setting visibilty false,i.e., tableColumn.setVisible(false) will not work in case of jTable column. But we can hide a column in jTable by the following two easy way.

Method 1:
    We can set the width of the column to zero to make it visible false. And to make it visible again, we have to set the size again. We need to set the min,max and preferred size.


    To hide the column:
        jTable1.getColumnModel().getColumn(index).setMaxWidth(0);
        jTable1.getColumnModel().getColumn(index).setMinWidth(0);
        jTable1.getColumnModel().getColumn(index).setPreferredWidth(0);
   

   To make the column visible again:
      jTable1.getColumnModel().getColumn(index).setMaxWidth(WIDTH_TO_SET);
      jTable1.getColumnModel().getColumn(index).setMinWidth(WIDTH_TO_SET);
      jTable1.getColumnModel().getColumn(index).setPreferredWidth(WIDTH_TO_SET);
   

index: index of the column to hide.

Replace WIDTH_TO_SET with the size you want.

Method 2:
    TableColumnModel contains all the column information of the jTable. By removing the coulmn from table column model we can make it hidden, and to make it visible we need to add it again. But when we add a column to the TableColumnModel it appends the column to the existing ones. This means the coulmn will be added to the right side of the last coulmn of the table. So after adding column to the tableColumnModel we need to move the column to its previous index to make it look the same as it was before hidding the column. To accomplish this we have save the reference of the removed column and its index before hidding so that when we need to show the same column again we can add the same reference to the tableColumnModel and also move the column to its original position.


    Mehtod to remove column:
        /*save the column if we need to make it visible later.
          You can also make a custom class which will contains the tableColumn   object and also the original index. Maintain a list of the objects of this custom class. This list will contain all the column that are made hidden.
        */
        //to simply hide second column.  
        TableColumn col = jTable1.getColumnModel().getColumn(1);
        int index = 1;
        jTable1.getColumnModel().removeColumn(jTable1.getColumnModel().getColumn(1));   
To make the column visible.
        /* If you have written your own class for saving removed columns then just    get that coulmn object and index and add it accordingly.
        */
        //simply to make second column visible.

    //Get the column object for the list or map maintained for the removed columns.
        TableColumn col = getColumnObejct();
        jTable1.getColumnModel().addColumn(col); //add it to the righmost side.
       //move the column to the index 1.        
    jTable1.getColumnModel().moveColumn(jTable1.getColumnModel().getColumnCount()-1, 1);
  

For free computer engineering JAVA programming books you can visit:
Free Computer Books