I am designing a chat system..i am making use of JText area to display chat, and JTextField to enter text.
My question is how to recognize smiley's like ":)" and replace it by corresponding image
on text area? i found no method which will append image on text area..Please help.
http://java-sl.com/tip_autoreplace_smiles.html
You'll have to use a read-only JEditorPane to display HTML instead of a JTextArea in that case.
JTextArea was made to display only multiple rows of text, but correct me if I'm wrong. To display Images you could use the JEditorPane control that will allow you to use html, with simple <img /> tags which will point to a image.
Regarding how to recognize smileys you could create a file / list of common patterns that you would like to support and then simple check if the text contains the pattern with the .contains , or even Regular Expressions.
Update
And with the JEditorPane you will also be to do addition things like say scan for emails or links and automatically convert them so the user can click them, always a nice feature to have.
Related
At the moment I try to find a method to create a TextArea that I can edit and simply (without cheating) apply syntax highlighting to. Is that possible without the need of a custom Component? I already managed to format a text using a JEditorPane, but I am not sure how to implement that the text is dynamicly highlighted... And that efficiently. Is that possibly without an enormous amount of coding?
You can't use a JTextArea since it does not support text attributes.
Instead you can use a JTextPane. Read the section from the Swing tutorial on Text Component Features for more information and examples.
How do I create a GUI with a scrolling text area and underneath small paragraphs of text there are corresponding images?
I am currently using a JTextArea with a scroll-bar to display the text and it works great, but I want to display the corresponding images underneath each small paragraph of text.
You can use javax.swing.JTextPane
Check TextSamplerDemo
You can find plenty of examples here:
http://www.java2s.com/Code/Java/Swing-JFC/TextPaneSample.htm
The easiest way to do this, would be to use JEditorPane with HTMLEditorKitYou can use HTML text formnatting including graphics, just like a webpage.
I have a few records of data (less then 10). Each record consists of a few lines of text.
I want to present records to the user in a kind of grid, where user can select one of the records.
I was thinking about List component or jTable, but I couldn't make them displaying more then one line of text. What component should I use then, or how to approach this?
In subject I suggested AWT because size does matter, i.e. I want use this functionality in the applet and would like to avoid any extra libraries.
Thanks in advance
Thanks to maksimov's link I found examples of how to tackle this issue, and also very interesting link I missed somehow - http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
To specify that a component's text has HTML formatting, just put the
tag at the beginning of the text, then use any valid HTML in
the remainder. Here is an example of using HTML in a button's text:
button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");
In my case it was just enough to set height of the row and add tag just before string data to be displayed. HTML tagging also let me use extra formatting, colors, etc,
Brilliant,
Thank you maksimiov
I am trying to make a simple email client in Java Swing.
I want to allow users to format their email in any way they want, like making some parts of the text bold, other parts italic, etc. In other words, I am trying to make a WYSIWYG editor. The formatting is done in HTML. I am using JEditorPane to display the text.
I have tried adding tags myself to the text directly by using setText and getText methods of JEditorPane. I could make it work for basic formatting, but it is quite difficult to handle complex formatting. (trying to remove tags from multi-tagged elements, for example)
Is there an easier way to accomplish this? I have looked at HTMLEditorKit but it seems like it does not support adding tags to and/or replacing a specific string.
Thanks in advance.
The HTMLEditorKit comes with some default Actions that allow you to do some basic styling of the text with the click of a menu item (or button). Take a look at the example in the section from the Swing tutorial on Text Component Features.
I have a JScrollPane that holds a JLabel using the following code:
//Create TEXT LOG JPanel
textLogPane = new JScrollPane(logLabel);
textLogPane.setPreferredSize(textLogPaneDim);
//textLogPane.setOpaque(true);
textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
textLogPane.getViewport().setBackground(Color.DARK_GRAY);
The JLabel, logLabel, is represented by a string with an HTML encoding using for carriage returns. I display certain images based on the contents of certain lines and I would like to be able to scroll the JScrollPane, textLogPane, to show that line whenever I am displaying that graphic. I know the contents of the line that I want to display but I can't seem to figure out how to get it to scroll down(or up) to the relevant line.
If need be I can change to something other than a JLabel as long as I can keep the HTML encoding and have it look just like multiple lines of text.
Sorry if this is a repeat I tried searching but couldn't find any results.
Thanks
You can do some custom maths and use scrollRectToVisible() in your viewport. I don't know how to compute the rect of a specific line in your JLabel. A better solution would be to stick your strings into a JList instead, perhaps with a custom renderer for the html, and use
list.ensureIndexIsVisible(list.getSelectedIndex());
You don't use "carriage returns" in HTML, you use "br" tags.
I would suggest you should probably be using a JTextPane for multiline text. I also find it easier to not use HTML, but instead to add strings with attributes. You can also insert icons into a JTextPane.
Read the section from the Swing tutorial on Using Text Components for a working example.