4

\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 automatically removes all letters and characters from the JTextField.

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! It's amazing how much I have learned today. I've been coding 13 hours straight.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
David Tunnell
  • 7,252
  • 20
  • 66
  • 124
  • 2
    Google tutorials on regular expressions. A starting point: [Oracle Regular Expressions Tutorial](http://docs.oracle.com/javase/tutorial/essential/regex/). My favorite: [Regular Expressions Tutorial](http://www.regular-expressions.info/tutorial.html). Start from the beginning as it's pretty intense stuff. – Hovercraft Full Of Eels Aug 07 '12 at 03:09
  • That's some funky code you have there. Generally you should call the matching method in `FilterBypass`, and you probably also want to override `insertString`. – Tom Hawtin - tackline Aug 07 '12 at 04:52
  • @David Tunnell Thank you so much for this topic – Honinbo Shusaku Mar 17 '14 at 22:23

1 Answers1

8

The replaceAll function takes in a regular expression. You can learn a bit about regular expressions from many tutorials online (see @Hovercraft Full Of Eels comment) or directly from the Java api: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Essentially you can put together any of the regular expression constructs (listed in the above link) together to form a regular expression. If you for instance wanted to ensure that only 0-9 and . are allowed, you can use:

text.replaceAll("[^0-9.]", "")
Nick Rippe
  • 6,465
  • 14
  • 30
  • I will read the tutorials. Thanks for all of the info the program is working now. – David Tunnell Aug 07 '12 at 13:12
  • I'm having trouble achieving a reliable `DocumentFilter` solution for similar problems. I need the input to follow a regex, but using a `replaceAll()` or similar (using `Matcher` to accumulate matches and remove non-matches) is rpoving painfully unreliable. --- For example, this answer's solution will go very bad as soon as the user inputs a second `'.'` in the `TextComponent`, as it breaks the decimal consistency. – CosmicGiant Nov 29 '12 at 15:26
  • @TheLima - your question is slightly different. It'd be beneficial to post it as it's own question (with a reference here), that way I could give you a full answer. But basically you want a more advanced Filter that senses whether the decimal has been used before. Just put a boolean field in the DocumentFilter which determines if the regex used for validation allows the period or not. – Nick Rippe Nov 29 '12 at 16:07
  • @NickRippe - The link is [here](http://stackoverflow.com/questions/13610320/how-to-make-jtextfield-or-jformattedtextfield-accept-input-only-if-it-matches-a). My problem is related but not exactly the same as the decimal-point consistency, but it's something I *also* don't know how to do and that might help solve the problem. --- It might be a better idea to post the decimal-point consistency answer here as an edit, rather than there though. – CosmicGiant Nov 30 '12 at 18:08