printable char in java

Does anyone knows how to detect printable characters in java?

After a while ( trial/error ) I get to this method:

    public boolean isPrintableChar( char c ) {
        Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
        return (!Character.isISOControl(c)) &&
                c != KeyEvent.CHAR_UNDEFINED &&
                block != null &&
                block != Character.UnicodeBlock.SPECIALS;

I'm getting the input via KeyListener and come Ctr-'key' printed an square. With this function seems fairly enough.

Am I missing some char here?

Asked by: Miranda504 | Posted: 21-01-2022

Answer 1

It seems this was the "Font" independent way.

public boolean isPrintableChar( char c ) {
    Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
    return (!Character.isISOControl(c)) &&
            c != KeyEvent.CHAR_UNDEFINED &&
            block != null &&
            block != Character.UnicodeBlock.SPECIALS;

Answered by: Emily561 | Posted: 22-02-2022

Answer 2

I'm not perfectly sure whether I understand your problem. But if you want detect if character can be drawn to Graphics object, and if not print some placeholder char you might find usefull:


It will check whether font can display specific codepoint (it is more that check whether font is displayable at all -- since there are chars that are displayable - like Ä… - but some fonts cant display them.

Answered by: Brad392 | Posted: 22-02-2022

Similar questions

How to find out if a Unicode code point is a "normal" printable one in Java?

I have to explain what I mean with "normal": the character has (or should have) a (not completely white) glyph unless it is in the Space Separator category (Character.getType(codepoint)==Character.SPACE_SEPARATOR) and if using a monospaced font it should occupy one position and advance the cursor one position to the right under all circumstances. Counter-examples: U+...

printing - Size Mismatch Between Swing Fonts and Printable Fonts in Java

I've got a panel displaying a JTextPane backed by a StyledDocument. When I print a string of text in, say Arial 16, the text it prints is the same size as the Arial 16 Word prints. However, the Arial 16 in the JTextPane appears to be smaller than the Arial 16 Word displays. Is there some sort of flaw in the translation of Swing fonts to Windows system fonts or something of the sort that makes it difficult (or impossible...

java - All parts of my Printable Swing component doesn't print

I'm trying to do a printable component (an invoice document). I use JComponent instead of JPanel because I don't want a background. The component has many subcomponents. The main component implements Printable and has a print-method that is calling printAll(g) so that all subcomponents should be printed. But my subcomponents doesn't print. What am I missin...

How can I "pack()" a printable Java Swing component?

I have implemented a Java Swing component that implements Printable. If I add the component to a JFrame, and do this.pack(); on the JFrame, it prints perfect. But if I don't add the component to a JFrame, just a blank page is printed. This code gives a great printout: final PrintablePanel p = new PrintablePanel(pageFormat); new JFrame() {{ getContentPane().add(p); this.pack...

database - How to generate printable (and savable) forms with Java?

Here is my situation. I have written a Database application in Java. Now (unfortunately as an afterthought) I want to generate forms from data I pull from the database. By form I mean a savable and printable file (not anything to do with UIs or swing). Basically I want to take a template form and fill in things from the data I pull from the database. Is there any simple API for this? I really don't care abo...

java - How to show the printable document directly in acrobat with out saving?

I am using PD4ML to print a PDF file and It is working fine. Now the thing is I want show that file directly in acrobat with out save that file. In Local version I am using Program.launch(getFilePath()); It is working fine but in web version I am unable to get that. Can you please suggest me, Its very helpful. Thanks, Vara Kumar PJD

java - How to show the printable PDF document with in the browser?

I want show the printable document directly with in the browser when I clicked on print button in my application. I already checked the 'Display PDF in browser' in adobe reader preferences and other PDF files are also being opened in my browser other than that my printable documents. Can you please suggest me Thanks, Vara Kumar PJD

java - when is the print method of printable interface called in this code and ...?

// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.print.PrinterException; import java.awt.print.*; class Printer extends JPanel implements Printable { JButton print; Printer() { buildGUI(); hookUpEvents(); } public void buildGUI() { JFrame fr = new JFrame("Program to Print on a Printer"); JPanel p = new JPanel...

java - Printable pairs?

Is there a way to create printable pairs? pair.toString() looks like this: android.util.Pair@fd55fdb8 , it is very hard to debug when I need to print a lot of arrays which contain those pairs.

How to make a class printable using Java/Groovy

Can I make a class printable (i.e. print(x) works) by overriding its toString() method, or is there some other way? toString() requires creating a string, which I imagine would involve a lot of wasted concatenation, particularly for nested structures. It would seem to be more sensible if there was a print(PrintStream os) method available, but I couldn't find one.

java - How to convert a long into printable bytes

I have a string which represent a long. Like "12345678901" (11 chars long). I convert it into a long using Long.parse(), that's fine. Now, I want to send this long as a short string, like "eR%s" over the wire. The goal is to have this final string as short as possible. Any idea what's the best way to do that? I can use more characters as the URL encoding (Like...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)