First of all, I don't know how to exactly formulate my question, so I don't know what to search for, really. If you know what I mean and it has a name, or even an answer somewhere, feel free to tell me.
I want to segment texts (into sentences or sentence-like structures) and show the segments to the user, with delimiters (extends JComponent) after each segment. Since the segmentation won't always fit, the user should then have the possibility to delete delimiters and insert new ones, but the text itself should not really be editable.
The question is, how (by what class or structure) should I display the text and delimiters so that the structure, but not the text itself is editable?
Any suggestions?
What I'm trying to do:
Related
I have a text editor in which the user can define a pattern for data. A pattern can contain a reference to another pattern. A simple example would be { name: $pattern2 }, where $pattern2 refers to a different pattern which will later be substituted into this one. However, patterns are identified by a UUID, not by a name, which makes it unwieldy to use. To compensate, users have a bunch of buttons that insert the UUID into the editor for them. However, UUIDs are not nice to look at.
A visually appealing way might be possible using a JTextPane (or JEditorPane) to replaces the UUID with a small square containing the referenced pattern's name. The first image below shows what the actual contents of the Document are, while the second image shows how I imagine it is displayed to the user, given two other patterns with the names GoodName and Date.
I have tried using the javax.swing.text.Style object obtained from jTextPane.addStyle("", null), I tried with a javax.swing.text.StyleContext, and I looked at extending javax.swing.text.DefaultStyledDocument, but those mostly seem to concern themselves with changing the way the font is rendered. I don't really see a way to render specific text as a non-text shape. Furthermore, I don't see a way of making the reference "atomic", in the sense that the user cannot select and remove half of the UUID, which would reveal the underlying data which I'm trying to keep hidden.
Is anyone aware of a component that supports behaviour like this? Am I overlooking a Swing feature here? Swing has loads of documentation but it's hard to find what you're looking for if you don't know what it's called.
Perhaps this is a case where a JLayer can be successfully applied.
Instead of replacing the UUIDs with small rectangles, an equally large rectangle containing the name could be drawn over each UUID.
In my android app, I want to create a text field with a lot of modification options (strong text, emphasized text, enter code here, enter link description here) like on stackoverflow.
I thought about this option:
In the answerfield of stackoverflow, strong texts are wrapped in "** **" and emphasized texts in "* *".
So I could conclude that every modification option have different special characters.
In the answer editor the special characters (**, *) are visible, so the special characters are inserted in the stackoverflow database.
So the only possibility is, that the text is interpreted after receiving it from database.
Is my guess right?
Can one of you give me a little bit android code of such a text modifier, so that I know how to start?
Just iterate About the Content in the textField and if you find a construct like ** then replace the following with a String in the whished font.
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 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
I have questions according to my final project in my IT Faculty.. I have to make A simple text editor (like notepad) without using JTextArea (GUI Java), I have to make my own JTextArea..
Idea/Topic= Own Text Editor
Algorithms= doesnt have any idea yet
Data Structure= Rope(Member of Binary Trees)
Requirement= Typing without JTextArea...
Is that possible to make that? Can a panel (or something else like that) has a listener or make a listener to do that?
simply thinking, First step I just want to try for typing on a panel, absolutely algorithms needed but i dont get right algorithms so far.. Is that possible?
My thinking is, for text that im typing is stored to a tree.. Is that Rope(Member of Binary Trees) for my data Structure?
Thanks for helping...
It's possible.
Use as you mentioned JPanel attaching KeyListener and MouseListener.
You need a model (Document). For simplest case it could be e.g StringBuilder where you can add content and show the StringBuilder content.
You need caret position (int field) to indicate where the edit should happen.
You need Font field to keep all the font info to be used in the editor.
Override paintComponent() and use FontMetrics to measure the text and calculate necessary width/height. For the start I would create a text area without line wrap.
Implement viewToModel/modelToView methods. They should calculate position of caret for given x,y and x,y for given caret position.