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.
Related
Doing linguistics and phonetics, I often need to use certain special phonetic symbols. Although I'm using a special keyboard layout that enables me to write some of those characters by typing, they key combinations can often get both quite complex and highly repetitive, so I would like to create a litle app that would contain some buttons, perhaps, each of them capable of sending a specified (phonetic) symbol to whatever the current cursor position is, no matter what window on one's screen is in focus.
Is anything of this sort possible to do in Java?
I've seen a solution that copies the values into clipboard and then pastes them (Java paste to current cursor position), but that is not a very clean way to do it, is it? Is there a way better than just pasting the charactedr(s) via ctrl+V?
Many thanks for any help or advice in advance!
P.
You can use the AWT Robot to generate key press events. This will not provided the ability to insert arbitrary unicode characters but you can combine it with the technique you already described: transfer the unicode characters to the clipboard and generate a CTRL+V key event afterwards. You can try to save and restore the original clipboard content but this will work with types supported by Java only.
The focus problem mentioned in the comments can be solved by setting the window to not receive the focus via Window.setFocusableWindowState with an argument of false.
An alternative is to provide the unicode text via drag&drop. Most applications support dropping text in their input fields. The code for exporting the text is very similar as both, clipboard and d&d use the same interfaces in Java.
For a play button in a Java GUI I currently use a button with the label set to ' ▻ ' (found this symbol in a Unicode symbol table). As I understand, it is better to not use such symbols directly in source code but rather use the explicit unicode representation like \u25BB in this example, because some tools (editor, ...) might not be able to handle files with non-ASCII content (is that correct?).
Assuming the compiled class contains the correct character, under which circumstances would the GUI not show the intended symbol on a current PC operating system? Linux, Windows, Mac should all support UTF-16, right? Do available fonts or font settings cause problems to this approach?
(Of course I could add an icon, but why add extra resources if a symbol should already be available... given that this is a portable solution)
Do available fonts or font settings cause problems to this approach?
Unfortunately they do. You can use unicode in the source code of course, but the problem is that currently unicode has 246,943 code points assigned so obviously no font has even a fraction of those defined. You'll get squares or some other weird rendering when the glyph isn't available. I've had cases where relatively simple symbols such as ³ render fine on one Windows computer and show up as squares in the next, almost identical computer. All sort of language and locale settings and minor version changes affect this, so it's quite fragile.
AFAIK there are few, if any, characters guaranteed to be always available. Java's Font class has some methods such as canDisplay and canDisplayUpTo, which can be useful to check this at runtime.
Instead of using icons, you could bundle some good TrueType font that has the special characters you need, and then use that font everywhere in your app.
I currently use a button with the label set to ' ▻ '
rather than I always use JButton(String text, Icon icon), and Icon doesn't matter if is there this Font or another Font, UTF-16 or Unicode
Most of editors have support for unicode, so go on.
Look at this post: Eclipse French support
If you are using simple editor like notepad then when you save type name and below it choose UTF encoding ( http://www.sevenforums.com/software/72727-how-make-notepad-save-txt-files-unicode.html )
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.
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.
i want to use following symbols for buttons in my app:
arrows http://img402.imageshack.us/img402/3176/arrowso.jpg
here my code:
Button goToFirstButton = new Button("\uE318");
Button prevPageButton = new Button("\uE312");
Button nextPageButton = new Button("\uE313");
Button goToLastButton = new Button("\uE319");
and the result is
result http://img693.imageshack.us/img693/9063/resultbu.jpg
It seems, that \uE318 and \uE313 are wrong. What should i use instead? For goToLastButton and goToFirstButton i prefer to use this images
alt text http://img3.imageshack.us/img3/5724/singlearrow.jpg
but i can't find, which code should i use.
I would suggest to use Icons on Buttons instead of special characters, because the ability
to display may be strongly affected by availability of fonts on client workstation.
The unicode codepoints you want to use are part of a private use area, i.e. every font manufacturer is free to put whatever character they like at whatever position. The font you used to look up the arrow characters is simply a different font than the one used for displaying the button text. If the button text maps \uE318 and \uE313 to some Chinese (?) graphem, then that's not wrong, just different.
Although multiple people have made the argument you should avoid using these codepoints because you can't rely on the users' systems having a font which displays these characters, the reason you're getting the wrong characters in your example case has been entirely missed. All of the symbols you are trying to draw are in the "private use area" which means that the symbols involved will potentially be different in every single font.
The Unicode standard states:
Private Use Area (E000-F8FF)
* The Private Use Area does not contain any character assignments, consequently no character code charts or namelists are provided for this area.
If you embed the particular font you want to use to insure you can use these codepoints, that's fine. But that does mean you should, indeed, use the \u#### notation in your code, because embedding the characters as Unicode directly means the source won't make sense unless somebody views it in the correct font.
All in all, it's probably better to use icons unless you already have a symbol font you think is simply far superior to any graphical work you would otherwise do.
◀ ▶
There are a couple of symbols close to what you want in the Geometric Shapes chart. However, as others have said, use icons and stay out of the private use area.
Java source files are UTF-8 encoded, so you can put the symbols you want directly in the source code (just copy 'n paste from a font viewer or web), as long as you use a decent editor. No need to use this confusing "\uXXXX" notation. For instance, I've found this useful for Greek letters commonly used in scientific notation (δ, ρ, ψ...) - you can even use them as variable names.
Of course, your font of choice must have the symbols you want, otherwise it won't work.
I would suggest that if you are worried about the client font's ability to display a particular character, use the following:
private static final HashMap<Character, Font> fontCache = new HashMap<Character, Font>();
public static Font getFontThatCanDisplay(char c, int style, float size) {
Font f = fontCache.get(c);
if (f == null) {
f = UIManager.getFont("Label.font");
for (Font font: GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
if (font.canDisplay(c)) {
f = font;
break;
}
}
fontCache.put(c, f);
}
return f.deriveFont(style, size);
}
I also suggest using icons. Not all fonts have symbols for all unicode character points.
You may get it working by specifying a particular font. But what if that font is found on Windows 7 computers, but not on Mac OS X or Linux or Windows XP? Then the system will choose another font, based on the browser defaults, and that default might not have the symbols you want.
I will defend using Unicode glyphs on buttons, but it's really easy to implement the Icon interface and use paintIcon() to draw anything you want. The tutorial "Creating a Custom Icon Implementation" is a good example; this code shows a more complex, animated histogram.