Results 1 to 5 of 5

Thread: How to write a wrapper around the JTextField class?

  1. #1
    Join Date
    Nov 2009
    Posts
    47

    How to write a wrapper around the JTextField class?

    Hi friends,
    I am last year Computer Science student. As part my syllabus I am working on live project where I am using java as front end and SQL as back end. I want to write a wrapper around the JTextField class for some more functionality such as filtering out alphabetic, filtering out alphabetic special characters which are of fixed length. Can anyone help me in this case.
    Thanks in advanced.
    Last edited by MAHAH; 12-02-2010 at 06:39 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to write a wrapper around the JTextField class?

    Hey it is very easy to write a wrapper around the JTextField class. I have written following code for you. In the following code I have use subclass JTextField and Document Model to do this. I also have use UpperCaseFieldEg class to do this. It is very simple program.

    Code:
    public class UpperCaseFieldEg extends JTextFieldeg {
    
    public UpperCaseFieldEg(int cols1) {
    
    super(cols1);
    
    }
    
    protected Document createDefaultModelEg() {
    
    return new UpperCaseDocumentEg();
    
    }
    
    static class UpperCaseDocumentEg extends PlainDocumentEg {
    
    public void insertString(int offss, String strs, AttributeSet as)
    
    throws BadLocationExceptionEg {
    
    if (strs == null) {
    
    return;
    
    }
    
    char[] upper = strs.toCharArray();
    
    for (int k = 0; k < upper.length; k++) {
    
    upper[k] = Character.toUpperCase(upper[k]);
    
    }
    
    super.insertString(offss, new String(uppers), as);
    
    }
    
    }
    
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to write a wrapper around the JTextField class?

    You have to use override method to write a wrapper around the JTextField class. It is very simple program to understand. I have written following code for you. In this code I have override the propertychange() method of JTextField class to get proper result. I also have modify code so that if I type characters more than the specified length then it will gives error message.
    Code:
     protected Document createDefaultModelEg() {
    
     return new UpperCaseDocumentEg();
    
    }
    
    
    
     static class UpperCaseDocumentEg extends
    
     tends PlainDocumentEg {
    
    
    
     public void insertString(int offss, String
    
     ss, String strs, AttributeSet as)
    
     throws BadLocationExceptionEg {
    
    
    
     if (strs == null) {
    
     return;
    
     }
    
     char[] uppers = strs.toCharArrays();
    
     for (int k = 0; k < upper.length; k++) {
    
     upper[k] = Character.toUpperCase(upper[k]);
    
     }
    
     super.insertString(offss, new
    
     ngs(offs, new String(uppers), as);
    
     }}
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to write a wrapper around the JTextField class?

    It is very easy process to write a wrapper around the JTextField class. I also have written following program for you. In the following program I have use javax.swing, java.awt.event and javax.swing.text. class to get correct output. Just try to understand following example.

    Code:
    import javax.swing.*;
    
    import java.awt.event.*;
    
    import javax.swing.text.*;
    
    import java.awt.*;
    
    public class NfieldEg extends JTextFieldEg
    
    {
    
    public NfieldEg()
    
    {
    
    super(1Eg);
    
    }
    
    protected DocumentEg createDefaultModelEg()
    
    {
    
    return new EngDocumentEg();
    
    }
    
    public class EngDocumentEg extends PlainDocumentEG
    
    {
    
    public void insertString(int offss, String strs, AttributeSet as)
    
    throws BadLocationExceptions
    
    {
    
    for (int k=0 ;k < str.length(); k++)
    
    if (Characters.issDigit(strs.charAt(ks)))
    
    super.insertString(offss+k, strs.substring(k,k+1) ,as);
    
    }
    
    }
    
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to write a wrapper around the JTextField class?

    You have to first read JTextField API to get information about wrapper class.
    In the JTextField API it is said that the UpperCaseField example is part of documentation. You also have to read "How to Use Text Fields" to fix this problem. Just try to understand following code.

    Code:
    public void insertString(int offss, String strs, AttributeSet as)
    			           throws BadLocationExceptions 
    {
    	for (int k=0 ;k < str.length(); k++) 
    	   	  if (Character.isDigit(str.charAt(k)))
    	   	 	  supera.insertString(offs+k, str.substring(k,k+1) ,as);

Similar Threads

  1. Custom Background Icon for JTextField?
    By melston in forum Software Development
    Replies: 3
    Last Post: 22-08-2010, 12:02 PM
  2. Display JtextField in Jpanel
    By New ID in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 12:01 PM
  3. Get Date by JTextField
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:11 AM
  4. What is wrapper class in java?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 30-01-2010, 08:40 AM
  5. How Can i achieve the functionality of TextListener for JTextField
    By Gap meer in forum Software Development
    Replies: 4
    Last Post: 25-10-2008, 06:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,024,910.22000 seconds with 16 queries