Dynamically highlighting strings in javaFX - java

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.

Related

Multiple colors in JavaFX Textarea

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

Simple, wrapping red validation message in java swing

So I've been developing web applications for 10 years, but have fond memories of working on small swing applications that related to an introductory programming curriculum I was paid to help with before that. I generally like building swing/java UI especially with a decent UI builder. However, in the intervening time I've become very accustomed to providing clear, red error messages of variable length and number when the data on a form is invalid. Sometimes with icons, sometimes with markers on the field etc. Very common stuff that's easy to do in the web world, and is really helpful and improves usability.
Today I was trying to find a way to display a nice list of error messages at the top or bottom of my swing form, icons and field markers can come later, but for now I just want a nice variable sized list of things that need to be corrected, in a red color.
At first blush this seems like it would be easy, but..
JLabel doesn't wrap, so long text is hidden, or you have to make the form ultra wide
JText Area has the same issue unless you define a number of columns, and I want to fill the space available. A set number of columns won't resize.
JTextPane wraps but as far as I can tell it won't turn red unless you get into the whole way over complicated StyledDocument bit. I don't want to do anything more complicated than change the text color so this seems like overkill.
There's also the minor issue of pushing outside the window space, and one can call pack(), but Id like to only increase the height of the form and wrap the text not change the dialog to the width of the message (creating lots of strained layout and dead space if a single long message doubles the width)
I also don't really want to have scroll bars either for the message area or the form as a whole.
This surely is a wheel that's already been invented, so what's the right way to show a list of red (but otherwise unformatted) error messages in a swing form?
Label doesn't wrap, so long text is hidden, or you have to make the
form ultra wide
use HTML
JText Area has the same issue unless you define a number of columns,
and I want to fill the space available. A set number of columns won't
resize.
use word and line wrap
A set number of columns won't resize. and together with "I also don't really want to have scroll bars"
put JTextArea to JScrollPane, to disable JScrollBars, set null for Borders from JScrollPane
JTextPane wraps but as far as I can tell it won't turn red unless you
get into the whole way over complicated StyledDocument bit. I don't
want to do anything more complicated than change the text color so
this seems like overkill.
use HighLighter, but nothing clear from posted description about your issue with StyledDocument

Homework assignment using Java/Swing

I've got an assignment in a class about user interfaces and usability testing. I have to do something that I'm sure I can figure out how to do programatically, but I have no experience with swing so I have no idea what components to use to do the job. My background is in C# so I'm fumbling at times trying to find the right component to use in NetBeans.
Based on the description below, can anybody recommend what kind of text field will do the job? I was thinking jFormattedTextField (based on the name) but I can't seem to figure it out.
Thanks
Submit a Java program that will run on the Linux installation in the general lab (EN-2036).
Provide the following functionality:
load the output of the last command (from a file) into a text viewing area that
allows the user to browse the data
bold all login names only (not the whole line)
bold all occurrences of a user-specified login name
bold all occurrences of a user-specified set of login names
Use JEditorPane or JTextpane, these allow you to add style to text inside them
Tutorial Link

Simple text formatting, such as superscript, in Java? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I write superscript word for checkbox text in java?
So I have this very simple program that basically consists of a GUI with a few text fields and a button.
The idea is that the user enters numbers into three fields and presses the button. Then what happens is a few mathematical procedures are carried out in the background, and the resuling answer is presented in a fourth text field.
Now, this does the job, but the formatting looks awful. At the very least, I would like to have part of the output superscripted. I have next to no experience with these things, but thought I would be able to get the hang of this on my own, but I'm stuck. I think I need to use AttributedString and possibly Font, but I can't get anything to work. And I've found no tutorials.
Does anyone have any quick pointers? That'd be most helpful!
Do you need the output field to be editable? If not, try using a JLabel with HTML code. Something like:
jLabel4.setText("<html>ax<sup>2</sup>+bx+c</html>");
You can add a border to that JLabel to make it look like a text field.
Why not use other swing components that support HTML to display the answer like JLabel. If you use a JLabel for instance you can use inline html to format your answer
The reasons you are having difficulty has to do with the lack of a default typsetting system. Font selection and such typically provides a very limited means to do proper math typesetting (which is a specialized subset of general typesetting).
I don't know of any math specific typesetting already built-in to the Java libraries, that said, perhaps you can integrate the ExTex project into your Java components or roll your own solution using the 2d API (look at baseline offsetting).
Other alternatives are to generate a graphic, and display it's rendered image. If the display is static, this might be a much easier choice.

Making words different colors in JTextField/JTextPane/?

I'm trying and failing to understand how to use Java's text editor components to colorize text as you insert it. I don't want or need a fully featured syntax highlighting library.
Basically, I have a JTextField (or some other JText... component), and a list of words. I want any words in the field that appear in the list to be red, and the rest of the words be green. So for example, if "fire" is in the list, "fir" would appear green and "fire" would appear red.
I've tried using a JTextPane and a DefaultStyledDocument, using a KeyListener to go over the text in the document and using AbstractStyledDocument.replace to replace the existing words with versions that have the correct attributes. This didn't do anything. What am I doing wrong?
Neither JTextPane nor JTextField isn't able to present formatted text, i.e text having more than one format. For text-editor-like capabilities like you'd find in WordPad or HTML, the component to use is the JEditorPane or its descendant, JTextPane.
The simplest thing you can do is set the ContentType of the JEditorPane to "text/html" and simply set its text to a string containing HTML. The Java structured text components are surprisingly competent with HTML; you can display tables and/or DIVs, and there is support for much of CSS2. Simplest to do your styles inline, but you can even do external style hrefs.
If you want to get fancy programmatically, you can access the DocumentModel and create text from spans of text each having their own formatting. The DocumentModel works essentially like a programmable text editor.
EDIT: Re-reading your question, I see my answer doesn't quite address it. Since you want multi-colored text JEditorPane is your only option; but rather than just piping in pre-colored text via HTML or such, you'll have to put a listener on your document model to catch changes introduced when you type; and after every document change you'll want to examine the text (again from the Document model) for text that should or should not be highlighted, and you'll want to apply formatting to certain runs of text.
There are devils in the details, but this should get you started.

Categories