Ctrl-Delete in JTextField - java

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.

Related

Sending Unicode Text to Cursor Position in Java

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.

Uneditable ParagraphViews in Java

I would like to have a document editor where certain document lines are uneditable, unclickable, etc.
I currently am using a JTextPane for my editor and extensions of DefaultStyledDocument and LeafElement, StyledEditorKit, and ParagraphView, to parse the document text into the appropriate elements and assign them to the appropriate extensions of ParagraphView. Everything is peachy up to this point.
So how to take the next step? How to designate and enforce certain extended ParagraphViews are "read-only"?
My current approach is to override getNextVisualPositionFrom in the Leaf and Section ParagraphViews to prevent arrow keys from moving the cursor into a restricted ParagraphView, but preventing the mouse from clicking inside a restricted ParagraphView has to be handled separately.
Is there a simpler, more comprehensive approach to this?
Thanks!
After a bit more research, a different approach that seems to fit more elegantly is to use a ComponentView for the uneditable views, instead of ParagraphView.
I can then use something as simple as a JTextArea in the ComponentView in its createComponent() method and setEditable to false.
It automatically ignores mouse clicks in that view. I still have to include my code for nicely skipping the caret over the view when the user presses up, down, left, right. Otherwise, the caret will invisibly move through underlying model text. I will also have to prevent backspacing through the protected view. Even though the view is uneditable, the model data underneath can still be affected by the user.

Best way to implement a selection box from a large number of entries

I have a large set of data from which the user has to select one. I'm thinking of a way to implement it (of course, in a GUI). I have a few ideas. But just thought of posting here as there may be better alternatives..
Say, user has to select a name from a large set of user base. If I simply put a text field for user to enter the name, then there can be issues like entering same name in different formats, misspelling etc...
I see two options here
Using a combo box
Using a list (Actually i'm thinking of something like a tool tip. As I cant show the whole list always due to space issues)
But combo box won't be much user friendly i guess. As the user will have to scroll around the whole list to select an entry. If the number of entries are too large, this will be
Which means, now I'm left only one option. A popping up list, which will change the content according the text user is entering in the text field. So he can type first few letters and the list will show all the entries starting from the entered text. Got my point, right?
Are there any other better to achieve this kind of need?
If I'm going to implement above, what will be the best way to follow. I'm thinking of extending the JTextField to add required functionality. Well, I'll put some method to set the popup list entries. And I'll add some actionListner to watch the text field, and control the popup list accordingly...
Autocomplete is what you are probably looking for. Google for "java swing jcombobox autocomplete" and limit results for the last couple of years to get relevant results. There will be a lot of examples and ideas on how to implement this with custom code.
I believe there is also some custom libraries like "swingx" that provide at least partial or full implementations to save time.
http://swingx.java.net/
They have released code as recently as the beginning of this years so it appears active and might have what you need.
You could take a look at SwingLab's autocomplete feature, it allows you to attach it to a JCombBox, JList or JTextComponent
use AutoComplete JComboBox/JTextField
based on Standard Java Classes
no issue with larger sets of data
no issue with Focus, BackSpace Key, Caret
for better performance to required sort the array before use
simple workaround for setStrict(true/false), restrict input to array

Hot Keys missaligned in Swing

I'm using Swing to create a hot key for all the menus. For Save As menu item The 'A' should be under lined instead of 'a' immediately following the S in Save as. I've used the following code to create hot keys for menuitems
"for Save As Menu: MenuItem.setMnemonic('a');"
You'll want to use the setDisplayedMnemonicIndex(...) to provide a hint to the JVM for which letter should get the underline. You invoke this after calling setMnemonic(KeyEvent.VK_A)
Please check out the API for more on this: AbstractButton API

Selecting nested subexpressions in a Swing GUI

As a part of a GUI design that I'm implementing in Java swing, I need the user to be able to select parts or subparts of a tree like structure represented as a string.
For example, if I were to display the following expression, ((a|b)|(c|d))
The user would need to be able to select any of the following
(a|b), (c|d) or the entire thing ((a|b)|(c|d)).
Ideally I'd like them to be able to navigate via the keyboard arrows, moving up and down though the nested subexpressions, and hit enter when they come to the subexpression they want. However if its only possible to do this on mouse click, thats also acceptable.
The main issue that I'm having with this is the nesting component. I could easily make the entire expression selectable, but I don't know how to allow subexpressions to be selectable using SWING components. Based on my research Swing doesn't allow nesting of labels of text areas in the manner that I need so I'm looking for any alternatives.
Any help or suggestions would be much appreciated.
You could use a Highlighter and a DocumentListener on the read-only JTextField suggested by Joop in the comment above, following http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html in the section about JTextFieldDemo. That way you can highlight your (partial) string and listen to what (sub)string is selected, and compare it to the original string to check for matches.

Categories