4 Replies Latest reply: Jun 3, 2014 7:07 AM by Gen.Java RSS

    JScrollPane doesn't respond to client changes

    Gen.Java

      Hi all,

      When a JTextArea is created in a JScrollPane and the contents of the JTextArea change, the JScrollPane's preferred size doesn't change.

      In the following code, I create a JScrollPane and a JTextArea as a client. I then, add contents to the JTextArea and call revalidate() on it to let the JScrollPane re-update itself. However, it keeps reporting the same preferred size.

       

       

      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.*;
      class c
      {
      static JScrollPane sp;
      static JTextArea ta;
      public static void main (String...args)
      {
      JFrame f;
      JButton b;
      b=new JButton("ok");
      b.addActionListener(new a());
      ta=new JTextArea("hello");
      sp=new JScrollPane(ta);
      sp.setPreferredSize(new Dimension(100,70));
      f=new JFrame();
      f.setLayout(new FlowLayout());
      f.add(sp);
      f.add(b);
      f.pack();
      f.setVisible(true);
      }
      static class a implements ActionListener
      {
      public void actionPerformed(ActionEvent e)
      {
      System.out.println(sp.getPreferredSize());
      ta.append("this is a\nlarge piece\nof text\njust tomake\nthe height\nbigger");
      ta.revalidate();
      }
      }
      }
      
      

       

      What am I doing wrong here?

      Thank you

        • 1. Re: JScrollPane doesn't respond to client changes
          gimbal2

          ... why do you think the preferred size should change based on the content in the client component? Of course not :s The viewable area (and thus the preferred size) stays the same; the thing that changes in size is the -scrollable- area.

          • 2. Re: JScrollPane doesn't respond to client changes
            Gen.Java

            I made the following changes:

            1. added the line: JOptionPane.showMessageDialog(null,sp); at the end of the handler.

            2. commented out the line: sp.setPreferredSize(new Dimension(100,70));

            I found that the JScrollPane keeps getting bigger with adding more text. However, when I removed the comment and set the preferred size of the JScrollPane, the size remained fixed.

            This shows that the preferred size of the JScrollPane respond to the client change provided that it doesn't get set directly. If this is the case, the question is why?

            • 3. Re: JScrollPane doesn't respond to client changes
              gimbal2

              Not surprisingly, the API documentation provides information regarding that:

               

              JScrollPane (Java Platform SE 7 )

              • 4. Re: JScrollPane doesn't respond to client changes
                Gen.Java

                I need to keep the size of the JScrollPane inside the screen, so if the JTextArea is too big to fit, I will set the preferred size of the JScrollPane directly; if the JTextArea is small enough to fit, I will not set the preferred size and leave it to the JScrollPane to detemive its own size based on the size of the client. In my case, JScrollPane behaves as expected; but once I set its preferred size (when the client gets too big), it stops responding to the size of the client when the client gets smaller.

                I checked the API but couldn't get an answer.