documentfilter

JTextArea only with numbers, but allowing negative values

懵懂的女人 提交于 2019-12-02 00:12:32
问题 I have a JTextArea which only has to accept numbers. This is my code: DocumentFilter onlyNumberFilter = new AxisJTextFilter(); final JTextArea areaTextoXMin = new JTextArea(String.valueOf(xMin)); ((AbstractDocument)areaTextoXMin.getDocument()).setDocumentFilter(onlyNumberFilter); Works OK for positive numbers, but not for negative numbers. How can I fix that? EDIT: Sorry, the AxisJTextFilter was found over the Internet and I forgot about that. Its code is: import javax.swing.text.*; import

Why does the DocumentFilter not give the intended result?

人走茶凉 提交于 2019-12-01 08:25:24
I figure this must be a simple mistake in the code or a misunderstanding on my part, but I cannot get a DocumentFilter to detect insertString events. Below is a simple filter for upper case letters, but that is not as important as the fact that the insertString(..) method never seems to be called! Why is the insertString(..) method of the DocumentFilter not called? The filter is applied to the JTextField at the top. Every time insertString(..) is called, it should append information to the JTextArea in the CENTER . At the moment, there is no action in the text field that causes text to be

Why does the DocumentFilter not give the intended result?

主宰稳场 提交于 2019-12-01 04:20:30
问题 I figure this must be a simple mistake in the code or a misunderstanding on my part, but I cannot get a DocumentFilter to detect insertString events. Below is a simple filter for upper case letters, but that is not as important as the fact that the insertString(..) method never seems to be called! Why is the insertString(..) method of the DocumentFilter not called? The filter is applied to the JTextField at the top. Every time insertString(..) is called, it should append information to the

How to set DocumentFilter with input length and range? e.g. 1-3 or 10-80

大城市里の小女人 提交于 2019-11-29 18:06:53
I'm using DocumentFilter to restrict input as integer or decimal. And the code I post here is working well for that. Can anybody help me about how to restrict the input length or range in the given code? Thanks!! class MyIntFilter extends DocumentFilter { public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { Document doc = fb.getDocument(); StringBuilder sb = new StringBuilder(); sb.append(doc.getText(0, doc.getLength())); sb.insert(offset, string); if (test(sb.toString())) { super.insertString(fb, offset, string, attr); } else {

Trouble using regex in DocumentFilter for JTextField

↘锁芯ラ 提交于 2019-11-29 16:40:39
I am using a DocumentFilter on a JTextField which is used to enter in the amount of time an employee has worked. the filter is to ensure that the limit of input is only 4 characters long and to only allow numbers. A decimal may or may not be used but should only be allowed to be entered once, once a decimal is entered, there should only allow for one more number. Meaning 9.5 or 10.5 should be accepted and 8.45 is not. So far I am able to get about half of the total desired functionality. No more than 4 characters can be entered and only digits are allowed. The latter is accomplished using the

Trouble using regex in DocumentFilter for JTextField

…衆ロ難τιáo~ 提交于 2019-11-28 11:45:11
问题 I am using a DocumentFilter on a JTextField which is used to enter in the amount of time an employee has worked. the filter is to ensure that the limit of input is only 4 characters long and to only allow numbers. A decimal may or may not be used but should only be allowed to be entered once, once a decimal is entered, there should only allow for one more number. Meaning 9.5 or 10.5 should be accepted and 8.45 is not. So far I am able to get about half of the total desired functionality. No

Limit the Characters in the text field using document listner

左心房为你撑大大i 提交于 2019-11-28 09:59:59
问题 How to limit the number of characters entered in a JTextField using DocumentListener ? Suppose I want to enter 30 characters max. After that no characters can be entered into it. I use the following code: public class TextBox extends JTextField{ public TextBox() { super(); init(); } private void init() { TextBoxListener textListener = new TextBoxListener(); getDocument().addDocumentListener(textListener); } private class TextBoxListener implements DocumentListener { public TextBoxListener() {

JTextField limiting character amount input and accepting numeric only

大兔子大兔子 提交于 2019-11-26 21:06:33
here's the code that i have on how to limit the character input length class JTextFieldLimit extends PlainDocument { private int limit; // optional uppercase conversion private boolean toUppercase = false; JTextFieldLimit(int limit) { super(); this.limit = limit; } JTextFieldLimit(int limit, boolean upper) { super(); this.limit = limit; toUppercase = upper; } @Override public void insertString (int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { if (toUppercase) str = str.toUpperCase(); super

java change the document in DocumentListener

自古美人都是妖i 提交于 2019-11-26 16:44:16
I use a DocumentListener to handle any change in a JTextPane document. while the user types i want to delete the contents of JTextPane and insert a customized text instead. it is not possible to change the document in the DocumentListener ,instead a solution is said here: java.lang.IllegalStateException while using Document Listener in TextArea, Java ,but i don't understand that, at least i don't know what to do in my case? DocumentListener is really only good for notification of changes and should never be used to modify a text field/document. Instead, use a DocumentFilter Check here for

Using DocumentFilter.FilterBypass

你说的曾经没有我的故事 提交于 2019-11-26 14:49:15
问题 I want to have a method like this on my DocumentFilter public void replaceUpdate(int offset, int length, String text) { try { super.replace(byPass, offset, length, text, null); } catch (BadLocationException ex) { //error } } Currently in order to get an instance of FilterBypass (byPass on method above) , I need to get from the overridden method insertString : private FilterBypass byPass; @Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet