String-based cursor positioning - java

How do I locate the cursor position in a Java textarea where the text is held in a StringBuffer? I am trying to implement the backspace function in a text editor. The API lists how to set data. I need to get the cursor position, to know at which character position the cursor is, not the onscreen global coordinates of the cursor.
Any assistance is sincerely appreciated.

You have to write KeyListeners for keypress events in the TextArea and keep a virtual cursor of where you are in the TextArea. Then map that offset into your StringBuffer, there is no magic way to do it.

If you are using a Swing JTextArea, use JTextArea.getCaretPosition().
If you are using an AWT TextArea, see Jarrod Roberson's answer.

Related

Drag and drop text from a JLabel outside the java application onto a pdf form

I was wondering if it was possible to drag the text from a JLabel, out of the java application onto a pdf form for example.
If it is possible, could you point me in the right direction please?
Its easy.What i woud do- Put listener onto JLabel, take text on event that you pick.Copy text from the JLabel value into Clipboard,
Now you have text that you can manipulate.
Drag effect.=Use (with combination of the previous listener) JNativeHook lib to get GLOBAL EVENTS of java application and outside.
Listen for dragging(Do on your own its easy) , on mouse release onto your pdf input text.
You might have to simulate mouse click(right after release) to select field (via robot class) to actualy SELECT THAT FIELD,so you can input text.
Input text part=Use robot class to insert string char by char.
This will work well.I dont have any other idea atm that woud be this easy to implement.
You can also (fool user) with effect of floating text etc if you put transparent window over the top and check mouse location on screen.

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.

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

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.

is that possible to built a simple text editor in java without using JTextArea?

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.

Categories