5 Replies Latest reply: Aug 1, 2013 11:48 PM by UdayaShankaraGandhi RSS

    Scrollpane scrollbars to entire Jframe when resizing/adding components

    GiorgosM

      Hi,i am trying to add scrollbars to my frame containing many different components and appearing when user resizes the window with the mouse

      adds pictures and admin of this online application adds labels or other components.

      I want scrollbars to appear when the frame  is resized and has components you don't see under so you can scroll down.

       

      If i have for example this code, how i add the scrollbars when i make frame smaller ?

       

       

      public class Test extends JFrame {
      
      private JPanel contentPane;
      
      /**
       * Launch the application.
       */
      public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
         public void run() {
         try {
         Test frame = new Test();
        frame.setVisible(true);
      
         } catch (Exception e) {
        e.printStackTrace();
         }
         }
         });
      }
      
      /**
       * Create the frame.
       */
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new SpringLayout());
      
         //Suppose that here we have many jlabels,jbuttons,jtextfields and other
      }
      
      } }
      
        • 1. Re: Scrollpane scrollbars to entire Jframe when resizing/adding components
          UdayaShankaraGandhi

          whenever you are in need of getting scrollbars (both horizontal and vertical) if you re size your jframe smaller, you need to use JScrollPane component. JScrollPane is a containter which holds other components like jlabels, jbuttons, jtextfields and other.

           

          Here i have given small snippet which is having a JFrame holding TextArea component.

           

          public class SampleFrame {
          
            public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
            createAndShowGUI();
            }
            });
            }
          
            public static void createAndShowGUI()
            {
            JFrame frame = new JFrame("JFrame with ScrollBar");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
            JTextArea textArea = new JTextArea(5,20);
          
            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            frame.getContentPane().add(scrollPane);
          
            frame.setSize(300, 200);
            frame.setVisible(true);
            }
          }
          

           

          If you run the program, you dont get any scrollbars as your text area is not having any content in it. Try to write some number of lines of code and as the number of lines increases you will find both scrollbars whenver they needed. Or if you make your frame as smaller also you would get scrollbars.

           

          If you need scrollbars always irrespective of the content use scrollpane policy as JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS,

          if you need scrollbars whenver they needed then use scrollpane policy as JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

          if you dont need scrollbars, then use scrollpane policy as JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED

           

          here i have taken TextArea as my component, if you need more components then add them to a JPanel and add that japnel to scrollpane. You will get scrollbars when you make your frame smaller.

          • 2. Re: Scrollpane scrollbars to entire Jframe when resizing/adding components
            GiorgosM

            Hi,thanks for the answer but still cant find solution for other components.

            Help me with something.You said "if you need more components then add them to a JPanel and add that japnel to scrollpane. You will get scrollbars when you make your frame smaller."

            I tried this in the past and now, and i get " java.lang.IllegalArgumentException: adding container's parent to itself "

             


            Look what i write and tell me what's wrong

             

            public class Test extends JFrame  {
            
                private JPanel contentPane;
                //private JLabel lblNewLabel_1;
                
                /**
                 * Launch the application.
                 */
                public static void main(String[] args) {
                    EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            try {
                                Test frame = new Test();
                                frame.setVisible(true);
                                
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            
                /**
                 * Create the frame.
                 */
                public Test() {
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setBounds(100, 100, 1200, 900);
                    setLocationRelativeTo(null);
                    contentPane = new JPanel();
                    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                    setContentPane(contentPane);
                    SpringLayout sl_contentPane = new SpringLayout();
                    contentPane.setLayout(sl_contentPane);
                    
                    
                      
                      JLabel lblNewLabel = new JLabel("New label");
            sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 366, SpringLayout.NORTH, contentPane);
                     sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel, -502, SpringLayout.EAST, contentPane);
                     sl_contentPane.putConstraint(SpringLayout.SOUTH, lblNewLabel, -245, SpringLayout.SOUTH, contentPane);
                     sl_contentPane.putConstraint(SpringLayout.EAST, lblNewLabel, -368, SpringLayout.EAST, contentPane);
                      contentPane.add(lblNewLabel);
                      
                      
                      
                      JScrollPane scrollPane = new JScrollPane(contentPane);  // Here you say that i add the panel to the scrollpane
                      scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);  
                      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);  
                      Test.this.getContentPane().add(scrollPane);  //but here i get adding container's parent to itself 
                    
                }
            }}
            

             

            Also when i write JScrollPane scrollPane = new JScrollPane(lblNewLabel); the location and the size of the label changes. Thanks in advance

            • 3. Re: Scrollpane scrollbars to entire Jframe when resizing/adding components
              UdayaShankaraGandhi

              I got your mistake.

              Here you are trying add a JPanel to itself causing "java.lang.IllegalArgumentException: adding container's parent to itself". Observe line numbers 30 and 48 in your code.

               

              setContentPane(contentPane);  // (Line Number 30 in your code) Here you are setting your jpanel instance contentPane to contentpane. Now JFrame's contentpane is your jpanel instance.
              
              

               

               

              Test.this.getContentPane().add(scrollPane);  // (Line Number 48 in your code) Here frame's contentpane is now your jpanel instance created in line number 30. But again you are trying to add scrollpane which is holding your jpanininstance to jframe's contentpane which means your jpanel instance. so you are getting illlegalArgumentException.                               
              
              

               

              so comment Line Number 30 in your code and run it again, you wouldn't get any error. \

              When you are calling

               

              getContentPane().add(lblNewLabel);

               

               

              in your code will not generate any error as you are adding the jlabel instance to frame's contentpane - your jpanel instance.

               

              And to answer your second question regarding jlabel position - its may be due to layout you are setting. I didn't worked with SpringLayout much. so i am not able to answer your question.

              • 4. Re: Scrollpane scrollbars to entire Jframe when resizing/adding components
                GiorgosM

                I think it can't work with SpringLayout.This is the fault.

                Not only the entire frame,but not even a label with images for example or text areas.

                Anyway.thanks for trying to help.

                • 5. Re: Scrollpane scrollbars to entire Jframe when resizing/adding components
                  UdayaShankaraGandhi

                  If your question got answered then make it as answered.