documentfilter

Only allowing numbers and a symbol (-) to be typed into a JTextField

守給你的承諾、 提交于 2019-11-26 09:58:05
问题 I\'m trying to create a math quiz and I only want the user to be able to enter numbers whether they\'re negative or positive. Is there any way to do so? I\'ve thought of using Regular Expressions but I\'ve heard that they are bad to use. I tried using a keylistener but then what if the user pastes? I\'ve tried parsing the string to get an integer but then the negative symbol will not work. Any ideas? package com.quiz.ui; import java.awt.BorderLayout; import java.awt.event.KeyEvent; import

JTextField limiting character amount input and accepting numeric only

别等时光非礼了梦想. 提交于 2019-11-26 07:49:07
问题 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;

java change the document in DocumentListener

女生的网名这么多〃 提交于 2019-11-26 04:55:33
问题 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? 回答1: DocumentListener is really only good for notification of