Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8242410

JEditorPane with test/html type and zero margins is not shown

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      The issue was observed on all platforms Windows, Linux, MacOS.
      Windows: Windows 10 Enterprise N, Version 1709, Build 16299.1747
      Linux: 5.3.0-45-generic #37~18.04.1-Ubuntu SMP Fri Mar 27 15:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
      MacOS: Darwin Kernel Version 19.4.0: Wed Mar 4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64

      Java:
      openjdk version "15-ea" 2020-09-15
      OpenJDK Runtime Environment (build 15-ea+17-717)
      OpenJDK 64-Bit Server VM (build 15-ea+17-717, mixed mode, sharing)

      Also this issue was reproduced on previous versions:
      JDK 8 - "1.8.0_242"
      JDK 9 - "9.0.4+11"
      JDK 10 - "10.0.2+13"
      JDK 11 - "11.0.6+10"
      JDK 14 - "14+36-1461"

      A DESCRIPTION OF THE PROBLEM :
      When JEditorPane is created with the type test/html, and empty text and zero top and bottom insets, the text is not shown after updating it by the method setText(String)
      It happens because the height of JEditorPane remains equal to 0.
      In the case of non-zero top and bottom insets the height is updated and text is displayed properly.
      JTReg test in source code section demonstrates this issue.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      jtreg -testjdk:<path_to_tested_jdk> ZeroMargin.java
      or
      <path_to_tested_jdk>/bin/javac ZeroMargin.java
      <path_to_tested_jdk>/bin/java ZeroMargin

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Test passed
      ACTUAL -
      ----------configuration:(0/0)----------
      ----------System.out:(0/0)----------
      ----------System.err:(13/907)----------
      java.lang.RuntimeException: Height of JEditorPane with text/html type was not updated
      at ZeroMargin.main(ZeroMargin.java:49)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at com.sun.javatest.regtest.agent.MainWrapper$MainThread.run(MainWrapper.java:127)
      at java.base/java.lang.Thread.run(Thread.java:834)

      JavaTest Message: Test threw exception: java.lang.RuntimeException: Height of JEditorPane with text/html type was not updated
      JavaTest Message: shutting down test

      STATUS:Failed.`main' threw exception: java.lang.RuntimeException: Height of JEditorPane with text/html type was not updated

      ---------- BEGIN SOURCE ----------
      //ZeroMargin.zava
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.KeyAdapter;
      import java.awt.event.KeyEvent;

      /**
       * @test
       * @summary The test checks that height of JEditorPane with zero top and bottom insets is updated after changing text
       * @run main/othervm ZeroMargin
       */
      public class ZeroMargin implements Runnable {
          private JFrame f;
          private JEditorPane htmlPane;

          public static void main(String[] args) throws Exception {
              ZeroMargin test = new ZeroMargin();
              try {
                  Robot r = new Robot();
                  SwingUtilities.invokeAndWait(test);
                  r.waitForIdle();
                  r.keyPress(KeyEvent.VK_A);
                  r.keyRelease(KeyEvent.VK_A);
                  r.delay(500);
                  if (test.htmlPane.getHeight() == 0) {
                      throw new RuntimeException("Height of JEditorPane with text/html type was not updated");
                  }
              } finally {
                  SwingUtilities.invokeAndWait(() ->test.f.dispose());
              }
          }

          private void initEditorPane(JEditorPane pane, KeyAdapter adapter) {
              pane.setEditable(false);
              pane.setOpaque(false);
              pane.setMargin(new Insets(0, 1, 0, 1));
              pane.addKeyListener(adapter);
          }

          @Override
          public void run() {
              f = new JFrame();
              htmlPane = new JEditorPane("text/html", "");
              KeyAdapter adapter = new KeyAdapter() {
                  @Override
                  public void keyPressed(KeyEvent e) {
                      super.keyPressed(e);
                      if (e.getKeyCode() == KeyEvent.VK_A) {
                          htmlPane.setText("<html><body>html string</body></html>");
                      }
                  }
              };
              initEditorPane(htmlPane, adapter);
              f.add(htmlPane, BorderLayout.NORTH);
              f.setSize(300, 200);
              f.setVisible(true);
              f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Method 1:
      set initial text
      htmlPane = new JEditorPane("text/html", "<html><body>a/body></html>");

      Method 2:
      Set non-zero top and/or bottom margin
      pane.setMargin(new Insets(1, 1, 1, 1));

      Method 3:
      Force height recalculation
      htmlPane.setSize(htmlPane.getWidth(), Integer.MAX_VALUE);

      FREQUENCY : always


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: