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

JScrollPane height is wrong when containing a JTextPane

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • client-libs
    • beta
    • x86
    • windows_nt, windows_2000



      Name: yyT116575 Date: 12/15/2000


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symjit)

      Execute this code:

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

      public class Test extends JFrame
      {

        public Test()
        {
            setSize(700, 300);
            addWindowListener(new WindowAdapter()
            { public void windowClosing(WindowEvent e)
               {
                 System.exit(0);
               }
            } );

            JTextPane m_TextArea = new JTextPane();
            m_TextArea.setText(texto);

            JViewport m_Viewport = new JViewport();
            m_Viewport.add(m_TextArea);

            JScrollPane m_ScrollPaneArea = new JScrollPane();
            m_ScrollPaneArea.setViewportView(m_Viewport);

      m_ScrollPaneArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

      m_ScrollPaneArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

            JPanel m_ParaPanel = new JPanel();
            m_ParaPanel.add(m_ScrollPaneArea);
              
            getContentPane().add("Center", m_ParaPanel);
         }

         public static void main(String[] args)
         {
           JFrame f = new Test();
           f.show();
         }

         private String texto = "k lk d kqjfh lkjhsf sqdd qs shkjdh kjh
      qlkjsdlk\njqsdjjjjjjjjjqslk qlkd \nlkdkl dkdqs\nkjjkl dqslk lk d\ntest";
      }


      Problem:
      If executed, the scroll pane appears with a very small height (about 6 pixels)
      and if you resize the frame, the scroll pane is then expanded to the size it
      should have had at the beginning. It seems that the viewport doesn't get the
      size given by the JTextPane...

      If you replace the JTextPane with a JTextArea, you won't get this problem.
      Therefore, I suppose this is a bug.

      I need this piece of code to set the minimum size of a scroll pane depending on
      the text entered by the user.

      (Review ID: 113855)
      ======================================================================

      Name: yyT116575 Date: 12/15/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      //
      // This creates a TextPane, and uses it in 5 places
      // - the table header
      // - table cell 0,0
      // - inside a ScrollPane (as the last Component added, and as an
      // intermediate Component)
      // - outside the table altogether
      //
      // Only the last case is rendered as expected, the first two cases look as
      // though the size is thought of as 0-height.
      // - the header is collapsed down to 1 pixel
      // - the table cell is the same height as other (smaller) table cells
      //
      // The TextPane embedded in a ScrollPane is incorrectly sized if it is the
      // last Component.
      //
      import java.util.*;
      import java.awt.*;
      import javax.swing.text.html.*;
      import javax.swing.text.*;
      import javax.swing.table.*;
      import javax.swing.*;

      public class Untitled1 extends JPanel {

        public Untitled1() {
            AbstractTableModel model = new AbstractTableModel() {
                public int getRowCount() { return 2; }
                public int getColumnCount() { return 2; }
                public Object getValueAt(int row, int column) { return new Integer(row); }
                public void setValueAt(int row, int column) { }
                public String getColumnName(int column) { return "hello"; }

            };
            JTable table = new JTable(model) {
                public TableCellRenderer getCellRenderer(int row, int column) {
                    if (row==0 && column==0) {
                        return new TableCellRenderer() {
                            public Component getTableCellRendererComponent(JTable table, Object value,
                                                                boolean isSelected, boolean hasFocus,
                                                                int row, int column) {
                                return createArea();
                            }
                        };
                    }
                    return super.getCellRenderer(row, column);
                }
            };

            table.getTableHeader().setDefaultRenderer(
                new TableCellRenderer() {
                    public Component getTableCellRendererComponent(JTable table, Object value,
                                                        boolean isSelected, boolean hasFocus,
                                                        int row, int column) {
                        return createArea();
                    }
            });
            add (new JScrollPane(table));

            add (new JScrollPane(createArea()));

            add (createArea());

            add (new JScrollPane(createArea()));

        }

        public Component createArea() {
            JTextPane area = new JTextPane();
            //JTextArea area = new JTextArea();

            DefaultStyledDocument doc = new DefaultStyledDocument();

            SimpleAttributeSet style = new SimpleAttributeSet();

            StyleConstants.setFontFamily(style, "Ariel");
            StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
            StyleConstants.setFontSize(style, 24);
            StyleConstants.setBold(style, true);
            StyleConstants.setItalic(style, true);
            StyleConstants.setForeground(style, Color.orange);
            StyleConstants.setBackground(style, Color.blue);

            area.setDocument(doc);
            area.setEditable(false);

            try {
                doc.insertString(0, "title", style);
                doc.setParagraphAttributes(0, doc.getLength(), style, true);
                doc.setCharacterAttributes(0, doc.getLength(), style, true);
            } catch (Exception e) { e.printStackTrace();}

            return area;
        }

        public static void main(String[] args) {
          Untitled1 untitled1 = new Untitled1();
          JFrame frame = new JFrame();
          frame.getContentPane().add(untitled1);
          frame.setSize(800,600);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.show();
        }
      }
      (Review ID: 113803)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: