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

Layout of HTML text in JLabel

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.2.2
    • client-libs



      Name: wl91122 Date: 08/30/99


      This may be the same bug as 4259549, but I'm unsure and submitting
      regardless.

      When a JLabel has multiple lines of HTML, it appears that
      JLabel doesn't know his size until after it is painted on screen.

      When multiple components are on the page, the whole frame seems
      to readjust right after it's visible. This gives the UI a very
      sophmoric look and is very undesirable.

      In the code below, there are two panels in a card layout with
      multiple components in each panel. The JLabel is at the very
      top of each panel, and when it finally figures out its size,
      all components below it suddenly drop down. The problem is made
      worse the more lines the JLabel needs to paint its text.

      ---------------------------------------------------------------

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class LabelTest {
      public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setSize(400,400);
      JPanel buttonPanel = new JPanel();
      buttonPanel.setLayout(new BorderLayout());
      JButton back = new JButton("1");
      buttonPanel.add(back, BorderLayout.WEST);
      JButton next = new JButton("2");
      buttonPanel.add(next, BorderLayout.EAST);
      final JPanel contentPanel = new JPanel();
      final CardLayout cLayout = new CardLayout();
      contentPanel.setLayout(cLayout);
      f.getContentPane().setLayout(new BorderLayout());
      f.getContentPane().add(buttonPanel, BorderLayout.NORTH);
      f.getContentPane().add(contentPanel, BorderLayout.CENTER);

      next.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt) {
      cLayout.show(contentPanel, "2");
      }
      });

      JPanel q1 = new JPanel();
      q1.setLayout(new BorderLayout());
      JLabel stv11 = new JLabel("<html><font size=\"3\" face=\"Serif\"> " +
      "We are trying to determine what is happening with the so called <i>distracter hopping</i> problem." +
      "I hope this gives us more information on how to fix the problem." +
      "The problem only shows when the panel is initially visible.<br><br></html>");
      q1.add(stv11, BorderLayout.NORTH);
      JPanel dp1 = new JPanel();
      dp1.setLayout(new GridLayout(2,2));
      JToggleButton jtb = new JToggleButton("A");
      dp1.add(jtb);
      JLabel stv2 = new JLabel("<html><font size=\"3\" face=\"Serif\">Problem #1. This is a huge issue. It makes our UI look amateur. Help Me Obi Wan.</html>");
      dp1.add(stv2);
      dp1.add(new JToggleButton("B"));
      JLabel stv3 = new JLabel("<html><font size=\"3\" face=\"Serif\">Problem #2</html>");
      dp1.add(stv3);
      q1.add(dp1, BorderLayout.CENTER);

      contentPanel.add(q1, "1");
      back.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt) {
      cLayout.show(contentPanel, "1");
      }
      });



      JPanel q2 = new JPanel();
      q2.setLayout(new BorderLayout());
      JLabel jstv1 = new JLabel("<html>The problem is made <b>WORSE</b> by " +
      "having a very long line of text in the <code>JLabel</code>." +
      " The longer it gets, the more of a 'hop' we notice in the window." +
      "<br><br> I have spent some time looking for a workaround, but I have been unsuccessful thus far." +
      "<br><br> It looks as though <code>JLabel</code> doesn't know it's preferred size" +
      " until after its painted. When it's laid out, it gets a preferred size" +
      " of one line only, then realizes it needs to wrap text." +
      "</html>");

      q2.add(jstv1, BorderLayout.NORTH);
      JPanel dp2 = new JPanel();
      dp2.setLayout(new GridLayout(2,2));

      final JToggleButton jb1 = new JToggleButton("A");
      dp2.add(jb1);
      JLabel jstv2 = new JLabel("<html>Problem #1. <b>DOH!</b></html>");
      dp2.add(jstv2);
      dp2.add(new JToggleButton("B"));
      JLabel jstv3 = new JLabel("<html>Problem #2. <b>DOH!</b></html>");
      dp2.add(jstv3);
      q2.add(dp2, BorderLayout.CENTER);

      contentPanel.add(q2, "2");
      f.setVisible(true);
      }
      }
      (Review ID: 94611)
      ======================================================================

            tprinzing Tim Prinzing (Inactive)
            wleesunw William Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: