how to

Reply

Join Date: Aug 2006
Posts: 4
Reputation: shapeshifter is an unknown quantity at this point 
Solved Threads: 0
shapeshifter shapeshifter is offline Offline
Newbie Poster

how to

 
0
  #1
Aug 3rd, 2006
i have a feild where the user must insert only integer values not chars..if they insert a char an error messege should appear..how can i do this and what is the code to do it
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to

 
0
  #2
Aug 3rd, 2006
You can test this using a loop which goes through the numbers:

"0123456789"

If this condition is valid then proceed to the next part of the program. For this you can use a key listener.
Last edited by iamthwee; Aug 3rd, 2006 at 9:40 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 4
Reputation: shapeshifter is an unknown quantity at this point 
Solved Threads: 0
shapeshifter shapeshifter is offline Offline
Newbie Poster

Re: how to

 
0
  #3
Aug 3rd, 2006
actually how do i write the code..i dont know anything abt keylister i am a newbie for java
can u give me a sample code
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to

 
0
  #4
Aug 4th, 2006
Originally Posted by shapeshifter
actually how do i write the code..i dont know anything abt keylister i am a newbie for java
can u give me a sample code
Something like this?

KeyView.java
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4.  
  5. class KeyView extends JFrame implements KeyListener {
  6. JTextField keyText = new JTextField(80);
  7. JLabel keyLabel = new JLabel("Type in a number mo fo.");
  8.  
  9. KeyView() {
  10. super("KeyView");
  11. setSize(350, 100);
  12. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. keyText.addKeyListener(this);
  14. Container pane = getContentPane();
  15. BorderLayout bord = new BorderLayout();
  16. pane.add(keyLabel, BorderLayout.NORTH);
  17. pane.add(keyText, BorderLayout.CENTER);
  18. setContentPane(pane);
  19. setVisible(true);
  20. }
  21.  
  22. public void keyTyped(KeyEvent input) {
  23. char key = input.getKeyChar();
  24. if (key >='0'&& key <='9')
  25. {
  26. keyLabel.setText("You pressed " + key);
  27. }
  28. else
  29. keyLabel.setText("I said press a freaking number?");
  30. }
  31.  
  32. public void keyPressed(KeyEvent txt) {
  33. // do nothing
  34. }
  35.  
  36. public void keyReleased(KeyEvent txt) {
  37. // do nothing
  38. }
  39.  
  40. public static void main(String[] arguments) {
  41.  
  42. try {
  43. UIManager.setLookAndFeel(
  44. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
  45.  
  46.  
  47. } catch (Exception e) { }
  48.  
  49. new KeyView();
  50. }
  51. }

Or you could wait till the user enters the entire expression and parse it at the end?
Last edited by iamthwee; Aug 4th, 2006 at 12:31 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: how to

 
0
  #5
Aug 6th, 2006
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 8
Reputation: b2daj is an unknown quantity at this point 
Solved Threads: 0
b2daj b2daj is offline Offline
Newbie Poster

Re: how to

 
0
  #6
Aug 9th, 2006
KeyListener will do what exactly what you wanna do.
Last edited by b2daj; Aug 9th, 2006 at 4:29 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 1
Reputation: jennifer_netto is an unknown quantity at this point 
Solved Threads: 0
jennifer_netto jennifer_netto is offline Offline
Newbie Poster

Re: how to

 
0
  #7
Aug 17th, 2006
hai, check this code and hope this helps u.

/*in ur main program where u have ur textfield, declare the TextField as follows*/

JTextField txtInput =username = new JTextField(new FixedNumericDocument(4,true),"",15);

/*where 4 is the maximum number of digits that is allowed, boolean true denotes whether u need the text box to accept only integers and false otherwise, and 15 is the size of the text field.*/

/* Then as a seperate class call the following code*/

///////FixedNumericDocument.java////////
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class FixedNumericDocument extends PlainDocument {
private int maxLength = 9999;
private boolean numericOnly;
public FixedNumericDocument(int maxLength, boolean numericOnly) {
super();
this.maxLength = maxLength;
this.numericOnly = numericOnly;
}
//this is where we'll control all input to our document.
//If the text that is being entered passes our criteria, then we'll just call
//super.insertString(...)
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (getLength() + str.length() > maxLength) {
Toolkit.getDefaultToolkit().beep();
return;
}
else {
try {
if (numericOnly) {
//check if str is numeric only
Integer.parseInt(str);
//if we get here then str contains only numbers
//so it's ok to insert
}
super.insertString(offset, str, attr);
}
catch(NumberFormatException exp) {
Toolkit.getDefaultToolkit().beep();
return;
}
}
return;
}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC