• Post Reply Bookmark Topic Watch Topic
  • New Topic

super int constraint  RSS feed

 
vinnie timoney
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm new to this forum so I hope someone can help me out. I have a java GUI and two classes IntConstraint.java and IntConstraintOptional.java which I will paste below; now the IntConstraintOptional class is used for fields in the GUI which are optional and I was wondering if there was a way to restrict an operator from going over the max integer value of 2147483647 without introducing longs etc. which would be a huge and difficult impact on the overall GUI. Could I use strings or regular expressions to fix this? see, if an operator enters a huge number, say 382648392649823648298374982 into an IntConstraintOptional field, the GUI continues on; i'd like it to stop and default the field back to zero. below are the files, any help would be appreciated.
thannks,
vinnie
############################################################################
public class IntConstraint implements IConstraint {
private int iFrom = Integer.MIN_VALUE;
private int iTo = Integer.MAX_VALUE;
private int iValue = 0;
private ILanguageBandle lBundle = null;
public IntConstraint( ILanguageBandle lBundle ) {
Trace.enter( this, "IntConstraint" );
this.lBundle = lBundle;
Trace.exit( this );
}
public IntConstraint( int iFrom, int iTo, ILanguageBandle lBundle ) {
Trace.enter( this, "IntConstraint" );
this.iFrom = iFrom;
this.iTo = iTo;
this.lBundle = lBundle;
Trace.exit( this );
}
public int getValue() {
Trace.enter( this, "getValue" );
return this.iValue;
}
public boolean isValueValid( java.lang.Object objValue ) {
Trace.enter( this, "isValueValid" );
if ( objValue instanceof Integer ) {
Trace.exit( this );
return checkInterval( ((Integer)objValue).intValue() );
}
try {
Trace.exit( this );
return checkInterval( Integer.parseInt( (java.lang.String)objValue ) );
}
catch ( java.lang.NumberFormatException nfEx ) {
Trace.exit( this );
return false;
}
}
private boolean checkInterval( int iTarget ) {
Trace.enter( this, "checkInterval" );
iValue = iTarget;
if ( iTarget >= iFrom && iTarget <= iTo ) {
Trace.exit( this );
return true;
}
else {
Trace.exit( this );
return false;
}
}
public java.lang.String getDescription() {
Trace.enter( this, "getDescription" );
// if ( iFrom == Integer.MIN_VALUE || iTo == Integer.MAX_VALUE ) {
// Trace.exit( this );
// return lBundle.getLocalizedString("CONSTRAINT_INT_FORMAT");
// }
// else {
Trace.exit( this );
return lBundle.getLocalizedString( "CONSTRAINT_INT_INTERVAL", new Object[]{ new Integer(iFrom) , new Integer(iTo) } );
// }
}
}
############################################################################
public class IntConstraintOptional extends IntConstraint {
public IntConstraintOptional( ILanguageBandle lBundle ) {
super( lBundle );
}
public IntConstraintOptional( int iFrom, int iTo, ILanguageBandle lBundle ) {
super( iFrom, iTo, lBundle );
}
public boolean isValueValid( java.lang.Object objValue ) {
if ( objValue == null ) return true;
return super.isValueValid( objValue );
}
}
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what you need is something in the GUI controller class that gets the value from the input field and compares it to the maximum allowed value. This sounds relatively simple, or am I missing something?
I'm not sure how the GUI continues if the user enteres too large a value - I would expect Java to throw an error when you try to put too large a value into the int variable??? Perhaps you just need to try{}catch{} for this error and default the field to zero if the error turns up due to bad input.
i think the error would be a java.lang.NumberFormatException
[ March 23, 2004: Message edited by: Ben Wood ]
 
vinnie timoney
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats more or less what I want it to do; but the problem is that the GUI continues when the value inputted is out of integer range; anything below 2147483647 is fine but i'm looking for a solution without changing ints to longs.
thanks
vinnie
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case could you attach a listener to the textfield that picks up what the user is entering and if they enter too large a value it will just default to the max int value instead.
 
vinnie timoney
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could I put that in the IntConstraintOptional class? what would the code look like?
thanks
vinnie
 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the method I used to solve a similar problem, if you add a listener to the field and call this method for each key event
protected void processComponentKeyEvent( KeyEvent event )
{
if( event.getID() == KeyEvent.KEY_PRESSED ) // must look for KEY_PRESSED as KEY_TYPED is consumed for editing keys
{
switch( event.getKeyCode() )
{
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
case KeyEvent.VK_LEFT: // ignore keys that don't change (editor keys)
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_HOME:
case KeyEvent.VK_END:
break;
case KeyEvent.VK_ENTER: // action only fired if valid
case KeyEvent.VK_ACCEPT:
if( !validateField() ) // you would supply the validateField method ... check the field length.. attmept to parse the value to an int return false if it fails
{
event.consume();
return;
}
break;
default: // all other keys will set myself dirty
dirty = true;
break;
}
} super.processComponentKeyEvent( event );
}
 
vinnie timoney
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code; it worked brilliantly! cheers. unfortunately, my requireents have now changed and ill be doing all the checking using SAXParser exceptions; but no knowledge is a waste I guess!
thanks,
vt
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!