| | |
how to
![]() |
•
•
•
•
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
KeyView.java
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.event.*; import java.awt.*; class KeyView extends JFrame implements KeyListener { JTextField keyText = new JTextField(80); JLabel keyLabel = new JLabel("Type in a number mo fo."); KeyView() { super("KeyView"); setSize(350, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); keyText.addKeyListener(this); Container pane = getContentPane(); BorderLayout bord = new BorderLayout(); pane.add(keyLabel, BorderLayout.NORTH); pane.add(keyText, BorderLayout.CENTER); setContentPane(pane); setVisible(true); } public void keyTyped(KeyEvent input) { char key = input.getKeyChar(); if (key >='0'&& key <='9') { keyLabel.setText("You pressed " + key); } else keyLabel.setText("I said press a freaking number?"); } public void keyPressed(KeyEvent txt) { // do nothing } public void keyReleased(KeyEvent txt) { // do nothing } public static void main(String[] arguments) { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ); } catch (Exception e) { } new KeyView(); } }
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*
•
•
Join Date: Aug 2006
Posts: 1
Reputation:
Solved Threads: 0
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;
}
}
/*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;
}
}
![]() |
Other Threads in the Java Forum
- Previous Thread: help
- Next Thread: code for detecting usb devices
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql stop string swing testautomation threads transforms tree ui unicode validation windows






