-
Great code BUT HOW DOES IT WORK ??? Plz help
/* This code implements the JTextfield from Java
It makes it possible to add 2 things to the JTextField
1. True or false (when True you can only type numbers in the textfield)
2. a number to determinate the lenght of char. you can type in this textfield.
expl.
private JTextFieldVeredeling operatorVeld= new JTextFieldVeredeling(4, true);
BUT HOW does this work ? (and it really works great !!)
I have to explain this to a Jury tomorrow ....
*/
package Veredeling_main;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.text.*;
//-----------------------------------------------------------------------------------
public class JTextFieldVeredeling extends JTextField {
//-----------------------------------------------------------------------------------
private int lengte;
private boolean alleenCijfers;
//-----------------------------------------------------------------------------------
public JTextFieldVeredeling() {
//-----------------------------------------------------------------------------------
super();
setEigenschappen(); //bepaalt de kader en lettertype van JTextField
setMaxLengte( Integer.MAX_VALUE );
setCijfers( false );
}
//-----------------------------------------------------------------------------------
public JTextFieldVeredeling(boolean alleenCijfers) {
//-----------------------------------------------------------------------------------
super();
setEigenschappen();
setMaxLengte( Integer.MAX_VALUE );
setCijfers( alleenCijfers );
}
//-----------------------------------------------------------------------------------
public JTextFieldVeredeling( int lengte, boolean alleenCijfers ) {
//-----------------------------------------------------------------------------------
super();
setEigenschappen();
setMaxLengte( lengte );
setCijfers( alleenCijfers );
}
//-----------------------------------------------------------------------------------
public JTextFieldVeredeling( int lengte ) {
//-----------------------------------------------------------------------------------
super();
setEigenschappen();
setMaxLengte( lengte );
setCijfers( false );
}
//-----------------------------------------------------------------------------------
public void setMaxLengte(int lengte ) //max lengte
//-----------------------------------------------------------------------------------
{ this.lengte = lengte; }
//-----------------------------------------------------------------------------------
public void setCijfers(boolean alleenCijfers ) //alleen cijfers true of false
//-----------------------------------------------------------------------------------
{ this.alleenCijfers = alleenCijfers; }
//-----------------------------------------------------------------------------------
protected void setEigenschappen() { //eigenschappen van JTextField
//-----------------------------------------------------------------------------------
Border border1 = BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder
(Color.white,new Color(178, 178, 165)),BorderFactory.createEmptyBorder(0,2,0,0));
this.setBorder( border1 );
this.setFont(new java.awt.Font("Dialog", 0, 13));
}
//-----------------------------------------------------------------------------------
protected Document createDefaultModel() { //als er geen max lengte is doorgegeven
//-----------------------------------------------------------------------------------
return new MaxLengteDocument( this );
}
//-----------------------------------------------------------------------------------
class MaxLengteDocument extends PlainDocument {
//-----------------------------------------------------------------------------------
JTextFieldVeredeling jt;
public MaxLengteDocument( JTextFieldVeredeling jt )
{ this.jt = jt; }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
try {
if ( !str.equals("") )
{
if (alleenCijfers)
{ int ab = Integer.parseInt( str ); }
String s = jt.getText()+str;
if ( s.length() > lengte ) {
super.insertString(offs, "", a);
} else {
super.insertString(offs, str, a);
}
}
}
catch (NumberFormatException ex)
{
}
}
}
}
-
(edit your post to use [ code] tags)
It creates a custom Docuement (the data model for text stuff) for the textfield, MaxLengteDocuement:
Code:
protected Document createDefaultModel() { //als er geen max lengte is doorgegeven
//-----------------------------------------------------------------------------------
return new MaxLengteDocument( this );
}
This does all the work, the outter custom JTextField just has extra variables that are set.
Code:
private int lengte;
private boolean alleenCijfers;
The above variables determine the character limit, and whether to use numbers only. MaxLengteDocuement checks these variables:
Code:
class MaxLengteDocument extends PlainDocument {
//-----------------------------------------------------------------------------------
JTextFieldVeredeling jt;
public MaxLengteDocument( JTextFieldVeredeling jt )
{ this.jt = jt; }
//all the action happens here
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
try { //for the number format exception
if ( !str.equals("") )
{
if (alleenCijfers) //if numbers only
//parses a number.
//if str isn't a number, then it throws a NumberFormatException,
//and execution will jump to 'catch(NumberformatException ex)',
//skipping the code that inserts the text
{ int ab = Integer.parseInt( str ); }
//this code adds the text
//add up all the text that is to be in the textfield
String s = jt.getText()+str;
//check if it doesn't exceed the character limit
if ( s.length() > lengte ) {
//if it does exceed, add a blank ""
super.insertString(offs, "", a);
} else {
//if it doesn't exceed the limit, insert it
super.insertString(offs, str, a);
}
}
}
//execution will jump here if a non-number is entered.
//All text insertion code will be skipped
catch (NumberFormatException ex)
{
}
}
}
BTW, what do you mean by 'Jury' ?
-
he means that tomorrow, he has an exam, couldnt figure out this question, so he thought he would ask for an answer on the internet so he could make it look like he knew what he was talking about..
</cynic>
-
Reply to jcard
Yes you are so right jcard....
I looked it up on the api files and the internet and I knew what it did, and worked great ! But I had to explain every bit of code i've used, so I had to know WHY it worked. And I do now...hope you do to.
Some times it 's better and easier to re-use great code
and learn from others... that's the point of learing...
It's is stupid to re-invent sturcture and code that allready exists. You better work-out new stuff and so help others that come next.
I did my exames and all worked great thanks to people
how are willing to help beginners like me....
so great comment jcard....
btw. you helped me in the past, so i'll forget you silly comments... Thx .
-
oyah totally agree.. i should have put a smily in there or soemthing, but hopefully you realise i wasnt being totally serious because I wrote </cynic> at the end of the message
-
Thx again Jcard
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|