Multiple colors in JavaFX Textarea - java

How would I change the color of different text in a textarea in JavaFX? I've been doing some research, and I can only find solutions for Java Swing, not JavaFX.
To describe my situation, I'm trying to highlight different keywords in code, like a IDE. Basically, like this:
In the above image, certain words are different colors.(.list is red, span is blue, etc.)
Does anyone know how to apply this effect to a JavaFX Textarea?

RichTextFX lets you to assign different styles to different ranges of text. You will have to parse the CSS yourself, though. See the demo of highlighting Java keywords

Related

How to change JavaFX slider thumb color without CSS

I'm using ScalaFX, which is currently proving itself to be thoroughly useless and bugging out anytime I try to use css. Is there any way to change the fill color of a Slider without using css, or at least without separate css files?
I've managed to change the track color with slider.setStyle("-fx-control-inner-background: #f08080;"), but can't get the thumb working.
You could use the -fx-base property, which sets the component colors palette according to the value you specify.
Example:
slider.setStyle("-fx-base: #f08080;");
Result:
However why don't you consider using a CSS file? It simplifies everything and allows you to make a lot more customizations. Take a look at the Slider JavaFX CSS reference, I think you will find what you're looking for.

which swing component i must use to apply different styles of text in a same field in java

i need to show a formatted text in a jframe, well is not that simple because i really lost in which component i must do this, i worked in visual time ago and there are a rich textbox that have all the function to do this work but in java, which is the similar swing component that can me allow to apply multiple fonts to specific portions of text, and if i can show the actual number of line and column in a label apart of where the cursor is in.
swing component that can me allow to apply multiple fonts to specific portions of text
Probably a JTextPane. See the section from the Swing tutorial on Text Component Features.
show the actual number of line and column in a label apart of where the cursor is in
You would need to use a CaretListener. You can then use the Text Utilities to get the current line/column values. You can then format a string containing the values and set the text in a label on your status bar.

Dynamically highlighting strings in javaFX

Please read before labelling this as a duplicate.
I am creating an application that calculates how fast a person can type by calculating WPM and CPM. However I have hit a snag with the UI as I found out that you cannot really highlight individual strings in a TextArea. My goal is to compare what the user is typing to the random text that is generated by having it so that the text is being dynamically coloured or highlighted as the user is typing.
See http://10fastfingers.com/typing-test/english to get an idea of what I mean
I recently read the following post Highlighting Strings in JavaFX TextArea
I was trying to achieve the same goal of highlighting individual strings inside a javaFX TextArea until I realised that it pretty much is not possible. So I looked into TextFlow which does allow me to edit individual strings. The problem with TextFlow is that all 200 of the generated words would have to appear at once which is not what I want. With a TextArea not all the text has to be displayed at once.
This is what I have so far just so that you can further get an idea of where I am heading with this.

Text layout across different columns

I wonder if there is some library I have missed that allows me to distribute text among different text objects and have it reflow depending on size, newspaper style? it's something pretty easily done in web but I can't find anything like it on Java for Android.
I don't know of any Android Layouts that work that way. You could probably create one that mimics the way html and css work though.
another option is to make your content with the Web technologies that you are used to and just display it inside of a WebView in your application. That would likely be a lot easier than trying to create the Layout yourself.
I had two text fields of different size and different default justification plus offset from the screen border. I simply overlapped them in the same space. Depending on what needed to be displayed, I would null out one field and fill in the other. It kept me from creating 2 different layouts and having to get creative with the text formatting.

Looking for a way to highlight specific words in textareas?

Hi i'm looking for a way to highlight specific words in text kind of like how a text editor might work with syntax highlighting. The highlighting will consist of the text being different colours and/or different styles such as italic, bold or regular.
In order to narrow focus, how this might be achieved using Java Swing components.
There are most probably a number of ways of doing this but one that is efficient in dealing with multiple highlighted words and large amounts of text.
Any feedback is appreciated. Thanks.
You can use the Highlighter that is available through JTextComponent's setHighlighter().
Have a look at Java's JEditorPane class: it does what you want.
I would use a JTextPane. Its easier to use than a JEditorPane as you don't have to know or worry about HTML. The link you where given to the Swing tutorial covers both components.
You probably need something like RSyntaxTextArea. I personally like it because:
- it's easy extendable
- it comes with really useful plugins
- opensource & free & maintained
- it supports bunch of languages like: C, Java, Ruby, Php, HTML, CSS, Sql and so on.

Categories