I want to make an auto completion textfield, which replaces the input with the first hit and selects the part which wasn't entered by the keyboard yet. To do so, I'll have to count the chars typed (or deleted). How can I filter if the KeyEvent is a printable Char (and not SHIFT f.e.)?
Look at https://swingx.dev.java.net/
This lib is already have great autocomplete text fields
i prefer to do it with the KeyListener
Well, a KeyListener is the wrong tool for the job.
If you really want to write your own code then you should start with a DocumentListener. Every time text is added or removed from the text field you will be notified. Then you can implement your auto completion code.
Related
Hi fellow programmers!
I have a JEditorPane, where user is supposed to input series of numbers (quantities with optional uncertainities) separated by semicolons, for example:
3.0; 5.8; 70+-5; ...
The list is then mapped to ArrayList wrapper containing parsed quantities.
I'd like to implement a specific behaviour for input pane. For example pressing ';' or ' ' should insert '; ', pressing backspace/delete between ';' and ' ' should erase both of them and combine separated numbers. Cuts and pastes also have specific behaviour, and so on. I want it to be user-friendly and intuitive.
I tried DocumentFilter, but it seems to be too simple and it can mess up caret/selection. So I thought about writing my very own StyledEditorKit subclass, but there's a load of Actions to implement and a lot of technical details, which I am not certaing about.
How can I deal with it? Is there any way of doing it without writing EditorKit from scratch?
First of all I wouldn't use a JEditorPane for this. A JEditorPane is for displaying HTML. It will be much easier using a JTextArea or JTextPane since this only contains text and no tags to worry about.
I tried DocumentFilter, but it seems to be too simple and it can mess up caret/selection.
A DocumentFilter is designed to used by multiple Documents and therefore knows nothing about the actual text component you are using. If you want to control caret location then you would need to pass the text component as a parameter to your DocumentFilter class.
pressing ';' or ' ' should insert '; ',
If you don't like a DocumentFilter, then maybe you can use Key Bindings and handle the keyTyped event
pressing backspace/delete between ';' and ' ' should erase both
A DocumentFilter should work or again you can use Key Bindings. Note for the backspace key I believe you need to handle Ctrl+H.
Cuts and pastes also have specific behaviour
A DocumentFilter won't the reason for the update to the Document (ie user typing or user pasting. You could try overriding the cut/copy/paste methods of the text component.
So I thought about writing my very own StyledEditorKit subclass, but there's a load of Actions to implement
Agreed, I think this is overkill.
I have done a GUI in Java with a JTextArea. It is filled with the content of a file.
When I select words with the mouse on the textarea, a new frame pops up on which I do some operations on the selected words. To do these operations, I need to know the line number of the selected text...
Does someone know how to get the line number?
(I look to some methods on the classes JTextArea and MouseListener, but i dont know how to do that...)
Thanks ;)
Check out the Text Utilities. The getLineAtCaret() method is close to what you need. It uses the offset of the caret to get the line number. In you case you will need to use the start offset of the selected text.
Doing linguistics and phonetics, I often need to use certain special phonetic symbols. Although I'm using a special keyboard layout that enables me to write some of those characters by typing, they key combinations can often get both quite complex and highly repetitive, so I would like to create a litle app that would contain some buttons, perhaps, each of them capable of sending a specified (phonetic) symbol to whatever the current cursor position is, no matter what window on one's screen is in focus.
Is anything of this sort possible to do in Java?
I've seen a solution that copies the values into clipboard and then pastes them (Java paste to current cursor position), but that is not a very clean way to do it, is it? Is there a way better than just pasting the charactedr(s) via ctrl+V?
Many thanks for any help or advice in advance!
P.
You can use the AWT Robot to generate key press events. This will not provided the ability to insert arbitrary unicode characters but you can combine it with the technique you already described: transfer the unicode characters to the clipboard and generate a CTRL+V key event afterwards. You can try to save and restore the original clipboard content but this will work with types supported by Java only.
The focus problem mentioned in the comments can be solved by setting the window to not receive the focus via Window.setFocusableWindowState with an argument of false.
An alternative is to provide the unicode text via drag&drop. Most applications support dropping text in their input fields. The code for exporting the text is very similar as both, clipboard and d&d use the same interfaces in Java.
I'm having a problem with JTextArea (not a problem though).
I want to create my own college program, one of the function is to write down and save a list of homework each day. It's text area having the main function here, which is to type everything my lecturer said. But text area will act like Windows original notepad, which is not keeping the indentation. So my questions are:
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
If number 1 is possible, then how do my program have behaviour when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Edited: I'm a beginner in Java, in truth, I'm making this program while studying Java in my college.
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
You need to use key bindings to listen for the user typing Shift+Tab.
If number 1 is possible, then how do my program have behavior when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Use a similar piece of logic to capture the Enter key presses. Then, check the previous line in the editor and count the number of tab characters preceding the text. Output a similar number at the start of the new line.
you could use Javascript/jquery for indenting by inserting empty space of specific line. while pressing your hot key combination call function to insert five spaces on your cursor.
How can I get JTextFields to allow Ctrl-Delete and Ctrl-Backspace when editing text?
In various other programs, these key combinations can delete an entire word in one go.
From what I can tell, the default behaviour of a JTextField allows the user to use CTRL to jump over an entire word when using left and right keys, and to select an entire word when SHIFT is used too. However, deleting just doesn't work.
Swing uses Key Bindings to map Actions to components. To find out the default mappings for a given component you can use the Key Bindings program. The article also contains a link to the Swing tutorial which contains a section on "How to Use Key Bindings".
To create your custom Action you would extend TextAction so you have access to the text component. You would then need to get the current caret position. Then you can use the Utilities class to get the start or end of the current word and then you can remove the characters from the Document.
You need to define an Action and put it into the action map of the composite. See this article for an introduction.