• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extending JTextField  RSS feed

 
Patrick Girard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ...i wanna extend a class from JTextField in order to check every character that is typed because my textfield contents is inserted in a database so when someone inserts a (') it raises a database error ...the only way i found to avoid this is to put another (') so showing one (') in the textfield but inserting ('') in the database ....but i dont wanna parse my textfield content directly in my other classes every time that is why i want a Extended class that would do the checking in the background ...maybe I'm far from the solution but here is my class....
<HTML>
<BODY>
<H4>
import javax.swing.JTextField;
import java.awt.event.*;
/**
* A Class class.
*

* @author
*/
public class MptTextField extends JTextField {
private String sContent;
/**
* Constructor
*/
public MptTextField() {
super();
sContent = "";
addListeners();
}
public void setText(String newContent){
sContent = newContent;
}
public String getText(){
return sContent;
}
private void addListeners(){
this.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
sContent += String.valueOf(e.getKeyChar());
//MptMessage.showErrorMsg(MptMainFrame.frame, "allo");
}
});
}
}
</H4>
</BODY>
</HTML>
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick,
When you start extending Swing components you should make use of the document listener instead of looking for key strokes. If you do that then you can easily make any TextField for any new application. Start with the following class.

We can see that if we want a certain textfield then we just need to create the document model that we want (shown above in bold). One such model is shown below.

Now just think of the possibilities given someone with your talents. Why, this time next week you might have a whole suite of validating textfields!
Enjoy,
Manfred.
 
Patrick Girard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help...didn't really have the time yet to look in
your code but I appreciate you taking the time to answer.
 
Patrick Girard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally ...writing my question answered my question!Even if the code you gave me is not going to help me now it sure will in the future ..but since our application as a Database Back-end which means we have a Database class...every insert goes in this class
so the parsing is only done in one class wich reduces the time and effort!
For example the method insert(string tablename , hashtable values)
the values are strings that i can clean upon reception of the hahstable in the function...
Thanx anywayzz
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!