List of usage examples for javax.swing.text DocumentFilter DocumentFilter
DocumentFilter
From source file:MainClass.java
public static void main(String[] args) { JTextField field = new JTextField(30); ((AbstractDocument) (field.getDocument())).setDocumentFilter(new DocumentFilter() { public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { System.out.println("insert"); fb.insertString(offset, string.toUpperCase(), attr); }//ww w .j a v a2 s . c o m public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr) throws BadLocationException { System.out.println("replace"); fb.replace(offset, length, string.toUpperCase(), attr); } }); JFrame frame = new JFrame("User Information"); frame.getContentPane().add(field); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { JTextField field = new JTextField(30); ((AbstractDocument) (field.getDocument())).setDocumentFilter(new DocumentFilter() { public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { System.out.println("insert"); fb.insertString(offset, string.toUpperCase(), attr); }//from w w w. j ava2 s .co m public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr) throws BadLocationException { System.out.println("replace"); fb.replace(offset, length, string.toUpperCase(), attr); } }); JFrame frame = new JFrame("User Information"); frame.getContentPane().add(field); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
From source file:Main.java
public static DocumentFilter getUpperCaseFilter() { return new DocumentFilter() { public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { fb.insertString(offset, text.toUpperCase(), attr); }/* ww w. j ava 2 s . com*/ public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { fb.replace(offset, length, text.toUpperCase(), attrs); } }; }
From source file:Main.java
private static JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() { JSpinner spinner = new JSpinner(new SpinnerNumberModel()); JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor(); JFormattedTextField textField = jsEditor.getTextField(); DocumentFilter digitOnlyFilter = new DocumentFilter() { @Override//from w ww . j a v a2 s.c om public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { if (stringContainsOnlyDigits(string)) { super.insertString(fb, offset, string, attr); } } @Override public void remove(FilterBypass fb, int offset, int length) throws BadLocationException { super.remove(fb, offset, length); } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if (stringContainsOnlyDigits(text)) { super.replace(fb, offset, length, text, attrs); } } private boolean stringContainsOnlyDigits(String text) { for (int i = 0; i < text.length(); i++) { if (!Character.isDigit(text.charAt(i))) { return false; } } return true; } }; NumberFormat format = NumberFormat.getPercentInstance(); format.setGroupingUsed(false); format.setGroupingUsed(true); format.setMaximumIntegerDigits(10); format.setMaximumFractionDigits(2); format.setMinimumFractionDigits(5); textField.setFormatterFactory(new DefaultFormatterFactory(new InternationalFormatter(format) { @Override protected DocumentFilter getDocumentFilter() { return digitOnlyFilter; } })); return spinner; }
From source file:com.hexidec.ekit.component.PropertiesDialog.java
public PropertiesDialog(Window parent, String[] fields, String[] types, String[] values, String title, boolean bModal) { super(parent, title); setModal(bModal);//from w w w. ja v a 2 s. c om htInputFields = new Hashtable<String, JComponent>(); final Object[] buttonLabels = { Translatrix.getTranslationString("DialogAccept"), Translatrix.getTranslationString("DialogCancel") }; List<Object> panelContents = new ArrayList<Object>(); for (int iter = 0; iter < fields.length; iter++) { String fieldName = fields[iter]; String fieldType = types[iter]; JComponent fieldComponent; JComponent panelComponent = null; if (fieldType.equals("text") || fieldType.equals("integer")) { fieldComponent = new JTextField(3); if (values[iter] != null && values[iter].length() > 0) { ((JTextField) (fieldComponent)).setText(values[iter]); } if (fieldType.equals("integer")) { ((AbstractDocument) ((JTextField) (fieldComponent)).getDocument()) .setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int offset, String text, AttributeSet attrs) throws BadLocationException { replace(fb, offset, 0, text, attrs); } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if (StringUtils.isNumeric(text)) { super.replace(fb, offset, length, text, attrs); } } }); } } else if (fieldType.equals("bool")) { fieldComponent = new JCheckBox(fieldName); if (values[iter] != null) { ((JCheckBox) (fieldComponent)).setSelected(values[iter] == "true"); } panelComponent = fieldComponent; } else if (fieldType.equals("combo")) { fieldComponent = new JComboBox(); if (values[iter] != null) { StringTokenizer stParse = new StringTokenizer(values[iter], ",", false); while (stParse.hasMoreTokens()) { ((JComboBox) (fieldComponent)).addItem(stParse.nextToken()); } } } else { fieldComponent = new JTextField(3); } htInputFields.put(fieldName, fieldComponent); if (panelComponent == null) { panelContents.add(fieldName); panelContents.add(fieldComponent); } else { panelContents.add(panelComponent); } } jOptionPane = new JOptionPane(panelContents.toArray(), JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, buttonLabels, buttonLabels[0]); setContentPane(jOptionPane); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); jOptionPane.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { String prop = e.getPropertyName(); if (isVisible() && (e.getSource() == jOptionPane) && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) { Object value = jOptionPane.getValue(); if (value == JOptionPane.UNINITIALIZED_VALUE) { return; } if (value.equals(buttonLabels[0])) { setVisible(false); } else { setVisible(false); } } } }); this.pack(); setLocation(SwingUtilities.getPointForCentering(this, parent)); }
From source file:hr.fer.zemris.vhdllab.applets.texteditor.TextEditor.java
@Override protected JComponent doInitWithoutData() { textPane = new CustomJTextPane(this); wrapInScrollPane = false;/*from ww w. j av a 2 s. co m*/ JScrollPane jsp = new JScrollPane(textPane); LineNumbers.createInstance(textPane, jsp, 30); Document document = textPane.getDocument(); if (document instanceof AbstractDocument) { ((AbstractDocument) document).setDocumentFilter(new DocumentFilter() { @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if (text != null && (text.length() - length) > 10 && !text.equals(CustomJTextPane.getClipboardText()) && !textIsBlank(text)) { informPastingIsDisabled(); } else { super.replace(fb, offset, length, text, attrs); } } private boolean textIsBlank(String text) { if (text == null) return true; for (char c : text.toCharArray()) { switch (c) { case ' ': break; case '\t': break; case '\r': break; case '\n': break; default: return false; } } return true; } private void informPastingIsDisabled() { JFrame frame = Application.instance().getActiveWindow().getControl(); JOptionPane.showMessageDialog(frame, "Pasting text from outside of vhdllab is disabled!", "Paste text", JOptionPane.INFORMATION_MESSAGE); } }); } textPane.addCaretListener(this); // commitTrigger = new CommitTrigger(); // TextComponentPopup.attachPopup(textPane, commitTrigger); return jsp; }
From source file:net.team2xh.crt.gui.editor.EditorTextPane.java
public EditorTextPane() { // Initialize highlighters lh = new LineHighlighter(Theme.getTheme().COLOR_13); ll = new LineHighlighter(Theme.getTheme().COLOR_14); wh = new WordHighlighter(Theme.getTheme().COLOR_11); eh = new ErrorHighlighter(Color.RED); // Initialise colors initAttributeSets();/*ww w . j av a2 s . com*/ setOpaque(false); // Background will be drawn later on setFont(font); doc = (DefaultStyledDocument) getDocument(); // Replace all tabs with four spaces // TODO: tab to next multiple of 4 column // TODO: tab whole selection // TODO: insert matching brace doc.setDocumentFilter(new DocumentFilter() { private String process(int offset, String text) { return text.replaceAll("\t", " ").replaceAll("\r", ""); } @Override public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { super.insertString(fb, offset, process(offset, text), attr); } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException { super.replace(fb, offset, length, process(offset, text), attr); } }); // Highlight text when text changes doc.addDocumentListener(new DocumentListener() { @Override public void removeUpdate(DocumentEvent e) { SwingUtilities.invokeLater(() -> highlightText()); changed = true; } @Override public void insertUpdate(DocumentEvent e) { SwingUtilities.invokeLater(() -> highlightText()); changed = true; } @Override public void changedUpdate(DocumentEvent e) { } }); addCaretListener(new CaretListener() { private boolean isAlphanum(char c) { return Character.isDigit(c) || Character.isLetter(c); } private boolean isAlphanum(String s) { for (char c : s.toCharArray()) // Allow dots in middle of words for floats { if (c != '.' && !isAlphanum(c)) { return false; } } return true; } @Override public void caretUpdate(CaretEvent ce) { try { // Highlight current line highlightCurrentLine(); // Clear previously highlighted occurrences for (Object o : occurrences) { getHighlighter().removeHighlight(o); } repaint(); occurrences.clear(); // Get start and end offsets, swap them if necessary int s = ce.getDot(); int e = ce.getMark(); if (s > e) { s = s + e; e = s - e; s = s - e; } // If there is a selection, if (s != e) { // Check if the char on the left and on the right are not alphanums char f = s == 0 ? ' ' : doc.getText(s - 1, 1).charAt(0); char l = s == doc.getLength() - 1 ? ' ' : doc.getText(e, 1).charAt(0); if (!isAlphanum(f) && !isAlphanum(l)) { String word = doc.getText(s, e - s); if (isAlphanum(word)) { highlightOccurrences(word, s, e); } } } } catch (Exception e) { e.printStackTrace(); } } }); setCaretColor(Theme.getTheme().COLOR_11); highlightText(); }