1

Working on my 1st program. I figured out how to identify characters I do not want to be able to be inputted. I would like to know how do delete the last character entered so that from a user perspectvive it will appear as only numbers can be entered.

@Override
public void keyPressed(KeyEvent e) {
    char keyChar = e.getKeyChar();;
    char[] badCharArray = "abcdefghijklmnopqrstuvwxyz-`~!@#$%^&*()[]{}<>_+=|\"':;?/ ".toCharArray();
        for (int i = 0; i < badCharArray.length; i++) {
            if (badCharArray[i] == keyChar) {
                System.out.print(badCharArray[i] + " bad\n");
                hourlyWageInput.setBackground(Color.RED);
                } 
            }
        }

Thanks. enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
David Tunnell
  • 7,252
  • 20
  • 66
  • 124

4 Answers4

4

The easiest way to do this is simply to use a JFormattedTextField instead of a JTextField.

NumberFormat format = NumberFormat.getNumberInstance();
format.setMinimumFractionDigits(2); // eventually maximum fraction digits

JFormattedTextField fld = new JFormattedTextField(format);
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
4

You should avoid keyListeners in this case. What happens if the user pastes text into field. The keyListener will not be notified, allowing invalid characters to be inserted.

You're better of using DocumentFilter to filter out any invalid characters you don't want.

Take a look at Implementing a DocumentFilter for more information and DocumentFilter for some nice examples

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • The key listener does actually detect ctrl + v, however, if he adds a right-click menu, yes, it would fail there, but by default that is not on a JTextField. – Alex Coleman Aug 06 '12 at 22:53
  • @Alex: this is the better answer by far. 1+. DocumentFilters will allow modification entered data *before* it is displayed, before it is validated. – Hovercraft Full Of Eels Aug 06 '12 at 22:54
  • @HovercraftFullOfEels Yes, I just said that this would fail for a certain case (that he doesnt have); however, 99% of the time mine will still work. No need to downvote it... – Alex Coleman Aug 06 '12 at 22:55
  • @Alex: You should not condone use of KeyListeners for Swing applications as they really should be avoided. Yes, they're a quick and "easy" solution, but they're too low level -- especially when more robust higher level solutions are available. Again, they fail anything to do with copy-paste, they fail for alternative input devices, they're just wrong in this situation. Period. – Hovercraft Full Of Eels Aug 06 '12 at 22:57
  • 1
    @AlexColeman what about `setText` ? `KeyListener` will not be notified of this at all? I agree, the question was about a `KeyListener` implementation, but we should encourage good practices as well, I also feel into the trap of just answering the question and while you're solution works, it is prone to Document mutation exceptions – MadProgrammer Aug 06 '12 at 22:59
  • As an aside, @David really should use a JTable with custom editors and renderers as has been suggested previously. – Hovercraft Full Of Eels Aug 06 '12 at 23:04
  • Thanks for all of the help. You guys rule. – David Tunnell Aug 07 '12 at 00:48
1

Simply set it's text to be the current text, without the character before the caret position


    @Override
    public void keyReleased(KeyEvent paramKeyEvent) {
        char keyChar = paramKeyEvent.getKeyChar();
        char[] badCharArray = "abcdefghijklmnopqrstuvwxyz-`~!@#$%^&*()[]{}<>_+=|\"':;?/ ".toCharArray();
            for (int i = 0; i < badCharArray.length; i++) {
                if (badCharArray[i] == keyChar) {
                    int caret = field.getCaretPosition()-1;
                    System.out.print(badCharArray[i] + " bad\n");
                    field.setText(field.getText().substring(0, caret) + 
                            field.getText().substring(caret+1));
                    field.setCaretPosition(caret);
                    } 
            }
    }

Took out the setting it to red, as if you're going to auto-correct it, no point in having that there (maybe set it to show red for one second?)


Actually, the only issue with that is that if they hold in an invalid key, it only deletes the last one; the following should work a lot better, and won't shift the caret :P

        boolean held = false;
        private int oldCaret = 1;
        @Override
        public void keyPressed(KeyEvent e) {
            if(! held) {
                oldCaret=field.getCaretPosition();
                System.out.println(oldCaret);
            }
            held = true;

        }
        @Override
        public void keyReleased(KeyEvent paramKeyEvent) {
            char keyChar = paramKeyEvent.getKeyChar();
            char[] badCharArray = "abcdefghijklmnopqrstuvwxyz-`~!@#$%^&*()[]{}<>_+=|\"':;?/ ".toCharArray();
            for (int i = 0; i < badCharArray.length; i++) {
                if(field.getText().contains(badCharArray[i]+"")) {
                    field.setText(field.getText().replace(badCharArray[i]+"", ""));
                    field.setCaretPosition(oldCaret);
                }
            }

            held=false;
        }
Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • @DavidTunnell: this is not the correct answer, and you do not want to use a KeyListener. The correct and more robust answer is to use a DocumentFilter as per MadProgrammer's answer. It can handle copy and paste as well as other non-keyboard input. – Hovercraft Full Of Eels Aug 06 '12 at 22:54
  • @DavidTunnell Btw, you need to add in capital characters to that char[], A-Z, not just a-z – Alex Coleman Aug 06 '12 at 22:54
1

Here is the creation of the JTextField:

hourlyWageInput = new JTextField("7.25");
DocumentFilter filter = new UppercaseDocumentFilter();
((AbstractDocument) hourlyWageInput.getDocument()).setDocumentFilter(filter);
hourlyWageInput.setHorizontalAlignment(JTextField.CENTER);
add(hourlyWageInput);

Here is my DocumentFilter:

import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class UppercaseDocumentFilter extends DocumentFilter {

     public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
          String text, javax.swing.text.AttributeSet attr)

          throws BadLocationException {
               fb.insertString(offset, text.replaceAll("\\D", ""), attr);   
     }
}

This works, automatically removing all letters and characters from the JTextField. So thank you, this is the best for sure.

However, I was wondering if anyone knows of a place with all of the commands similar to "\D". It took me a while to find the right information.

Also, the code I have now also prevents . from being types which I need as I am working with doubles. Any ideas?

Thanks for all of the help, its amazing how much I have learned today. Been coding 13 hours straight.

David Tunnell
  • 7,252
  • 20
  • 66
  • 124