Display Emoji character in JTable

2019-07-02 15:03发布

问题:

I want to display loudspeaker character in a JTable:

    @Override
    public Object getValueAt(int row, int col)
    {
        switch (col) {
            [...]
            case 2:
                String symbol = "\uD83D\uDD0A";
                return "State " + symbol;
            default:
                return "";
        }
    }

Unfortunaly i just see an square box. I'm not sure if i have to set an specific Font supporting this character or to apply an other encoding.

For Googlers looking for a solution:
I implemented a CustomRenderer for the JTable like @trashgod sugested. Examples are available here or here.

回答1:

Yes, you'll need two things:

  • A font containing the required glyph, loaded as shown here.

  • A TableCellRenderer in which to use the font, as shown here.

Alternatively, consider a TableCellRenderer that implements the Icon interface, as shown here. The graphics context's fill(Polygon) and drawArc() methods should produce a satisfactory result.