Hi all
In my Swing application i am using Jtextfields.If Jtextfield have empty string("") i need to set it to 0 always .Is there any easy way to set this without if else comparison.
Thanks
Nimish
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi all
In my Swing application i am using Jtextfields.If Jtextfield have empty string("") i need to set it to 0 always .Is there any easy way to set this without if else comparison.
Thanks
Nimish
Why dont you try JFormattedTextField instead?
hi relixus
i am newbie here. Can u give me some example how to use JFormattedTextfield for this.
Thanks
Why can't you use if/else?
Just to clarify, if the text is deleted out of the textfield, you want it to automatically update to 0?
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Hello PF
Really thats i want .. I want to automatically updated into 0.
Thanks
Does this need to check in real-time? When the user is typing/deleting text from the field or does it check when a button is pressed?
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Hi PF
i need to check in when the user is typing/deleting. Plz help me to find a solution ,,Thanks
Have a play with this code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class TextValidation { /** * JavaProgrammingForums.com */ public static MyTextField t = new MyTextField(16); public static void main(String[] a) { JFrame f = new JFrame("JAVA FORUMS"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //MyTextField t = new MyTextField(16); t.setText("0"); f.getContentPane().add(t, BorderLayout.CENTER); f.pack(); f.setVisible(true); } private static class MyTextField extends JTextField implements ActionListener, DocumentListener { public MyTextField(int l) { super(l); addActionListener(this); Document doc = this.getDocument(); doc.addDocumentListener(this); } //Enter key pressed public void actionPerformed(ActionEvent e) { //System.out.println(getText()); } //Text inserted public void insertUpdate(DocumentEvent e) { //System.out.println(getText()); } //Text deleted public void removeUpdate(DocumentEvent e) { //System.out.println(getText()); if(getText().equals("")){ System.out.println("IM BLANK!"); } } public void changedUpdate(DocumentEvent e) { //System.out.println(getText()); } } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
public void removeUpdate(DocumentEvent e) { //System.out.println(getText()); if(getText().equals("")){ setText("0"); } }
I tried the above .But i got Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification.I Google it. Using DoucmentFilter may slove the problem.
public class Doc extends DocumentFilter { public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { super.insertString(fb, offset, string, attr); } public void remove(FilterBypass fb, int offset, int length) throws BadLocationException { // super.remove(fb, offset, length); if(fb.getDocument().getLength()==0) { insertString(fb, offset, "0", null); } } public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { // super.replace(fb, offset, length, text, attrs); if(fb.getDocument().getText(0, 1).equals("0")) { remove(fb, 0, 1); } } }
But dont know it's the exact way of doing this .Plz let me know if u have any suggestions.
Thanks