Results 1 to 18 of 18
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Why is it not scrolling

    Why is it not scrolling?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    /**
     *
     * @author willem
     */
    public class PagePane  extends JPanel {
        
        public PagePane(JFrame frame) {
            Container contentPane = frame.getContentPane();
            JScrollPane scrollPane = new JScrollPane(this,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            contentPane.add(scrollPane, BorderLayout.CENTER);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
             g.drawRect(50, 50,700, 850);
             g.drawRect(50, 950,700, 850);
          
        }
    
        public static void main(String[] arg) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Willem Writer");
            frame.setBounds(400, 30, 800, 1000);
            PagePane pp = new  PagePane(frame);
            frame.setVisible(true);
         }
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: Why is it not scrolling

    Painting in a scrolled panel does not cause the panel to scroll. I can't give the exact reason because I have not investigated it but I do not believe that scroll pane will react to a JPanel exceeding its physical boundaries when painting. It is scrolled to accomodate differently sized components. If your scrolled JPanel is larger in size than your containing JPanel or JFrame, then it will scroll.

    Also, in case you didn't know. Frame.add() will add components to the contentPane. This is a convenience method. Nothing wrong with the way you did it.

    Regards,
    Jim
    Last edited by jim829; 03-04-2014 at 04:19 PM. Reason: typo - panel = s/smaller/larger
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Why is it not scrolling

    Indeed, to make it scroll you'd have to use the scrollRectToVisible() method on the component INSIDE the scrollpane. Man I had trouble using that method because I was mistakenly calling it on the JScrollPane itself while it must be called on the component that you are putting inside it.

    The Swing tutorial has a small example:

    How to Use Scroll Panes (The Java Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Why is it not scrolling

    A component will scroll when the preferred size of the component is greater than the size of the scrollPane. You need to override the getPreferredSize() method of your PagePane to return the size of the panel.

    Also, You should NOT have code in the PagePane class to add the panel to a scrollpane.. That code should be moved to the code that actually builds the GUI. Something like:

    Java Code:
    PagePane pp = new  PagePane();
    JScrollPane scrollPane = new JScrollPane(pp);
    
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Willem Writer");
    frame.setBounds(400, 30, 800, 1000);
    //PagePane pp = new  PagePane(frame);
    frame.add(scrollPane); // the default is CENTER so you don't need to specify it here
    frame.setVisible(true);

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: Why is it not scrolling

    Indeed, camickr, that, did it....
    Gimbal2 I am very pleased to know that I am not the only
    having a hard time....

    Java Code:
    public class PagePane  extends JPanel {
        
        public PagePane() {
           
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
             g.drawRect(50, 50,700, 850);
             g.drawRect(50, 950,700, 850);
          
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(500, 2000);
        }
    
    
        public static void main(String[] arg) {
            PagePane pp = new  PagePane();
            JScrollPane scrollPane = new JScrollPane(pp);
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Willem Writer");
            frame.setBounds(400, 30, 800, 1000);
            //PagePane pp = new  PagePane(frame);
            frame.add(scrollPane); // the default is CENTER so you don't need to specify it here
            frame.setVisible(true);
         }
    
    }

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: Why is it not scrolling

    Imaging this setup as a point of departure of a page view of a text component
    like JTextPane.
    Any suggestions what other directions to take...
    I found this code on the net (with the Java Rangers) but it is till rather mysterious to me

    pagination in jtextpane (Swing / AWT / SWT forum at JavaRanch)

    For sure I have to pay attention to the ViewFactory
    Because we are talking about the View part of the MVC, right?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Why is it not scrolling

    oops, I misunderstood the requirement. I thought this was about scrolling to the spot of the scrollable area where something changed, but it is about making scrolling possible to begin with.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: Why is it not scrolling

    No problem, so you had a misunderstanding of my misunderstanding....

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: Why is it not scrolling

    Actually, I prefer to just do a setPreferredSize() on the component. It is unwise to override methods which are not documented to do so either in the JDK or by official examples. The designers could change getPreferredSize to do something else while still meeting its advertised contract. You would never know and it could, in theory, break your application. So at a minimum, invoke the super class method first.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: Why is it not scrolling

    But indeed the scroller only scrolls until what "Dimension(500, 2000);" indicates!

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: Why is it not scrolling

    Is that not what I did, Jim? And how would that look like?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: Why is it not scrolling

    Quote Originally Posted by willemjav View Post
    Is that not what I did, Jim? And how would that look like?
    Java Code:
    public PagePane() { 
         setPreferredSize(new Dimension(500,2000));        
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: Why is it not scrolling

    Would this be any better (it does work):

    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    /**
     *
     * @author willem
     */
    public class PagePane  extends JPanel {
        
        public PagePane() {
           this.setPreferredSize(new Dimension(500, 2000));
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
             g.drawRect(50, 50,700, 850);
             g.drawRect(50, 950,700, 850);
          
        }
    
    
        public static void main(String[] arg) {
            PagePane pp = new  PagePane();
            JScrollPane scrollPane = new JScrollPane(pp);
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Willem Writer");
            
            frame.setBounds(400, 30, 800, 1000);
            //PagePane pp = new  PagePane(frame);
            frame.add(scrollPane); // the default is CENTER so you don't need to specify it here
           
            frame.setVisible(true);
         }
    
    }

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: Why is it not scrolling

    Quote Originally Posted by willemjav View Post
    Would this be any better (it does work):
    I won't say any better because it "may" be an acceptable practice to override getPreferredSize() and it apparently works. I just don't override stuff unless I really have too.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: Why is it not scrolling

    I got it, thanks (I did my code btw without seeing yours!)

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: Why is it not scrolling

    I did some perusing of the tutorials and found this in one of their menu examples.

    Java Code:
    public Dimension getMinimumSize() {            
        return getPreferredSize();
    }
     
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }
    The code overrides the min and max size methods. Still seems risky to me.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Why is it not scrolling

    it "may" be an acceptable practice to override getPreferredSize() and it apparently works.
    Not only is it an acceptable practice. It is the preferred practice. Swing components should be responsible for determining their own sizes.

  18. #18
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

Similar Threads

  1. Side Scrolling
    By djokovic in forum Java 2D
    Replies: 2
    Last Post: 08-01-2012, 10:37 PM
  2. Scrolling text
    By gyijhbk in forum New To Java
    Replies: 1
    Last Post: 06-20-2012, 01:50 PM
  3. Scrolling problem
    By lethalwire in forum Eclipse
    Replies: 5
    Last Post: 06-27-2010, 12:37 PM
  4. Side-scrolling
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-21-2010, 05:20 PM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •