Margub Khan
Margub Khan

Reputation: 21

Java21: Illegal Rectangle is drawn while painting date time

image Above image shows a rectangle which is in between time-value and am, In Java21.0.3, DateTime string has a character NNBSP which is converting to rectangle while painting it.

Following code illustrates the issue:

public class RunningStringPanel extends JPanel implements ActionListener {
    private String text;

    public RunningStringPanel(String text, int delay) {
        this.text = text;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setFont(new Font("MS Gothic", Font.ITALIC, 24));
        System.out.println(text);
        g.drawString(text, 10, 50); // Draw the string up to the current index
        System.out.println();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Running String");
        RunningStringPanel panel = new RunningStringPanel(getText(), 200);
        frame.add(panel);
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static final Long LONG_TIME = 1573430400000L;

    public static String getText() {

        Date created = new Date(LONG_TIME);
        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
        String value = format.format(created);
        return value;
    }

}

What we tried to solve the issue

  1. setting stdout.encoding and stderr.encoding to UTF-8
  2. Use latest DateFormatter APIs also, that also results same
  3. Change Windows Locale to UTF-8

Question: is there a way not to print rectangle in the string?

Upvotes: 0

Views: 29

Answers (0)

Related Questions