Limiting the number of characters in a JTable cell.

  • Follow


Hi,
   I have a JTable wherein each cell can have only a fixed number of
characters.The number of characters allowed differs for each
cell.Suppose number of characters allowed for a cell is 3.once the
user types the fourth character i need to validate the data entered by
the user and if it is correct i need to move the cursor automatically
to the next cell.
I have my own celleditor where I have handled the key typed event on
my JTextField.I am not able to figure out how to filter out the keys
which do not type any character for e.g. Backspace.I need to allow
these even if the maximum allowable length of a cell is reached.The
user can also navigate to the next cell using the Enter key.
The code snippet for cell editor is :--
*****************************************************
    public class DEVTableCellEditor
        extends AbstractCellEditor
        implements TableCellEditor
    {
        // This is the component that will handle the editing of the
        // cell value
         DEVTextField lComponent = new DEVTextField();


        // This method is called when a cell value is edited by the
user.
        public Component getTableCellEditorComponent(...)
        {
            final JTable lTable = pTable;
            final int lRowIndex=pRowIndex;
            final int lColIndex=pColIndex;
 

lComponent.addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyTyped(KeyEvent pEvent)
{
String lValue = lComponent.getText();       
 if (lComponent.validateLength(pEvent , lValue , lRowIndex ,
lColIndex))
    {
    int lRow = tblDEVEntry.getSelectedRow();
    int lCol = tblDEVEntry.getSelectedColumn();
    //if not on last row
    if (lRow != mTotalRows)
    {
     validateDataAndAct(lValue , lRow , lCol);
    } //last row
    else
    {
     validateDataAndActLastRow(lValue , lRow );
    }
     }
      }
       });

//handle the enter key event
KeyStroke lEnterKeyStroke =
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false);
                Action lEnterAction = new AbstractAction()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        int lRow = tblDEVEntry.getSelectedRow();
                        int lCol = tblDEVEntry.getSelectedColumn();
                        String lValue = lComponent.getText();

                        //if not on last row
                        if (lRow != mTotalRows)
                        {
                            validateDataAndAct(lValue , lRow , lCol);
                        } //last row
                        else
                        {
                            validateDataAndActLastRow(lValue , lRow );
                        }
                            mDataModel.setIdentifier(true);
                    }
                };
lTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put
(lEnterKeyStroke , "ENTER");
                lTable.getActionMap().put("ENTER" , lEnterAction);

             lComponent.setText( (String) pValue);

            // Return the configured component
            return lComponent;
        }


        // This method is called when editing is completed.
        // It must return the new value to be stored in the cell.
        public Object getCellEditorValue()
        {
            return  lComponent.getText();
        }
    }
*****************************************************************
code snippet for my custom text field is-----

    public class DEVTextField
        extends JTextField 
    {
  
public boolean validateLength(KeyEvent pEvent , String pValue , int
pRow , int pCol)
        {
            if (pValue.length() >= 2)
            {
                 pEvent.consume();
                return true;
            }
            else
            {
                return false;
            }
        }
    }

}
******************************************************************
I tried getting the keycode() inside my keyTyped event but it always
gives a 0 value.I am confused as to why this is happening?And any way
to filter out the keystrokes which do no cause a character to be
typed?

Thanks in Advance
Ananya
0
Reply ananya 9/30/2003 5:34:57 PM

You could derive a class from, PlainDocument and override 
    public void insertString(int index, String s, AttributeSet a)
throws BadLocationException
And set your derived class to be the document of your JTextField.

Do your validation in insertString. If you are happy with the
character or characters in 's' then call the base class method. To get
the complete string you'll need to put s into the current document
text at the supplied index. This works for pasted chars as well as
typed chars.

HTH 

Steve
0
Reply google 10/1/2003 3:18:17 PM


1 Replies
860 Views

(page loaded in 0.047 seconds)

Similiar Articles:













7/29/2012 8:54:34 AM


Reply: