im trying to implement a Chat feature in my application. i have used 2 JEditorPane. one for holding chat history and the other for sending chat to the previous JEditorPane.
the JEditorPane is text/html type.
the problem i am having is when i put more than one space between characters it is automatically removed by the parser because it is HTML!
how can i make it so, that the spaces are not stripped?
example: hello world
becomes: hello world
also i am having to parse the html tags so the new messages can be added to the history window.
is there a better option than using JEditorPane? if i used JTextPane would it be easier to implement?
i would like the chat boxes/panes to be able to handle bold, URL embedding for now.
thank you and look forward to your guidance.
EDIT: im trying to replace " " with a relavent char.
newHome[1] = newHome[1].replace(" ", newChar)
what should be the newChar value?
EDIT: im trying:
newHome[1] = newHome[1].replaceAll(" ", " ");
but it is not producing the results. any ideas?
EDIT: #Thomas - thanks! for some reason i can post a note to your answer.
Using HTML markup is a quick way to get simple text formatting done in a Swing text component. However, it's not the only way.
A more sophisticated method is to use a javax.swing.text.StyledDocument to which you can attach different "styles" (hence the name). A style is basically a set of attributes, for instance, whether the text should be in bold or italics or what Color it should have.
JTextPane provides a number of convenience methods to deal with styles, and it is a subclass of JEditorPane which means it should integrate rather seamlessly into your existing code. As an example, to mark a portion of the text within a JTextPane as bold, you could use something like this:
JTextPane textPane = new JTextPane();
Style bold = textPane.addStyle("bold", null);
StyleConstants.setBold(bold, true);
textPane.setText("I'll be bold.");
textPane.getStyledDocument().setCharacterAttributes(8, 4, bold, true);
Similarly, you could define a second style that e.g. uses a blue, underlined font and which you could use to display hyperlinks.
Unfortunately, the downside is that you will have to take care of the mechanics of the links yourself. Although you can use the existing infrastructure of javax.swing.event.HyperlinkListener et al., you will be responsible for detecting mouse clicks. The same goes for hovering and changing the Cursor into a hand-symbol etc.
Related
I have a JTextArea which has its text set to a string of information. In this string of information I have a variable which I would like coloured red, to do this I edit the string as follows:
"Result: <html><font color=red>" + negativeValue + "</font></html>"
I would expect this to give Result: ## where the number is red. However it just puts the following into the text area:
Result: <html><font color=red>##</font></html>
I'm not really sure how to get this working, so could someone offer advice as to how to do so?
JTextArea is not a component designed for styled text. If the text can be all one color, call setForeground(Color).
Otherwise use a styled text component such as a JEditorPane or JTextPane. For more info. on using them, see How to Use Editor Panes and Text Panes.
Also as pointed out by others, the entire String must start with <html> .
You cannot use HTML in a JTextArea, but you can use it with a JEditorPane
JTextArea doesn't support styled text area, in order to render HTML you need an instance of JEditorPane or JTextPane. See the tutorial on Using Text Components
.
Sample code here
If memory serves, JTextArea is for plain text display only. For HTML, you must use JEditorPane or one of its subclasses.
Java renders html code only if it starts with <html>. You should try it like
"<html>Result: <font color=red>" + negativeValue + "</font></html>"
Can we change the String Style in java from normal to bold...
Example: I wish to change the String "Name" from normal Style to bold Style. Any default
method included in java?
The question is a bit vague, but in Swing you can use HTML in order to do format the way your strings appear in components:
jLabel1.setText("<html><b>Bold text</b></html>");
jButton1.setText("<html><b><i>Bold and Italic text</i></b></html>");
A string is just the representation of some text. Applying some style information depends on the GUI framework you use: Swing, AWT, JSP, etc...
It depends where do you want to change its style ?
If you are using Swings' components or AWT or something like that then surely you can do it.
A String object in Java just holds the data. It does not have any styling associated with it. Display styles are associated with the presentation components. You sure are missing something.
If you are using a GUI System like a JLabel, JTextField etc, then you can go two ways:
eg:
JLabel myLabel = new JLabel("<html><b>My Text");
or
Font F = new Font("Calibre",Font.BOLD,12);
myLabel.setFont(f);
Hope that answers you Question.
There are two ways,
Either create a font by yourself ,
something like, Font abc = new Font("Arial",font.BOLD,15)
and for any of the components you can use the setFont() method to set the font for the component.
See the javadoc for more
Use html,
Java supports html formating on all of its swing components.
Here is How to Use HTML in Swing Components.
Any default method included in java?
So I suppose for you its better to use a font, while setting the style via html means, if only for that text ,its not default for the component.
This is a generalized answer, for more specific answer,ask more specific question :)
In my Java application I want to output striked letters (like html tag do). Is there any way to do this using Unicode (combine )
You can use U+0336, the combining long stroke overlay, to accomplish this task.
The official Combining Diacritical Marks unicode chart lists "strikethrough" as an 'informative alias', meaning that this is is the official specified purpose of this character.
0336 ̶◌ COMBINING LONG STROKE OVERLAY
= strikethrough
• connects on left and right
For comparison, here is U+0336 compared to the html <strike> tag:
U̶n̶i̶c̶o̶d̶e ̶c̶o̶m̶b̶i̶n̶i̶n̶g̶ ̶l̶o̶n̶g̶ ̶s̶t̶r̶o̶k̶e̶ ̶o̶v̶e̶r̶l̶a̶y̶
Hypertext strike tag
Note that many font rendering engines do not render U+0336 correctly, and when possible one should use markup formatting or another mechanism. Depending on your browser, the above text likely has a large gap in the line around the "m" in combining, and #Alex78191 reports that it renders so low for them that it looks more like an underline than a strikethrough.
For this reason, one should still prefer HTML another markup technology over U+0336 for this purpose, given the option.
No, this is not possible. While there is the concept of a stroke as diacritic, it's not available as a separate Unicode character, probably because the various letters that use a stroke diacritic do not place it at the same height or even angle. So the result would not resemble strikethrough markup anyway.
To output strikethrough text in Java, you need to use an output format that allows you to use explicit markup. If you have a Swing app, you're in luck as many Swing components support HTML. Otherwise it depends on what presentation technology you're using.
As said before, Unicode doesn't do that, but a lot of Swing components understand basic HTML tags.
JLabel label = new JLabel("<html><s>My stroke</s></html>")
No. Unicode does not define a combining strikeout mark. Unicode's view is that this is the job of markup -- like HTML.
I am creating a java app that needs to display multiple documents worth of plain text. I have been playing around with JEditorPane (for each document to display text) with JTabbedPane (for document selection) but I think I would prefer to use something better (if it exists).
Is there an existing class that creates a dead simple editor with document selector functionality built in?
Nothing is built-in to do this, but JDesktopPane might be a better option for selection rather than a JTabbedPane.
For plain text, I would tend to prefer JTextArea "that displays multiple lines of text and optionally allows the user to edit the text." It is described in the article How to Use Text Areas. The append() method is particularly convenient. The articles Text Area Scrolling and Message Console are also very helpful.
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.