0

I've got question regarding to typing in JTextField. My program search thru few csv files and look for specified in JTextField string. I have add to readLine function ".toLowerCase" to read all strings as lowercase. Is it possible to set JTextField to automatically convert uppercase to lower case while writing to JTextField?

if (line.toLowerCase().contains(searchedString))...

Grzegorz
  • 39
  • 1
  • 7

3 Answers3

2

Yes, you can use the KeyListener and when a key is pressed in the textfield, you will make the input string lowerCase while keeping the cursor position where it was. Like the code below:

jTextField1.addKeyListener(new KeyListener() {
    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int pos = jTextField1.getCaretPosition();
        jTextField1.setText(jTextField1.getText().toLowerCase());
        jTextField1.setCaretPosition(pos);
    }
});

Source:

Community
  • 1
  • 1
Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
  • Works perfect! That's what I was looking for. Thanks. – Grzegorz Feb 12 '17 at 10:42
  • You can save yourself a bit of code by using `KeyAdapter` rather than `KeyListener`. It gives you no-op versions of methods so you only need to implement `keyReleased`. – sprinter May 03 '21 at 23:45
  • Also note this will only work for keys. Other entry methods (e.g. cut-and-paste) won't be lowercased. – sprinter May 03 '21 at 23:48
1

You can create a class that extends DocumentFilter class and override methods insertString and replace so that:

  • In insertString method it will call it's super, passing in one of the parameters string.toLowerCase()
  • In replace method it will call it's super, passing in one of the parameters text.toLowerCase()
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;


class LowerCaseDocumentFilter extends DocumentFilter {
    @Override
    public void insertString(final FilterBypass fb, final int offset, final String string, final AttributeSet attr) throws BadLocationException {
        super.insertString(fb, offset, string.toLowerCase(), attr);
    }

    @Override
    public void replace(final FilterBypass fb, final int offset, final int length, final String text, final AttributeSet attrs) throws BadLocationException {
        super.replace(fb, offset, length, text.toLowerCase(), attrs);
    }
}

then adds an instance of this class so that the JTextField will automatically convert to lower case:

class Main {
    public static void main(String[]args) {
        JFrame jFrame = new JFrame("Example");
        jFrame.setSize(500, 500);
        jFrame.setVisible(true);
        JPanel jPanel = new JPanel();
        jFrame.add(jPanel);
        JTextField jTextField = new JTextField("Example JTextField");
        ((AbstractDocument)jTextField.getDocument()).setDocumentFilter(new LowerCaseDocumentFilter());
        jPanel.add(jTextField);
        jFrame.pack();
    }
}

Source: https://stackoverflow.com/a/11573312

0

You can create your own class by extending the JTextfield and override constructor/setter method.

DNAj
  • 240
  • 3
  • 9