Java coloring characters GUI - java

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.

Related

Hyperlink in swing component with character wrap (combination of JEditorPane and JTextArea)

I want to put a hyperlink and some other text in a swing component. This component should be able to fit all of the text in its horizontal space (i.e. no horizontal scrollbars) by wrapping by words, and only by characters when words are too long to fit across the entire component. Parts of this can be accomplished using certain components:
JEditorPanes support hyperlinks. However, they don't break on characters and do weird things when placed in JScrollPanes.
JTextAreas can wrap by words or characters, but do not support hyperlinking.
Is there some combination of these components, or some way I can get one to act like the other?
Some of the other SO questions that I've looked at (for reference):
Wrap long words in JTextPane (Java 7)
JEditorPane inside JScrollPane not resizing as needed
How can I add a clickable URL in a JTextArea?
Note: I am using java 8, and would prefer not to have to download anything, if possible.
Thanks in Advance!
Can you get a JEditorPane to break on characters if you add this to its stylesheet?
word-wrap: break-word;
If you want to elaborate on the "weird things" that might be holding you back from using it, we can try to address those too.

Swing: non input text field

I'm new in Swing, and don't want to learn it. I just want to write a simple frame divided into two equal parts: top and bottom. The top part of frame should be a simple immutable text field.
Problem: As I understand, to show text with beautiful font I should use JTextPane. But JTextPane:
Doesn't support vertical text alignment; (I haven't got any desire to write something like that)
I don't know how to switch off editing.
Quedtion I believe there is a simpler solution for my purpose. Is there?
The top part of frame should be a simple immutable text field.
Use a JLabel. It supports HTML which might help with your formatting.
I don't know how to switch off editing.
setEditable( false );

How to force JTable to NOT invert text in the case it contains arabic characters?

I have the following text:
وزا.word
But when displaying it on my JTable it looks like this:
word.وزا
In every JLabel or TextArea or any other input it does look like the original text:
وزا.word
ONLY on the JTable I am having such problem.
I do not care if it is making sense or not, and yes I know the Arabic Language is written from right to left. My guess is Java is detecting it and automatically inverting it, but I do not want it to.
Note: I have no idea what وزا means, and for practical purposes I don't care. It's also irrelevant for this case wether وزا.word does not make sense and word.وزا does or viceversa.
Note 2: The text, reversed or not is always aligned to the left (as I expect it to).
Thanks in advance.
At a guess, your default Locale is giving the default renderer a ComponentOrientation that is inconsistent with your other settings. You might try creating a custom renderer having the preferred orientation using one of the approaches suggested here.
Addendum: java.text.Bidi supports bidirectional reordering; you may be able to use unicode format control code points, as suggested in this Q&A.

How to give different - 2 color to text written in JTextArea?

I want to change color of text written in JTextArea. For e.g. I am pasting Java code in JTextArea.
I want to give the different-2 color to Java keywords, variables and classes written in code.
Is it possible?
See Rob Camick's Message Console. It seems perfect for this use case.
Actually '2 color' might not be enough for this.
See How to Use Editor Panes and Text Panes for more details on the formatted text components that might replace JTextArea.
it's not possible
if you want to set different colors in the same area, i think JTextPane could help you
i'm not sure how to use that, but i found this
http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

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