Turn fonts to Monospaced - java

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.

Related

JEditorPane unexpected behavior with new lines

I am trying to highlight text in a JEditorPane text field and save the index of the selected text. However I keep running into problems when I save the selections. The JEditorPane seems to save new lines in a different way each time. It will write a "\n", a "\r" and sometimes even a "\r\n" when the user presses the enter key. And sometimes the JEditorPane will even ignore carriage returns which causes wrong selection indices.
Is it possible to get a consistent behavior when the user presses the enter key?
Why are you using a JEditorPane. That is for displaying HTML and HTML generally doesn't have line breaks. You use the "br" tag in HTML. For normal text you should be using either a JTextPane or JTextArea.
I am trying to highlight text
The text in a string depends on the component you are using and whether you get the text from the Document or the component.
In general you should get the text from the Document since the Document only stores the "\n" so you should be able to calculate the offset correctly and highlight the text.
When you get the text from the component, the platform new line String is inserted in the text which would cause your offset problems.
See Text and New Lines for more information.

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.

move up down button in the text areas

I have two text areas on the screen, I have made bold the different words between two text areas and I want to have a button for up and down to go the different words on the text areas. Is this possible ?
have to look at JTextArea, but notice isn't designated for nice formatting or higlighting, then to use JTextPane instead
TextUtilities can help you to find out desired indexes rellative to screen or rellative to the model (Document)

Java coloring characters GUI

I am making a GUI dictionary program and have an input of letters that need to be highlighted in each words that is display. I was wondering how to do this, it doesn't seem very hard to color my entire JTextArea, but coloring only certain characters seems to be a little more difficult. I have read about JTextPane where I can use styled fonts, but even that doesn't seem straightforward. I need to parse every dictionary word's character and then change that characters color.
Yes, you'd want to use JEditorPane or JTextPane to do this sort of thing. You can either use styles, as you've seen, or you could create the contents as HTML and display that, which would be a lot simpler, if less flexible.
The relevant section of the Java Tutorial starts here.
You can add your own Highlights. See DefaultHighlighter and HighlightPainter.
See the http://java-sl.com/tip_vertical_selection.html example of custom highlights using. You can define any desired colors.

Java JTextArea Question

I am designing an on-screen form to be filled in, and I think it makes sense to stick it together as a collection of text areas. I note in the documentation of JTextArea that a text area can be subdivided into rows and columns, but I can't find any methods that appear to deal with placing text directly in any specific row/column cell in a text area.
Are there such methods, or is there an alternative text component that would work better for this purpose?
Thanks in advance for any insights.
John Doner
If you want a table, there is JTable but it is a bit more complicated. (Here is a tutorial) Alternatively you could put your JTextAreas into a layout such as GridLayout
A bit hackish, but you could insert (row-1) newlines and (column-1) space characters before your actual content. Of course that would only work on a previously empty text area.
However, you can extend that approach so that you only insert characters if needed, and otherwise just count already existing characters. That is, to go to a row, you skip the first row-1 newlines. Then in that line, you skip the first column-1 characters.
If there aren't enough newlines or characters already in the text area, you add more at the end of the text.
However, it gets trickier if your text content contains newlines itself.
It's doable, but it's gonna be ugly.
The row and columns values are just used to give the text area a preferred size.
I don't see the point of trying to set text at a specific row when creating a form. Generally forms would be designed with label/text field pairs, so the user know where there are entering the text.
If you text area is for output then you just add new lines when you want to display text on a different line.
Forcing a text area to be able to insert text at a given row/column is definitely not the way it was intended to be used.
I don't understand the real requirement so I can't make any other suggestions.

Categories

Resources