Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Unable to add JPanel to JScrollPane

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unable to add JPanel to JScrollPane

    Hi,

    I want to add a JPanel (called panel) to the mother object, in this case another JPanel. On 'panel' there should be a list of some CheckBoxes, and since their number is not constant, I want to have a possibility to scroll down the 'panel' to see them all if there are more of them. So, I wanted to add 'panel' to a JScrollPane scrollPane, but it does not work. Do you know what have I done wrong?

            JPanel panel;
            JScrollPane scrollPane;
     
    //this is in the constructor of the mother object, JPanel
            panel = new JPanel();
            panel.setBorder(BorderFactory.createEtchedBorder());
            panel.setBounds(0,0,415,420);
            panel.setBackground(new Color(255,255,255));
     
            scrollPane = new JScrollPane(panel);
            add(scrollPane, 0, 40, 415, 400);
     
    //method add defined in the mother object
        public void add(Component c, int x, int y, int sX, int sY)
        {
            add(c);
            c.setBounds(x,y,sX,sY);
        }

    In the final program, the scrollPane appears on the mother JPanel, but without any sign of the 'panel'. I tried to add some other object to 'panel' (test JLabels and JButtons), but it was still the same - no 'panel' clipped to the scrollPane.

    Thank you very much for your response.


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Unable to add JPanel to JScrollPane

    Try this. I did not actually test this but I think this is your problem. Add the panel to the viewport of the scroll pane.

     
            JPanel panel;
            JScrollPane scrollPane;
     
    //this is in the constructor of the mother object, JPanel
            panel = new JPanel();
            panel.setBorder(BorderFactory.createEtchedBorder());
            panel.setBounds(0,0,415,420);
            panel.setBackground(new Color(255,255,255));
     
            scrollPane = new JScrollPane();
            scrollPane.getViewport().add(panel)//<---Add the panel to the viewport of the scroll pane.
            add(scrollPane, 0, 40, 415, 400);
     
    //method add defined in the mother object
        public void add(Component c, int x, int y, int sX, int sY)
        {
            add(c);
            c.setBounds(x,y,sX,sY);
        }

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unable to add JPanel to JScrollPane

    No, it has to be something else, that does very much the same. :/

    I've also tried not to set bounds of 'panel' but set its preferred size, but with no success.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Unable to add JPanel to JScrollPane

    Don't use setBounds (unless you are using null layouts, which I advise against). Use setPreferredSize to set the size of the panel. Try the following:

            panel.setPreferredSize(new Dimension(415,420));
            panel.setBackground(new Color(255,255,255));
            scrollPane = new JScrollPane(panel);

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unable to add JPanel to JScrollPane

    This I've tried before (as I mentioned above), but still nothing. I do not know what else should I do. It seems like the 'panel' was not created or what (for example, for JTextPane it does exactly the same thing, until I set its Text... but I do not know about the alternative for setText in JPanel, so I cannot try it)

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Unable to add JPanel to JScrollPane

    Quote Originally Posted by nenay View Post
    This I've tried before (as I mentioned above), but still nothing. I do not know what else should I do. It seems like the 'panel' was not created or what (for example, for JTextPane it does exactly the same thing, until I set its Text... but I do not know about the alternative for setText in JPanel, so I cannot try it)
    Post an SSCCE that reproduces the problem, which allows one to test the code as opposed to guessing at things such as layouts, etc...and allow you to break the problem down to the troublesome part (and often the solution reveals itself when making the SSCCE)

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Unable to add JPanel to JScrollPane

    I tested your code and changed setbounds to setPreferredsize and it worked. I changed your color to black to make it easier to see over the white on white.

    package testing;
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
     
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
     
    public class TestScroller extends JPanel
    {
        JPanel panel;
        JScrollPane scrollPane;
     
        public TestScroller() {
    		// TODO Auto-generated constructor stub
     
    //this is in the constructor of the mother object, JPanel
        panel = new JPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        //panel.setBounds(0,0,415,420);
     
        panel.setPreferredSize(new Dimension(415,420));
        panel.setBackground(Color.BLACK);
     
        scrollPane = new JScrollPane(panel);
        scrollPane.setPreferredSize(new Dimension(415,400));
        add(scrollPane);
      //  add(scrollPane, 0, 40, 415, 400);
        }
    /*  //method add defined in the mother object
          public void add(Component c, int x, int y, int sX, int sY)
          {
              add(c);
              c.setBounds(x,y,sX,sY);
          }*/
     
     
     
     
    	public static void main( String args[] )
    	{
    		JPanel baseView = new TestScroller();
    		baseView.setPreferredSize(new Dimension(500,500));
    		baseView.setOpaque(false);
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(baseView);
    		frame.setSize(500,500);
     
    		frame.setVisible(true);
    		frame.repaint();
    	}
    }

    So if preferredSize is not working for you then some where else in the code it is broken and not in the section you provide.

  8. #8
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Unable to add JPanel to JScrollPane

    You're my boy mesatrin1!

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unable to add JPanel to JScrollPane

    Thank you guys! Yes, I am using null layout, and I know it has many disadvantages, but in this programme I wanted to have it like this. And I found the problem - the single repaint on the mother JFrame was not enough, bud when I resized it, the JPanel appeared in the JScrollPane. So, I found the solution, although it is weird, and my problem is solved. Once again, thank you for your help.

Similar Threads

  1. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  2. Add JScrollPane to a JPanel
    By alibm in forum AWT / Java Swing
    Replies: 2
    Last Post: March 7th, 2011, 02:59 PM
  3. JPanel and JScrollPane inheritance
    By ohiggins in forum AWT / Java Swing
    Replies: 2
    Last Post: February 15th, 2011, 10:06 AM
  4. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  5. Unable to render printing the complete JPanel
    By Stephen Douglas in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 11:48 AM