Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By Fubarable
  • 2 Post By camickr

Thread: JScrollPane - A little help please

  1. #1
    Ermm is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default JScrollPane - A little help please

    I have searched the internet for the past few hours and no where that i could find had a simple example of using a JPanel and adding a scroll bar to it, or properly using a JScrollPanel showing how to make it so that it scrolls and how to choose the location where the scrollbar is to be placed.

    I'm pretty new new to java and i was hoping someone could just post a very simple yet working way of implementing a scrollbar into a JPanel or using a JScrollPane to scroll vertically down a page.

    I have placed lots of JTextsFields, and JLabels inside it and just want to be able to scrolldown to allow the adding of more.

    Thanks

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

  3. #3
    Ermm is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: JScrollPane - A little help please

    I have read that a few times, but i can't seem to get it right.

    JPanel Panel = new JPanel();
    Panel.setLayout(null);
    Panel.setSize(950, 350);

    JScrollPane Scroll = new JScrollPane(Panel, 950, 500);
    Scroll.setPreferredSize(new Dimension(900, 500));
    Scroll.setVerticalScrollBarPolicy(500);

    obviously i've missed out a few hundred lines of code as i'd rather not post the entire thing on here, but that's what i've pinned it down to. Like i said i have no idea how to use JScrollPane's properly.

    And i had read that, i just wanted it even simpler... Although thanks for the help anyway.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: JScrollPane - A little help please

    You really need to learn to read and study the API.

    For instance, yes, JScrollPane has a constructor that takes a Component and two ints, but the API will tell you what those ints should represent -- the vertical and horizontal scrollbar policies, not the size of the scrollpane.

    Suggestions:
    • First and foremost, read the API of the components that you are using.
    • Do not limit the size of the JPanel that you are adding to as JScrollPane's viewport view.
    • Usually you will want to set the preferredSize of the JScrollPane itself.
    • If the pane JPanel gets larger than the enclosing JScrollPane, the scrollbars will appear automatically (they won't in your case as you constrain the size of pane to that of the JScrollPane itself).
    • Get rid of those "size" ints in your JScrollPane constructor.
    • Read the API (again).
    DarrylBurke likes this.

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

    Default Re: JScrollPane - A little help please

    Don't use a null layout on the panel. Let the layout managers to their job and calculate the preferred size of the panel.
    Fubarable and DarrylBurke like this.

  6. #6
    Ermm is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: JScrollPane - A little help please

    Thanks for the help guys! It turns out i thought the API was something entirely different, was why i was getting confused.
    I've now narrowed it down to the policy, it is compiling, but stating that the policy is the thing causing problems when i run it

    Scroll.setVerticalScrollBarPolicy(VERTICAL_SCROLLB AR_ALWAYS);
    Scroll.getVerticalScrollBarPolicy();

    Also i have said that int VERTICAL_SCROLLBAR_ALWAYS = 1;
    which is what it said somewhere else.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: JScrollPane - A little help please

    Quote Originally Posted by Ermm View Post
    Thanks for the help guys! It turns out i thought the API was something entirely different, was why i was getting confused.
    I've now narrowed it down to the policy, it is compiling, but stating that the policy is the thing causing problems when i run it

    Scroll.setVerticalScrollBarPolicy(VERTICAL_SCROLLB AR_ALWAYS);
    Scroll.getVerticalScrollBarPolicy();

    Also i have said that int VERTICAL_SCROLLBAR_ALWAYS = 1;
    which is what it said somewhere else.
    I'm sorry but I'm confused. What is your question or do you even have a question? Have you solved it?

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

    Default Re: JScrollPane - A little help please

    There is no need to set the policy the scrollbars will appear if required.

    However, if you do override the default setting then you should be using the scrollbar policy variables that are defined in the API.

Similar Threads

  1. JScrollPane Help
    By Huskies in forum AWT / Swing
    Replies: 2
    Last Post: 08-06-2011, 07:06 PM
  2. need jscrollpane
    By aambhi in forum Advanced Java
    Replies: 1
    Last Post: 04-29-2011, 10:38 AM
  3. Need Help With JScrollPane Please
    By MemorableSmile in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 03:39 AM
  4. JScrollPane
    By hiddenpremise in forum AWT / Swing
    Replies: 2
    Last Post: 12-25-2010, 06:15 AM
  5. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 09:39 AM

Tags for this Thread

Posting Permissions

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