Remove last character from JTextPane - java

I'm writing a vt100 emulator and I'm using a JTextPane with a DefaultStyledDocument to display the formatted text. Now I want to implement the backspace, so i need to be able to remove the last character.
I tried the following:
doc.remove(doc.getEndPosition().getOffset()-1, doc.getEndPosition().getOffset());
But I keep getting a 'javax.swing.text.BadLocationException: Invalid remove'
How should this be done?

You're using the API wrong. The last parameter is the number of characters to remove which in your case should be 1.
Here is the API for Document.remove(int, int).

Related

Arabic pronounce symbol not showing correctly

Im trying to put text with arabic letters, some of the text working correctly, and the others symbol is showing weirdly.
Some of the letters just font issue, i can still tolerate if it fix all the mistakenly displayed symbol.
I tried to change font, putting on string, custom font, but it does not work. Any ideas guys ?
i put the pull the text from string res currently.
here is the wrong letters.
here is the correct letters
You should use custom font for your view..
For example this view support TextView custom font from assets.
If you were wondering what encoding would be most efficient:
All Arabic characters can be encoded using a single UTF-16 code unit (2 bytes), but they may take either 2 or 3 UTF-8 code units (1 byte each), so if you were just encoding Arabic, UTF-16 would be a more space efficient option.

convert a method to work with gui

I've got a huge method that prints multiple lines, numbers, characters and uses system.out and multiple data types, it works. But I'd like to use it in a jframe. I tried converting every system.out statement to a jtextArea.setText(), and did casting for non string types but nothing comes out when I run it.
Is it possible? what is the right way of doing that.
jtextarea right for my method.
If you wish to append text to a JTextArea, use the append method. Right now your code is using setText, which does just that: sets the text of the JTextArea, removing all previous text in the process (and in this way your code seems to almost guarantee that the JTextArea either contain no text, or contain a single new line character).
try jTextField if its only gonna output.
Try jTextField.append if the user will write stuff on the text.

How to listen for uppercase characters in a JTextPane as the user types

First time asker.
I have developed a text editor using java's JTextPane. The editor currently does everything an editor should do; open, close, save, saveAs, cut, copy and paste etc. I have a couple of extra buttons that do various things to selected text.
Now my question:
I want one of these methods to be performed every time the user types a word beginning with a capitol letter. I'm just a bit lost as to where to begin with checking if each character is a capitol as the user types.
Hope I've provided enough information.
Thanks
What do you want to do when the user types "I"? You don't know what the next character will be. It could be an upper case character, a lower case character or a space.
I'm not sure what type of processing you intend to do every time an upper case character is typed because usually you don't know the context of that character until other characters have been entered.
In any case you intercept changes to the Document of the JTextPane before the change is made by using a DocumentFilter. So you can use the DocumentFilter to invoke your method when an upper case character is entered. Check out the section from the Swing tutorial on Implementing a Document Filter for more information.
Also, what happens when the user pastes a String of text to the text pane? Several of the words could contain upper case characters.
As camickr said, DocumentFilter is the best answer, what he needs to do is keep track of space, if followed by a capital letter "[A-Z]", then save the caret position, when another space is entered the word is finished and the action is fired. BTW, he needs to keep track of not only space, but also new line and tab characters.

open office api text size

I am using open office API with Java UNO. I need to get size of selected text in the document content (for example embedded pictures have own size in mm but text inserted via XText.insertString(...) method doesn't have any size).
In other words: I want to get size (preferably in mm) of the box which surrounds part of text (it can be whole paragraph or selected text via some type of cursor). Is there any possibility to achieve that?
After searching, I think there is no option to achieve this at the moment. For my purposes I write small method for getting height of the paragraph in 1/100 mm.
Here is how this method works:
Get XTextViewCursor of the XTextDocumment controller for going left/right.
Go to paragraph to measure.
Loop through paragraph getting each char. For each char do: check its height (CharHeight property of the paragraph); get XLineCursor from XTextViewCursor and check if there is end of the line - if is then add (to the result) biggest height of the character in line.
This is temporary solution (still wait for something better) and has number of bugs (example line-spacing different than single; paragraph should only contain text) but maybe it will be helpful for someone.

Turn fonts to Monospaced

I am trying to make a bizarre text editor for people with reading problems with Netbeans. You load the text you like and the editor starts highlighting it word by word with bold letters. The change from plain to bold constantly change the word dimensions and moves the line. One solution was the Monospaced Font but I would like to add a few more fonts available for the user to choose. Is there any way to do this with Arial for example by giving some orders to the JTextPane?
You can manually split the String with <br/> by counting characters and splitting at the right spot to keep the width under your desired character width. Give some leeway so if you get a big word, it won't still go to the next line.
Alternatively, you could use a JList to display your lines (instead of using <br/>). That way, there's no way the line would split to the next line. However, if you do it that way, the user will click on the list like a list and not be able to select text like in a normal text pane.

Categories

Resources